|
| 1 | +use crate::utils::{ |
| 2 | + is_adjusted, is_type_diagnostic_item, match_trait_method, match_var, paths, remove_blocks, span_lint_and_sugg, |
| 3 | +}; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_lint::{LateLintPass, LateContext}; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_hir::*; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// **What it does:** Checks for instances `iterator.map(f)` where `f` is the identity function. |
| 12 | + /// |
| 13 | + /// **Why is this bad?** It can be written more concisely as `iterator`, without the call to `map`. |
| 14 | + /// |
| 15 | + /// **Known problems:** None. |
| 16 | + /// |
| 17 | + /// **Example:** |
| 18 | + /// |
| 19 | + /// ```rust |
| 20 | + /// // example code where clippy issues a warning |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// // example code which does not raise clippy warning |
| 25 | + /// ``` |
| 26 | + pub MAP_IDENTITY, |
| 27 | + style, |
| 28 | + "default lint description" |
| 29 | +} |
| 30 | + |
| 31 | +declare_lint_pass!(MapIdentity => [MAP_IDENTITY]); |
| 32 | + |
| 33 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapIdentity { |
| 34 | + fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) { |
| 35 | + // println!("start"); |
| 36 | + if expr.span.from_expansion() { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if_chain! { |
| 41 | + if let Some(func) = get_map_argument(cx, expr); |
| 42 | + if let Some(body) = get_body(cx, func); |
| 43 | + if is_identity_function(cx, body); |
| 44 | + then { |
| 45 | + span_lint_and_sugg( |
| 46 | + cx, |
| 47 | + MAP_IDENTITY, |
| 48 | + expr.span, |
| 49 | + "Unnecessary map of the identity function", |
| 50 | + "Remove the `map` call", |
| 51 | + String::new(), |
| 52 | + Applicability::MachineApplicable |
| 53 | + ) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +/// Returns the function passed into iterator.map() if the expression is a method call to |
| 61 | +/// iterator.map(). Otherwise, returns None. |
| 62 | +fn get_map_argument<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<&'a Expr<'a>> { |
| 63 | + if_chain! { |
| 64 | + if let ExprKind::MethodCall(ref method, _, ref args) = expr.kind; |
| 65 | + if args.len() == 2 && method.ident.as_str() == "map"; |
| 66 | + let caller_ty = cx.tables.expr_ty(&args[0]); |
| 67 | + if match_trait_method(cx, expr, &paths::ITERATOR) |
| 68 | + || is_type_diagnostic_item(cx, caller_ty, sym!(result_type)) |
| 69 | + || is_type_diagnostic_item(cx, caller_ty, sym!(option_type)); |
| 70 | + then { |
| 71 | + Some(&args[1]) |
| 72 | + } else { |
| 73 | + None |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Determines if a function is the identity function (in a naive way) |
| 79 | +fn is_identity_function(cx: &LateContext<'_, '_>, func: &Body<'_>) -> bool { |
| 80 | + let params = func.params; |
| 81 | + let body = remove_blocks(&func.value); |
| 82 | + |
| 83 | + // if there's less/more than one parameter, then it is not the identity function |
| 84 | + if params.len() != 1 { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + match body.kind { |
| 89 | + ExprKind::Path(QPath::Resolved(None, _)) => match_expr_param(cx, body, params[0].pat), |
| 90 | + ExprKind::Ret(Some(ref ret_val)) => match_expr_param(cx, ret_val, params[0].pat), |
| 91 | + ExprKind::Block(ref block, _) => { |
| 92 | + if_chain! { |
| 93 | + if block.stmts.len() == 1; |
| 94 | + if let StmtKind::Semi(ref expr) | StmtKind::Expr(ref expr) = block.stmts[0].kind; |
| 95 | + if let ExprKind::Ret(Some(ref ret_val)) = expr.kind; |
| 96 | + then { |
| 97 | + match_expr_param(cx, ret_val, params[0].pat) |
| 98 | + } else { |
| 99 | + false |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + _ => false |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Returns an associated function/closure body from an expression |
| 108 | +fn get_body<'a>(cx: &LateContext<'a, '_>, expr: &'a Expr<'a>) -> Option<&'a Body<'a>> { |
| 109 | + match expr.kind { |
| 110 | + ExprKind::Closure(_, _, body_id, _, _) => Some(cx.tcx.hir().body(body_id)), |
| 111 | + ExprKind::Path(QPath::Resolved(_, ref path)) => path_to_body(cx, path), |
| 112 | + _ => None |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/// Returns the function body associated with a path |
| 117 | +fn path_to_body<'a>(cx: &LateContext<'a, '_>, path: &'a Path<'a>) -> Option<&'a Body<'a>> { |
| 118 | + if let def::Res::Def(_, def_id) = path.res { |
| 119 | + def_id.as_local() |
| 120 | + .and_then(|local_id| cx.tcx.hir().opt_local_def_id_to_hir_id(local_id)) |
| 121 | + .and_then(|hir_id| cx.tcx.hir().maybe_body_owned_by(hir_id)) |
| 122 | + .map(|body_id| cx.tcx.hir().body(body_id)) |
| 123 | + } else { |
| 124 | + None |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Determines if an expression (syntactically) returns the same thing as a parameter's pattern |
| 129 | +fn match_expr_param(cx: &LateContext<'_, '_>, expr: &Expr<'_>, pat: &Pat<'_>) -> bool { |
| 130 | + if let PatKind::Binding(_, _, ident, _) = pat.kind { |
| 131 | + match_var(expr, ident.name) && !(cx.tables.hir_owner == Some(expr.hir_id.owner) && is_adjusted(cx, expr)) |
| 132 | + } else { |
| 133 | + false |
| 134 | + } |
| 135 | +} |
0 commit comments