Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clippy_lints/src/loops/needless_range_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
Expand All @@ -181,6 +188,10 @@ pub(super) fn check<'tcx>(
vec![(pat.span, "<item>".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",
);
},
);
}
Expand Down
23 changes: 17 additions & 6 deletions tests/ui/needless_range_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 <item> in vec2.iter().take(vec.len()) {
LL + for <item> in vec2[..vec.len()].iter() {
|

error: the loop variable `i` is only used to index `vec`
Expand All @@ -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() {
Expand All @@ -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 <item> in vec.iter().take(MAX_LEN) {
LL + for <item> in vec[..MAX_LEN].iter() {
|

error: the loop variable `i` is only used to index `vec`
Expand All @@ -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 <item> in vec.iter().take(MAX_LEN + 1) {
LL + for <item> in vec[..MAX_LEN].iter() {
|

error: the loop variable `i` is only used to index `vec`
Expand All @@ -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 <item> in vec.iter().take(10).skip(5) {
LL + for <item> in vec[..10].iter().skip(5) {
|

error: the loop variable `i` is only used to index `vec`
Expand All @@ -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 <item> in vec.iter().take(10 + 1).skip(5) {
LL + for <item> in vec[..10].iter().skip(5) {
|

error: the loop variable `i` is used to index `vec`
Expand Down Expand Up @@ -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 <item> in a.iter().take(MAX_LEN) {
LL + for <item> in a[..MAX_LEN].iter() {
|

error: aborting due to 16 previous errors
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/needless_range_loop2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <item> in ns.iter().take(10).skip(3) {
LL + for <item> in ns[..10].iter().skip(3) {
|

error: the loop variable `i` is only used to index `ms`
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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 <item> in vec.iter_mut().skip(x).take(4) {
LL + for <item> in vec[..x + 4].iter_mut().take(4) {
|

error: the loop variable `i` is only used to index `vec`
Expand All @@ -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 <item> in vec.iter_mut().skip(x).take(4 + 1) {
LL + for <item> in vec[..x + 4].iter_mut().take(4 + 1) {
|

error: the loop variable `i` is only used to index `arr`
Expand All @@ -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 {
Expand All @@ -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 <item> in arr.iter().take(2) {
LL + for <item> in arr[..2].iter() {
|

error: the loop variable `i` is only used to index `arr`
Expand All @@ -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 {
Expand Down