]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/loops.rs
Make needless_range_loop not applicable to structures without iter method
[rust.git] / clippy_lints / src / loops.rs
index 058d9adcb514e8ce181522083b3e2710cdba9f02..fcf6a879b91574d1c553f5dcc55e62ed5f533632 100644 (file)
@@ -27,7 +27,7 @@
 
 use crate::utils::paths;
 use crate::utils::{
-    get_enclosing_block, get_parent_expr, higher, is_integer_literal, is_refutable, last_path_segment,
+    get_enclosing_block, get_parent_expr, has_iter_method, higher, is_integer_literal, is_refutable, last_path_segment,
     match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt, snippet_with_applicability,
     span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq,
 };
@@ -1118,6 +1118,12 @@ fn check_for_loop_range<'a, 'tcx>(
                     }
                 }
 
+                // don't lint if the container that is indexed does not have .iter() method
+                let has_iter = has_iter_method(cx, indexed_ty);
+                if has_iter.is_none() {
+                    return;
+                }
+
                 // don't lint if the container that is indexed into is also used without
                 // indexing
                 if visitor.referenced.contains(&indexed) {