diff --git a/clippy_lints/src/loops/needless_range_loop.rs b/clippy_lints/src/loops/needless_range_loop.rs index a0de464ff7f8..ad0eb4bee315 100644 --- a/clippy_lints/src/loops/needless_range_loop.rs +++ b/clippy_lints/src/loops/needless_range_loop.rs @@ -166,6 +166,13 @@ pub(super) fn check<'tcx>( } else { let repl = if starts_at_zero && take_is_empty { format!("&{ref_mut}{indexed}") + } else if !take_is_empty { + // Adding condition for when 'take' is not empty Fixing `.take(n)` with slicing to preserve panic + // semantics + format!( + "{indexed}[..{}].{method}(){method_2}", + snippet(cx, end.unwrap().span, "..") + ) } else { format!("{indexed}.{method}(){method_1}{method_2}") }; @@ -181,6 +188,10 @@ pub(super) fn check<'tcx>( vec![(pat.span, "".to_string()), (span, repl)], Applicability::HasPlaceholders, ); + diag.note( + "this suggestion preserves panic behavior, but the panic will occur \ + before iteration if the upper bound exceeds the collection length", + ); }, ); } diff --git a/tests/ui/needless_range_loop.stderr b/tests/ui/needless_range_loop.stderr index 33a519d8a80d..c00c85fccc15 100644 --- a/tests/ui/needless_range_loop.stderr +++ b/tests/ui/needless_range_loop.stderr @@ -4,6 +4,7 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 0..vec.len() { | ^^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length = note: `-D clippy::needless-range-loop` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_range_loop)]` help: consider using an iterator @@ -18,6 +19,7 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 0..vec.len() { | ^^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..vec.len() { @@ -30,6 +32,7 @@ error: the loop variable `j` is only used to index `STATIC` LL | for j in 0..4 { | ^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for j in 0..4 { @@ -42,6 +45,7 @@ error: the loop variable `j` is only used to index `CONST` LL | for j in 0..4 { | ^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for j in 0..4 { @@ -66,10 +70,11 @@ error: the loop variable `i` is only used to index `vec2` LL | for i in 0..vec.len() { | ^^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..vec.len() { -LL + for in vec2.iter().take(vec.len()) { +LL + for in vec2[..vec.len()].iter() { | error: the loop variable `i` is only used to index `vec` @@ -78,6 +83,7 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 5..vec.len() { | ^^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 5..vec.len() { @@ -90,10 +96,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 0..MAX_LEN { | ^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..MAX_LEN { -LL + for in vec.iter().take(MAX_LEN) { +LL + for in vec[..MAX_LEN].iter() { | error: the loop variable `i` is only used to index `vec` @@ -102,10 +109,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 0..=MAX_LEN { | ^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..=MAX_LEN { -LL + for in vec.iter().take(MAX_LEN + 1) { +LL + for in vec[..MAX_LEN].iter() { | error: the loop variable `i` is only used to index `vec` @@ -114,10 +122,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 5..10 { | ^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 5..10 { -LL + for in vec.iter().take(10).skip(5) { +LL + for in vec[..10].iter().skip(5) { | error: the loop variable `i` is only used to index `vec` @@ -126,10 +135,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in 5..=10 { | ^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 5..=10 { -LL + for in vec.iter().take(10 + 1).skip(5) { +LL + for in vec[..10].iter().skip(5) { | error: the loop variable `i` is used to index `vec` @@ -186,10 +196,11 @@ error: the loop variable `i` is only used to index `a` LL | for i in 0..MAX_LEN { | ^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..MAX_LEN { -LL + for in a.iter().take(MAX_LEN) { +LL + for in a[..MAX_LEN].iter() { | error: aborting due to 16 previous errors diff --git a/tests/ui/needless_range_loop2.stderr b/tests/ui/needless_range_loop2.stderr index cb979b3f3c24..cacc53c78c4b 100644 --- a/tests/ui/needless_range_loop2.stderr +++ b/tests/ui/needless_range_loop2.stderr @@ -4,12 +4,13 @@ error: the loop variable `i` is only used to index `ns` LL | for i in 3..10 { | ^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length = note: `-D clippy::needless-range-loop` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_range_loop)]` help: consider using an iterator | LL - for i in 3..10 { -LL + for in ns.iter().take(10).skip(3) { +LL + for in ns[..10].iter().skip(3) { | error: the loop variable `i` is only used to index `ms` @@ -18,6 +19,7 @@ error: the loop variable `i` is only used to index `ms` LL | for i in 0..ms.len() { | ^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..ms.len() { @@ -30,6 +32,7 @@ error: the loop variable `i` is only used to index `ms` LL | for i in 0..ms.len() { | ^^^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..ms.len() { @@ -42,10 +45,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in x..x + 4 { | ^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in x..x + 4 { -LL + for in vec.iter_mut().skip(x).take(4) { +LL + for in vec[..x + 4].iter_mut().take(4) { | error: the loop variable `i` is only used to index `vec` @@ -54,10 +58,11 @@ error: the loop variable `i` is only used to index `vec` LL | for i in x..=x + 4 { | ^^^^^^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in x..=x + 4 { -LL + for in vec.iter_mut().skip(x).take(4 + 1) { +LL + for in vec[..x + 4].iter_mut().take(4 + 1) { | error: the loop variable `i` is only used to index `arr` @@ -66,6 +71,7 @@ error: the loop variable `i` is only used to index `arr` LL | for i in 0..3 { | ^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..3 { @@ -78,10 +84,11 @@ error: the loop variable `i` is only used to index `arr` LL | for i in 0..2 { | ^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 0..2 { -LL + for in arr.iter().take(2) { +LL + for in arr[..2].iter() { | error: the loop variable `i` is only used to index `arr` @@ -90,6 +97,7 @@ error: the loop variable `i` is only used to index `arr` LL | for i in 1..3 { | ^^^^ | + = note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length help: consider using an iterator | LL - for i in 1..3 {