about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-10-01 16:29:08 +0000
committerbors <bors@rust-lang.org>2020-10-01 16:29:08 +0000
commit8c9800a3a9026aa3cc1071276d0672585ba2175e (patch)
tree43f094e2ddd0c5f94f50b4efcef588e10a9b7695
parentd4313737d819891ae0fe790febe5a0a622e1b4a5 (diff)
parent0a91fe7016cdb48d3824c218e02eca8cfdec3854 (diff)
downloadrust-8c9800a3a9026aa3cc1071276d0672585ba2175e.tar.gz
rust-8c9800a3a9026aa3cc1071276d0672585ba2175e.zip
Auto merge of #6102 - giraffate:no_lint_when_invalid_suggestion_in_needless_range_loop, r=flip1995
Don't emit a lint for the suggestion leading to errors in `needless_range_loop`

Fix #5945

changelog: Don't emit a lint for the suggestion leading to errors in `needless_range_loop`
-rw-r--r--clippy_lints/src/loops.rs6
-rw-r--r--tests/ui/needless_range_loop2.rs14
2 files changed, 19 insertions, 1 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs
index 7f998c63f49..61b63597b16 100644
--- a/clippy_lints/src/loops.rs
+++ b/clippy_lints/src/loops.rs
@@ -3,7 +3,7 @@ use crate::utils::paths;
 use crate::utils::sugg::Sugg;
 use crate::utils::usage::{is_unused, mutated_variables};
 use crate::utils::{
-    get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
+    contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
     is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_trait_method,
     match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_opt, snippet_with_applicability,
     snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
@@ -1276,6 +1276,8 @@ fn check_for_loop_range<'tcx>(
 
                 let skip = if starts_at_zero {
                     String::new()
+                } else if visitor.indexed_mut.contains(&indexed) && contains_name(indexed, start) {
+                    return;
                 } else {
                     format!(".skip({})", snippet(cx, start.span, ".."))
                 };
@@ -1302,6 +1304,8 @@ fn check_for_loop_range<'tcx>(
 
                     if is_len_call(end, indexed) || is_end_eq_array_len(cx, end, limits, indexed_ty) {
                         String::new()
+                    } else if visitor.indexed_mut.contains(&indexed) && contains_name(indexed, take_expr) {
+                        return;
                     } else {
                         match limits {
                             ast::RangeLimits::Closed => {
diff --git a/tests/ui/needless_range_loop2.rs b/tests/ui/needless_range_loop2.rs
index a82b1159161..7633316e0f8 100644
--- a/tests/ui/needless_range_loop2.rs
+++ b/tests/ui/needless_range_loop2.rs
@@ -82,6 +82,20 @@ fn main() {
     for i in 1..3 {
         println!("{}", arr[i]);
     }
+
+    // Fix #5945
+    let mut vec = vec![1, 2, 3, 4];
+    for i in 0..vec.len() - 1 {
+        vec[i] += 1;
+    }
+    let mut vec = vec![1, 2, 3, 4];
+    for i in vec.len() - 3..vec.len() {
+        vec[i] += 1;
+    }
+    let mut vec = vec![1, 2, 3, 4];
+    for i in vec.len() - 3..vec.len() - 1 {
+        vec[i] += 1;
+    }
 }
 
 mod issue2277 {