|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
1 | 3 | use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
|
2 | 4 | use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context};
|
3 | 5 | use clippy_utils::sugg::Sugg;
|
4 | 6 | use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
|
5 |
| -use clippy_utils::{get_parent_expr, is_trait_method, is_ty_alias, match_def_path, path_to_local, paths}; |
| 7 | +use clippy_utils::{ |
| 8 | + get_parent_expr, is_diag_trait_item, is_trait_method, is_ty_alias, match_def_path, path_to_local, paths, |
| 9 | +}; |
6 | 10 | use if_chain::if_chain;
|
7 | 11 | use rustc_errors::Applicability;
|
8 | 12 | use rustc_hir::def_id::DefId;
|
9 | 13 | use rustc_hir::{BindingAnnotation, Expr, ExprKind, HirId, MatchSource, Node, PatKind};
|
10 | 14 | use rustc_lint::{LateContext, LateLintPass};
|
11 |
| -use rustc_middle::ty; |
| 15 | +use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor}; |
12 | 16 | use rustc_session::{declare_tool_lint, impl_lint_pass};
|
13 | 17 | use rustc_span::{sym, Span};
|
14 | 18 |
|
@@ -59,22 +63,83 @@ impl MethodOrFunction {
|
59 | 63 | }
|
60 | 64 | }
|
61 | 65 |
|
62 |
| -/// Returns the span of the `IntoIterator` trait bound in the function pointed to by `fn_did` |
63 |
| -fn into_iter_bound(cx: &LateContext<'_>, fn_did: DefId, into_iter_did: DefId, param_index: u32) -> Option<Span> { |
64 |
| - cx.tcx |
65 |
| - .predicates_of(fn_did) |
66 |
| - .predicates |
67 |
| - .iter() |
68 |
| - .find_map(|&(ref pred, span)| { |
69 |
| - if let ty::ClauseKind::Trait(tr) = pred.kind().skip_binder() |
70 |
| - && tr.def_id() == into_iter_did |
71 |
| - && tr.self_ty().is_param(param_index) |
| 66 | +/// Returns the span of the `IntoIterator` trait bound in the function pointed to by `fn_did`, |
| 67 | +/// iff the `IntoIterator` bound is the only bound on the type parameter. |
| 68 | +/// |
| 69 | +/// This last part is important because it might be that the type the user is calling `.into_iter()` |
| 70 | +/// on might not satisfy those other bounds and would result in compile errors: |
| 71 | +/// ```ignore |
| 72 | +/// pub fn foo<I>(i: I) |
| 73 | +/// where I: IntoIterator<Item=i32> + ExactSizeIterator |
| 74 | +/// ^^^^^^^^^^^^^^^^^ this extra bound stops us from suggesting to remove `.into_iter()` ... |
| 75 | +/// { |
| 76 | +/// assert_eq!(i.len(), 3); |
| 77 | +/// } |
| 78 | +/// |
| 79 | +/// pub fn bar() { |
| 80 | +/// foo([1, 2, 3].into_iter()); |
| 81 | +/// ^^^^^^^^^^^^ ... here, because `[i32; 3]` is not `ExactSizeIterator` |
| 82 | +/// } |
| 83 | +/// ``` |
| 84 | +fn exclusive_into_iter_bound( |
| 85 | + cx: &LateContext<'_>, |
| 86 | + fn_did: DefId, |
| 87 | + into_iter_did: DefId, |
| 88 | + param_index: u32, |
| 89 | +) -> Option<Span> { |
| 90 | + #[derive(Clone)] |
| 91 | + struct ExplicitlyUsedTyParam<'a, 'tcx> { |
| 92 | + cx: &'a LateContext<'tcx>, |
| 93 | + param_index: u32, |
| 94 | + } |
| 95 | + |
| 96 | + impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for ExplicitlyUsedTyParam<'a, 'tcx> { |
| 97 | + type BreakTy = (); |
| 98 | + fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 99 | + // Ignore implicit `T: Sized` bound |
| 100 | + if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) = p.kind().skip_binder() |
| 101 | + && let Some(sized_trait_did) = self.cx.tcx.lang_items().sized_trait() |
| 102 | + && sized_trait_did == tr.def_id() |
72 | 103 | {
|
73 |
| - Some(span) |
| 104 | + return ControlFlow::Continue(()); |
| 105 | + } |
| 106 | + |
| 107 | + // Ignore `<T as IntoIterator>::Item` projection, this use of the ty param specifically is fine |
| 108 | + // because it's what we're already looking for |
| 109 | + if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) = p.kind().skip_binder() |
| 110 | + && is_diag_trait_item(self.cx,proj.projection_ty.def_id, sym::IntoIterator) |
| 111 | + { |
| 112 | + return ControlFlow::Continue(()); |
| 113 | + } |
| 114 | + |
| 115 | + p.super_visit_with(self) |
| 116 | + } |
| 117 | + |
| 118 | + fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 119 | + if t.is_param(self.param_index) { |
| 120 | + ControlFlow::Break(()) |
74 | 121 | } else {
|
75 |
| - None |
| 122 | + ControlFlow::Continue(()) |
76 | 123 | }
|
77 |
| - }) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let mut into_iter_span = None; |
| 128 | + |
| 129 | + for (pred, span) in cx.tcx.explicit_predicates_of(fn_did).predicates { |
| 130 | + if let ty::ClauseKind::Trait(tr) = pred.kind().skip_binder() |
| 131 | + && tr.def_id() == into_iter_did |
| 132 | + && tr.self_ty().is_param(param_index) |
| 133 | + { |
| 134 | + into_iter_span = Some(*span); |
| 135 | + } else if pred.visit_with(&mut ExplicitlyUsedTyParam { cx, param_index }).is_break() { |
| 136 | + // Found another reference of the type parameter; conservatively assume |
| 137 | + // that we can't remove the bound. |
| 138 | + return None; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + into_iter_span |
78 | 143 | }
|
79 | 144 |
|
80 | 145 | /// Extracts the receiver of a `.into_iter()` method call.
|
@@ -167,7 +232,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
|
167 | 232 | && let Some(arg_pos) = args.iter().position(|x| x.hir_id == e.hir_id)
|
168 | 233 | && let Some(&into_iter_param) = sig.inputs().get(kind.param_pos(arg_pos))
|
169 | 234 | && let ty::Param(param) = into_iter_param.kind()
|
170 |
| - && let Some(span) = into_iter_bound(cx, parent_fn_did, into_iter_did, param.index) |
| 235 | + && let Some(span) = exclusive_into_iter_bound(cx, parent_fn_did, into_iter_did, param.index) |
171 | 236 | {
|
172 | 237 | // Get the "innermost" `.into_iter()` call, e.g. given this expression:
|
173 | 238 | // `foo.into_iter().into_iter()`
|
|
0 commit comments