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