Skip to content

Commit 716214d

Browse files
committed
fix: preserve panic behavior in needless_range_loop lint; update .stderr tests
1 parent d06ae84 commit 716214d

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ pub(super) fn check<'tcx>(
166166
} else {
167167
let repl = if starts_at_zero && take_is_empty {
168168
format!("&{ref_mut}{indexed}")
169+
} else if !take_is_empty {
170+
// Adding condition for when 'take' is not empty Fixing `.take(n)` with slicing to preserve panic semantics
171+
format!(
172+
"{indexed}[..{}].{method}(){method_2}",
173+
snippet(cx, end.unwrap().span, "..")
174+
)
169175
} else {
170176
format!("{indexed}.{method}(){method_1}{method_2}")
171177
};
@@ -181,6 +187,10 @@ pub(super) fn check<'tcx>(
181187
vec![(pat.span, "<item>".to_string()), (span, repl)],
182188
Applicability::HasPlaceholders,
183189
);
190+
diag.note(
191+
"this suggestion preserves panic behavior, but the panic will occur \
192+
before iteration if the upper bound exceeds the collection length",
193+
);
184194
},
185195
);
186196
}

tests/ui/needless_range_loop.stderr

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: the loop variable `i` is only used to index `vec`
44
LL | for i in 0..vec.len() {
55
| ^^^^^^^^^^^^
66
|
7+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
78
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
89
= help: to override `-D warnings` add `#[allow(clippy::needless_range_loop)]`
910
help: consider using an iterator
@@ -18,6 +19,7 @@ error: the loop variable `i` is only used to index `vec`
1819
LL | for i in 0..vec.len() {
1920
| ^^^^^^^^^^^^
2021
|
22+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
2123
help: consider using an iterator
2224
|
2325
LL - for i in 0..vec.len() {
@@ -30,6 +32,7 @@ error: the loop variable `j` is only used to index `STATIC`
3032
LL | for j in 0..4 {
3133
| ^^^^
3234
|
35+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
3336
help: consider using an iterator
3437
|
3538
LL - for j in 0..4 {
@@ -42,6 +45,7 @@ error: the loop variable `j` is only used to index `CONST`
4245
LL | for j in 0..4 {
4346
| ^^^^
4447
|
48+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
4549
help: consider using an iterator
4650
|
4751
LL - for j in 0..4 {
@@ -66,10 +70,11 @@ error: the loop variable `i` is only used to index `vec2`
6670
LL | for i in 0..vec.len() {
6771
| ^^^^^^^^^^^^
6872
|
73+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
6974
help: consider using an iterator
7075
|
7176
LL - for i in 0..vec.len() {
72-
LL + for <item> in vec2.iter().take(vec.len()) {
77+
LL + for <item> in vec2[..vec.len()].iter() {
7378
|
7479

7580
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`
7883
LL | for i in 5..vec.len() {
7984
| ^^^^^^^^^^^^
8085
|
86+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
8187
help: consider using an iterator
8288
|
8389
LL - for i in 5..vec.len() {
@@ -90,10 +96,11 @@ error: the loop variable `i` is only used to index `vec`
9096
LL | for i in 0..MAX_LEN {
9197
| ^^^^^^^^^^
9298
|
99+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
93100
help: consider using an iterator
94101
|
95102
LL - for i in 0..MAX_LEN {
96-
LL + for <item> in vec.iter().take(MAX_LEN) {
103+
LL + for <item> in vec[..MAX_LEN].iter() {
97104
|
98105

99106
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`
102109
LL | for i in 0..=MAX_LEN {
103110
| ^^^^^^^^^^^
104111
|
112+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
105113
help: consider using an iterator
106114
|
107115
LL - for i in 0..=MAX_LEN {
108-
LL + for <item> in vec.iter().take(MAX_LEN + 1) {
116+
LL + for <item> in vec[..MAX_LEN].iter() {
109117
|
110118

111119
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`
114122
LL | for i in 5..10 {
115123
| ^^^^^
116124
|
125+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
117126
help: consider using an iterator
118127
|
119128
LL - for i in 5..10 {
120-
LL + for <item> in vec.iter().take(10).skip(5) {
129+
LL + for <item> in vec[..10].iter().skip(5) {
121130
|
122131

123132
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`
126135
LL | for i in 5..=10 {
127136
| ^^^^^^
128137
|
138+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
129139
help: consider using an iterator
130140
|
131141
LL - for i in 5..=10 {
132-
LL + for <item> in vec.iter().take(10 + 1).skip(5) {
142+
LL + for <item> in vec[..10].iter().skip(5) {
133143
|
134144

135145
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`
186196
LL | for i in 0..MAX_LEN {
187197
| ^^^^^^^^^^
188198
|
199+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
189200
help: consider using an iterator
190201
|
191202
LL - for i in 0..MAX_LEN {
192-
LL + for <item> in a.iter().take(MAX_LEN) {
203+
LL + for <item> in a[..MAX_LEN].iter() {
193204
|
194205

195206
error: aborting due to 16 previous errors

tests/ui/needless_range_loop2.stderr

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ error: the loop variable `i` is only used to index `ns`
44
LL | for i in 3..10 {
55
| ^^^^^
66
|
7+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
78
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
89
= help: to override `-D warnings` add `#[allow(clippy::needless_range_loop)]`
910
help: consider using an iterator
1011
|
1112
LL - for i in 3..10 {
12-
LL + for <item> in ns.iter().take(10).skip(3) {
13+
LL + for <item> in ns[..10].iter().skip(3) {
1314
|
1415

1516
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`
1819
LL | for i in 0..ms.len() {
1920
| ^^^^^^^^^^^
2021
|
22+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
2123
help: consider using an iterator
2224
|
2325
LL - for i in 0..ms.len() {
@@ -30,6 +32,7 @@ error: the loop variable `i` is only used to index `ms`
3032
LL | for i in 0..ms.len() {
3133
| ^^^^^^^^^^^
3234
|
35+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
3336
help: consider using an iterator
3437
|
3538
LL - for i in 0..ms.len() {
@@ -42,10 +45,11 @@ error: the loop variable `i` is only used to index `vec`
4245
LL | for i in x..x + 4 {
4346
| ^^^^^^^^
4447
|
48+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
4549
help: consider using an iterator
4650
|
4751
LL - for i in x..x + 4 {
48-
LL + for <item> in vec.iter_mut().skip(x).take(4) {
52+
LL + for <item> in vec[..x + 4].iter_mut().take(4) {
4953
|
5054

5155
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`
5458
LL | for i in x..=x + 4 {
5559
| ^^^^^^^^^
5660
|
61+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
5762
help: consider using an iterator
5863
|
5964
LL - for i in x..=x + 4 {
60-
LL + for <item> in vec.iter_mut().skip(x).take(4 + 1) {
65+
LL + for <item> in vec[..x + 4].iter_mut().take(4 + 1) {
6166
|
6267

6368
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`
6671
LL | for i in 0..3 {
6772
| ^^^^
6873
|
74+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
6975
help: consider using an iterator
7076
|
7177
LL - for i in 0..3 {
@@ -78,10 +84,11 @@ error: the loop variable `i` is only used to index `arr`
7884
LL | for i in 0..2 {
7985
| ^^^^
8086
|
87+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
8188
help: consider using an iterator
8289
|
8390
LL - for i in 0..2 {
84-
LL + for <item> in arr.iter().take(2) {
91+
LL + for <item> in arr[..2].iter() {
8592
|
8693

8794
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`
9097
LL | for i in 1..3 {
9198
| ^^^^
9299
|
100+
= note: this suggestion preserves panic behavior, but the panic will occur before iteration if the upper bound exceeds the collection length
93101
help: consider using an iterator
94102
|
95103
LL - for i in 1..3 {

0 commit comments

Comments
 (0)