|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use clippy_utils::{get_parent_expr, is_trait_method}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::def_id::DefId; |
| 6 | +use rustc_hir::*; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | +use rustc_span::sym; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for calls to [`IntoIterator::into_iter`](https://doc.rust-lang.org/stable/std/iter/trait.IntoIterator.html#tymethod.into_iter) |
| 16 | + /// in a call argument that accepts `IntoIterator`. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// If a function has a generic parameter with an `IntoIterator` trait bound, it means that the function |
| 20 | + /// will *have* to call `.into_iter()` to get an iterator out of it, thereby making the call to `.into_iter()` |
| 21 | + /// at call site redundant. |
| 22 | + /// |
| 23 | + /// Consider this example: |
| 24 | + /// ```rs,ignore |
| 25 | + /// fn foo<T: IntoIterator<Item = u32>>(iter: T) { |
| 26 | + /// let it = iter.into_iter(); |
| 27 | + /// ^^^^^^^^^^^^ the function has to call `.into_iter()` to get the iterator |
| 28 | + /// } |
| 29 | + /// |
| 30 | + /// foo(vec![1, 2, 3].into_iter()); |
| 31 | + /// ^^^^^^^^^^^^ ... making this `.into_iter()` call redundant. |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + /// The reason for why calling `.into_iter()` twice (once at call site and again inside of the function) works in the first place |
| 35 | + /// is because there is a blanket implementation of `IntoIterator` for all types that implement `Iterator` in the standard library, |
| 36 | + /// in which it simply returns itself, effectively making the second call to `.into_iter()` a "no-op": |
| 37 | + /// ```rust,ignore |
| 38 | + /// impl<I: Iterator> IntoIterator for I { |
| 39 | + /// type Item = I::Item; |
| 40 | + /// type IntoIter = I; |
| 41 | + /// |
| 42 | + /// fn into_iter(self) -> I { |
| 43 | + /// self |
| 44 | + /// } |
| 45 | + /// } |
| 46 | + /// ``` |
| 47 | + /// |
| 48 | + /// ### Example |
| 49 | + /// ```rust |
| 50 | + /// fn even_sum<I: IntoIterator<Item = u32>>(iter: I) -> u32 { |
| 51 | + /// iter.into_iter().filter(|&x| x % 2 == 0).sum() |
| 52 | + /// } |
| 53 | + /// |
| 54 | + /// let _ = even_sum(vec![1, 2, 3].into_iter()); |
| 55 | + /// ``` |
| 56 | + /// Use instead: |
| 57 | + /// ```rust |
| 58 | + /// fn even_sum<I: IntoIterator<Item = u32>>(iter: I) -> u32 { |
| 59 | + /// iter.into_iter().filter(|&x| x % 2 == 0).sum() |
| 60 | + /// } |
| 61 | + /// |
| 62 | + /// let _ = even_sum(vec![1, 2, 3]); |
| 63 | + /// ``` |
| 64 | + #[clippy::version = "1.71.0"] |
| 65 | + pub EXPLICIT_INTO_ITER_FN_ARG, |
| 66 | + pedantic, |
| 67 | + "explicit call to `.into_iter()` in function argument accepting `IntoIterator`" |
| 68 | +} |
| 69 | +declare_lint_pass!(ExplicitIntoIterFnArg => [EXPLICIT_INTO_ITER_FN_ARG]); |
| 70 | + |
| 71 | +enum MethodOrFunction { |
| 72 | + Method, |
| 73 | + Function, |
| 74 | +} |
| 75 | + |
| 76 | +/// Returns the span of the `IntoIterator` trait bound in the function pointed to by `fn_did` |
| 77 | +fn into_iter_bound(cx: &LateContext<'_>, fn_did: DefId, param_index: u32) -> Option<Span> { |
| 78 | + if let Some(into_iter_did) = cx.tcx.get_diagnostic_item(sym::IntoIterator) { |
| 79 | + cx.tcx |
| 80 | + .predicates_of(fn_did) |
| 81 | + .predicates |
| 82 | + .iter() |
| 83 | + .find_map(|&(ref pred, span)| { |
| 84 | + if let ty::PredicateKind::Clause(ty::Clause::Trait(tr)) = pred.kind().skip_binder() |
| 85 | + && tr.def_id() == into_iter_did |
| 86 | + && tr.self_ty().is_param(param_index) |
| 87 | + { |
| 88 | + Some(span) |
| 89 | + } else { |
| 90 | + None |
| 91 | + } |
| 92 | + }) |
| 93 | + } else { |
| 94 | + None |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<'tcx> LateLintPass<'tcx> for ExplicitIntoIterFnArg { |
| 99 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 100 | + if expr.span.from_expansion() { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if let ExprKind::MethodCall(name, recv, ..) = expr.kind |
| 105 | + && is_trait_method(cx, expr, sym::IntoIterator) |
| 106 | + && name.ident.name == sym::into_iter |
| 107 | + && let Some(parent_expr) = get_parent_expr(cx, expr) |
| 108 | + { |
| 109 | + let parent_expr = match parent_expr.kind { |
| 110 | + ExprKind::Call(recv, args) if let ExprKind::Path(ref qpath) = recv.kind => { |
| 111 | + cx.qpath_res(qpath, recv.hir_id).opt_def_id() |
| 112 | + .map(|did| (did, args, MethodOrFunction::Function)) |
| 113 | + } |
| 114 | + ExprKind::MethodCall(.., args, _) => { |
| 115 | + cx.typeck_results().type_dependent_def_id(parent_expr.hir_id) |
| 116 | + .map(|did| (did, args, MethodOrFunction::Method)) |
| 117 | + } |
| 118 | + _ => None, |
| 119 | + }; |
| 120 | + |
| 121 | + if let Some((parent_fn_did, args, kind)) = parent_expr |
| 122 | + && let sig = cx.tcx.fn_sig(parent_fn_did).skip_binder().skip_binder() |
| 123 | + && let Some(arg_pos) = args.iter().position(|x| x.hir_id == expr.hir_id) |
| 124 | + && let Some(&into_iter_param) = sig.inputs().get(match kind { |
| 125 | + MethodOrFunction::Function => arg_pos, |
| 126 | + MethodOrFunction::Method => arg_pos + 1, // skip self arg |
| 127 | + }) |
| 128 | + && let ty::Param(param) = into_iter_param.kind() |
| 129 | + && let Some(span) = into_iter_bound(cx, parent_fn_did, param.index) |
| 130 | + { |
| 131 | + let sugg = snippet(cx, recv.span.source_callsite(), "<expr>").into_owned(); |
| 132 | + span_lint_and_then(cx, EXPLICIT_INTO_ITER_FN_ARG, expr.span, "explicit call to `.into_iter()` in function argument accepting `IntoIterator`", |diag| { |
| 133 | + diag.span_suggestion( |
| 134 | + expr.span, |
| 135 | + "consider removing `.into_iter()`", |
| 136 | + sugg, |
| 137 | + Applicability::MachineApplicable, |
| 138 | + ); |
| 139 | + diag.span_note(span, "this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`"); |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments