|
1 | 1 | use crate::utils::paths; |
2 | 2 | use crate::utils::sugg; |
3 | 3 | use crate::utils::{ |
4 | | - get_arg_name, get_parent_expr, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self, |
5 | | - is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method, |
6 | | - match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, |
7 | | - snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg, |
8 | | - span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq, |
| 4 | + get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, is_expn_of, |
| 5 | + is_self, is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, |
| 6 | + match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, |
| 7 | + single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, |
| 8 | + span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq, |
9 | 9 | }; |
10 | 10 | use if_chain::if_chain; |
11 | 11 | use matches::matches; |
@@ -2237,47 +2237,23 @@ fn ty_has_iter_method( |
2237 | 2237 | cx: &LateContext<'_, '_>, |
2238 | 2238 | self_ref_ty: ty::Ty<'_>, |
2239 | 2239 | ) -> Option<(&'static Lint, &'static str, &'static str)> { |
2240 | | - // FIXME: instead of this hard-coded list, we should check if `<adt>::iter` |
2241 | | - // exists and has the desired signature. Unfortunately FnCtxt is not exported |
2242 | | - // so we can't use its `lookup_method` method. |
2243 | | - static INTO_ITER_COLLECTIONS: [(&Lint, &[&str]); 13] = [ |
2244 | | - (INTO_ITER_ON_REF, &paths::VEC), |
2245 | | - (INTO_ITER_ON_REF, &paths::OPTION), |
2246 | | - (INTO_ITER_ON_REF, &paths::RESULT), |
2247 | | - (INTO_ITER_ON_REF, &paths::BTREESET), |
2248 | | - (INTO_ITER_ON_REF, &paths::BTREEMAP), |
2249 | | - (INTO_ITER_ON_REF, &paths::VEC_DEQUE), |
2250 | | - (INTO_ITER_ON_REF, &paths::LINKED_LIST), |
2251 | | - (INTO_ITER_ON_REF, &paths::BINARY_HEAP), |
2252 | | - (INTO_ITER_ON_REF, &paths::HASHSET), |
2253 | | - (INTO_ITER_ON_REF, &paths::HASHMAP), |
2254 | | - (INTO_ITER_ON_ARRAY, &["std", "path", "PathBuf"]), |
2255 | | - (INTO_ITER_ON_REF, &["std", "path", "Path"]), |
2256 | | - (INTO_ITER_ON_REF, &["std", "sync", "mpsc", "Receiver"]), |
2257 | | - ]; |
2258 | | - |
2259 | | - let (self_ty, mutbl) = match self_ref_ty.sty { |
2260 | | - ty::Ref(_, self_ty, mutbl) => (self_ty, mutbl), |
2261 | | - _ => unreachable!(), |
2262 | | - }; |
2263 | | - let method_name = match mutbl { |
2264 | | - hir::MutImmutable => "iter", |
2265 | | - hir::MutMutable => "iter_mut", |
2266 | | - }; |
2267 | | - |
2268 | | - let def_id = match self_ty.sty { |
2269 | | - ty::Array(..) => return Some((INTO_ITER_ON_ARRAY, "array", method_name)), |
2270 | | - ty::Slice(..) => return Some((INTO_ITER_ON_REF, "slice", method_name)), |
2271 | | - ty::Adt(adt, _) => adt.did, |
2272 | | - _ => return None, |
2273 | | - }; |
2274 | | - |
2275 | | - for (lint, path) in &INTO_ITER_COLLECTIONS { |
2276 | | - if match_def_path(cx.tcx, def_id, path) { |
2277 | | - return Some((lint, path.last().unwrap(), method_name)); |
2278 | | - } |
| 2240 | + if let Some(ty_name) = has_iter_method(cx, self_ref_ty) { |
| 2241 | + let lint = match ty_name { |
| 2242 | + "array" | "PathBuf" => INTO_ITER_ON_ARRAY, |
| 2243 | + _ => INTO_ITER_ON_REF, |
| 2244 | + }; |
| 2245 | + let mutbl = match self_ref_ty.sty { |
| 2246 | + ty::Ref(_, _, mutbl) => mutbl, |
| 2247 | + _ => unreachable!(), |
| 2248 | + }; |
| 2249 | + let method_name = match mutbl { |
| 2250 | + hir::MutImmutable => "iter", |
| 2251 | + hir::MutMutable => "iter_mut", |
| 2252 | + }; |
| 2253 | + Some((lint, ty_name, method_name)) |
| 2254 | + } else { |
| 2255 | + None |
2279 | 2256 | } |
2280 | | - None |
2281 | 2257 | } |
2282 | 2258 |
|
2283 | 2259 | fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: ty::Ty<'_>, method_span: Span) { |
|
0 commit comments