Skip to content

Commit 66d3672

Browse files
committed
out_of_bounds_indexing improved reporting of out of bounds value
1 parent 5c39282 commit 66d3672

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
120120
utils::span_lint(
121121
cx,
122122
OUT_OF_BOUNDS_INDEXING,
123-
expr.span,
123+
range.start.map_or(expr.span, |start| start.span),
124124
"range is out of bounds",
125125
);
126126
return;
@@ -132,7 +132,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
132132
utils::span_lint(
133133
cx,
134134
OUT_OF_BOUNDS_INDEXING,
135-
expr.span,
135+
range.end.map_or(expr.span, |end| end.span),
136136
"range is out of bounds",
137137
);
138138
return;

tests/ui/indexing_slicing.stderr

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ error: slicing may panic.
4848
= help: Consider using `.get(n..)` or .get_mut(n..)` instead
4949

5050
error: range is out of bounds
51-
--> $DIR/indexing_slicing.rs:30:6
51+
--> $DIR/indexing_slicing.rs:30:11
5252
|
5353
30 | &x[..=4];
54-
| ^^^^^^^
54+
| ^
5555
|
5656
= note: `-D clippy::out-of-bounds-indexing` implied by `-D warnings`
5757

5858
error: range is out of bounds
59-
--> $DIR/indexing_slicing.rs:31:6
59+
--> $DIR/indexing_slicing.rs:31:11
6060
|
6161
31 | &x[1..5];
62-
| ^^^^^^^
62+
| ^
6363

6464
error: slicing may panic.
6565
--> $DIR/indexing_slicing.rs:32:6
@@ -70,34 +70,34 @@ error: slicing may panic.
7070
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
7171

7272
error: range is out of bounds
73-
--> $DIR/indexing_slicing.rs:32:6
73+
--> $DIR/indexing_slicing.rs:32:8
7474
|
7575
32 | &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
76-
| ^^^^^^
76+
| ^
7777

7878
error: range is out of bounds
79-
--> $DIR/indexing_slicing.rs:33:6
79+
--> $DIR/indexing_slicing.rs:33:8
8080
|
8181
33 | &x[5..];
82-
| ^^^^^^
82+
| ^
8383

8484
error: range is out of bounds
85-
--> $DIR/indexing_slicing.rs:34:6
85+
--> $DIR/indexing_slicing.rs:34:10
8686
|
8787
34 | &x[..5];
88-
| ^^^^^^
88+
| ^
8989

9090
error: range is out of bounds
91-
--> $DIR/indexing_slicing.rs:35:6
91+
--> $DIR/indexing_slicing.rs:35:8
9292
|
9393
35 | &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
94-
| ^^^^^^
94+
| ^
9595

9696
error: range is out of bounds
97-
--> $DIR/indexing_slicing.rs:36:6
97+
--> $DIR/indexing_slicing.rs:36:12
9898
|
9999
36 | &x[0..=4];
100-
| ^^^^^^^^
100+
| ^
101101

102102
error: slicing may panic.
103103
--> $DIR/indexing_slicing.rs:37:6
@@ -148,46 +148,46 @@ error: slicing may panic.
148148
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
149149

150150
error: range is out of bounds
151-
--> $DIR/indexing_slicing.rs:60:6
151+
--> $DIR/indexing_slicing.rs:60:12
152152
|
153153
60 | &empty[1..5];
154-
| ^^^^^^^^^^^
154+
| ^
155155

156156
error: range is out of bounds
157-
--> $DIR/indexing_slicing.rs:61:6
157+
--> $DIR/indexing_slicing.rs:61:16
158158
|
159159
61 | &empty[0..=4];
160-
| ^^^^^^^^^^^^
160+
| ^
161161

162162
error: range is out of bounds
163-
--> $DIR/indexing_slicing.rs:62:6
163+
--> $DIR/indexing_slicing.rs:62:15
164164
|
165165
62 | &empty[..=4];
166-
| ^^^^^^^^^^^
166+
| ^
167167

168168
error: range is out of bounds
169-
--> $DIR/indexing_slicing.rs:63:6
169+
--> $DIR/indexing_slicing.rs:63:12
170170
|
171171
63 | &empty[1..];
172-
| ^^^^^^^^^^
172+
| ^
173173

174174
error: range is out of bounds
175-
--> $DIR/indexing_slicing.rs:64:6
175+
--> $DIR/indexing_slicing.rs:64:14
176176
|
177177
64 | &empty[..4];
178-
| ^^^^^^^^^^
178+
| ^
179179

180180
error: range is out of bounds
181-
--> $DIR/indexing_slicing.rs:65:6
181+
--> $DIR/indexing_slicing.rs:65:16
182182
|
183183
65 | &empty[0..=0];
184-
| ^^^^^^^^^^^^
184+
| ^
185185

186186
error: range is out of bounds
187-
--> $DIR/indexing_slicing.rs:66:6
187+
--> $DIR/indexing_slicing.rs:66:15
188188
|
189189
66 | &empty[..=0];
190-
| ^^^^^^^^^^^
190+
| ^
191191

192192
error: indexing may panic.
193193
--> $DIR/indexing_slicing.rs:74:5
@@ -230,10 +230,10 @@ error: slicing may panic.
230230
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
231231

232232
error: range is out of bounds
233-
--> $DIR/indexing_slicing.rs:78:6
233+
--> $DIR/indexing_slicing.rs:78:8
234234
|
235235
78 | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
236-
| ^^^^^^^
236+
| ^^
237237

238238
error: slicing may panic.
239239
--> $DIR/indexing_slicing.rs:79:6
@@ -268,16 +268,16 @@ error: indexing may panic.
268268
= help: Consider using `.get(n)` or `.get_mut(n)` instead
269269

270270
error: range is out of bounds
271-
--> $DIR/indexing_slicing.rs:97:6
271+
--> $DIR/indexing_slicing.rs:97:13
272272
|
273273
97 | &x[num..10]; // should trigger out of bounds error
274-
| ^^^^^^^^^^
274+
| ^^
275275

276276
error: range is out of bounds
277-
--> $DIR/indexing_slicing.rs:98:6
277+
--> $DIR/indexing_slicing.rs:98:8
278278
|
279279
98 | &x[10..num]; // should trigger out of bounds error
280-
| ^^^^^^^^^^
280+
| ^^
281281

282282
error: aborting due to 39 previous errors
283283

0 commit comments

Comments
 (0)