Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8c9800a

Browse files
committed
Auto merge of rust-lang#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 rust-lang#5945 changelog: Don't emit a lint for the suggestion leading to errors in `needless_range_loop`
2 parents d431373 + 0a91fe7 commit 8c9800a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::usage::{is_unused, mutated_variables};
55
use crate::utils::{
6-
get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
6+
contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
77
is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_trait_method,
88
match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_opt, snippet_with_applicability,
99
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>(
12761276

12771277
let skip = if starts_at_zero {
12781278
String::new()
1279+
} else if visitor.indexed_mut.contains(&indexed) && contains_name(indexed, start) {
1280+
return;
12791281
} else {
12801282
format!(".skip({})", snippet(cx, start.span, ".."))
12811283
};
@@ -1302,6 +1304,8 @@ fn check_for_loop_range<'tcx>(
13021304

13031305
if is_len_call(end, indexed) || is_end_eq_array_len(cx, end, limits, indexed_ty) {
13041306
String::new()
1307+
} else if visitor.indexed_mut.contains(&indexed) && contains_name(indexed, take_expr) {
1308+
return;
13051309
} else {
13061310
match limits {
13071311
ast::RangeLimits::Closed => {

tests/ui/needless_range_loop2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ fn main() {
8282
for i in 1..3 {
8383
println!("{}", arr[i]);
8484
}
85+
86+
// Fix #5945
87+
let mut vec = vec![1, 2, 3, 4];
88+
for i in 0..vec.len() - 1 {
89+
vec[i] += 1;
90+
}
91+
let mut vec = vec![1, 2, 3, 4];
92+
for i in vec.len() - 3..vec.len() {
93+
vec[i] += 1;
94+
}
95+
let mut vec = vec![1, 2, 3, 4];
96+
for i in vec.len() - 3..vec.len() - 1 {
97+
vec[i] += 1;
98+
}
8599
}
86100

87101
mod issue2277 {

0 commit comments

Comments
 (0)