2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-13 17:01:03 -06:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::{is_adjusted, is_trait_method, match_path, match_var, paths, remove_blocks};
|
2020-07-14 07:59:59 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-02 10:32:55 -06:00
|
|
|
use rustc_span::sym;
|
2020-07-14 07:59:59 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for instances of `map(f)` where `f` is the identity function.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It can be written more concisely without the call to `map`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let x = [1, 2, 3];
|
|
|
|
/// let y: Vec<_> = x.iter().map(|x| x).map(|x| 2*x).collect();
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let x = [1, 2, 3];
|
|
|
|
/// let y: Vec<_> = x.iter().map(|x| 2*x).collect();
|
|
|
|
/// ```
|
|
|
|
pub MAP_IDENTITY,
|
|
|
|
complexity,
|
|
|
|
"using iterator.map(|x| x)"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(MapIdentity => [MAP_IDENTITY]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for MapIdentity {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
if expr.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if let Some([caller, func]) = get_map_argument(cx, expr);
|
|
|
|
if is_expr_identity_function(cx, func);
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_IDENTITY,
|
|
|
|
expr.span.trim_start(caller.span).unwrap(),
|
|
|
|
"unnecessary map of the identity function",
|
|
|
|
"remove the call to `map`",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the arguments passed into map() if the expression is a method call to
|
|
|
|
/// map(). Otherwise, returns None.
|
|
|
|
fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a [Expr<'a>]> {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
|
2020-12-29 16:04:31 -06:00
|
|
|
if args.len() == 2 && method.ident.name == sym::map;
|
2020-07-17 03:47:04 -05:00
|
|
|
let caller_ty = cx.typeck_results().expr_ty(&args[0]);
|
2021-03-12 11:09:19 -06:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator)
|
2020-11-02 10:32:55 -06:00
|
|
|
|| is_type_diagnostic_item(cx, caller_ty, sym::result_type)
|
|
|
|
|| is_type_diagnostic_item(cx, caller_ty, sym::option_type);
|
2020-07-14 07:59:59 -05:00
|
|
|
then {
|
|
|
|
Some(args)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if an expression represents the identity function
|
|
|
|
/// Only examines closures and `std::convert::identity`
|
|
|
|
fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)),
|
|
|
|
ExprKind::Path(QPath::Resolved(_, ref path)) => match_path(path, &paths::STD_CONVERT_IDENTITY),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a function's body represents the identity function
|
|
|
|
/// Looks for bodies of the form `|x| x`, `|x| return x`, `|x| { return x }` or `|x| {
|
|
|
|
/// return x; }`
|
|
|
|
fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
|
|
|
|
let params = func.params;
|
|
|
|
let body = remove_blocks(&func.value);
|
|
|
|
|
|
|
|
// if there's less/more than one parameter, then it is not the identity function
|
|
|
|
if params.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match body.kind {
|
|
|
|
ExprKind::Path(QPath::Resolved(None, _)) => match_expr_param(cx, body, params[0].pat),
|
|
|
|
ExprKind::Ret(Some(ref ret_val)) => match_expr_param(cx, ret_val, params[0].pat),
|
|
|
|
ExprKind::Block(ref block, _) => {
|
|
|
|
if_chain! {
|
|
|
|
if block.stmts.len() == 1;
|
|
|
|
if let StmtKind::Semi(ref expr) | StmtKind::Expr(ref expr) = block.stmts[0].kind;
|
|
|
|
if let ExprKind::Ret(Some(ref ret_val)) = expr.kind;
|
|
|
|
then {
|
|
|
|
match_expr_param(cx, ret_val, params[0].pat)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true iff an expression returns the same thing as a parameter's pattern
|
|
|
|
fn match_expr_param(cx: &LateContext<'_>, expr: &Expr<'_>, pat: &Pat<'_>) -> bool {
|
|
|
|
if let PatKind::Binding(_, _, ident, _) = pat.kind {
|
2020-07-17 03:47:04 -05:00
|
|
|
match_var(expr, ident.name) && !(cx.typeck_results().hir_owner == expr.hir_id.owner && is_adjusted(cx, expr))
|
2020-07-14 07:59:59 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|