@@ -168,6 +168,7 @@ enum IndexEntry<'hir> {
168
168
/// if the `assert!` asserts the right length.
169
169
AssertWithIndex {
170
170
highest_index : usize ,
171
+ is_first_highest : bool ,
171
172
asserted_len : usize ,
172
173
assert_span : Span ,
173
174
slice : & ' hir Expr < ' hir > ,
@@ -177,6 +178,7 @@ enum IndexEntry<'hir> {
177
178
/// Indexing without an `assert!`
178
179
IndexWithoutAssert {
179
180
highest_index : usize ,
181
+ is_first_highest : bool ,
180
182
indexes : Vec < Span > ,
181
183
slice : & ' hir Expr < ' hir > ,
182
184
} ,
@@ -247,6 +249,7 @@ fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Uni
247
249
if slice. span . lo ( ) > assert_span. lo ( ) {
248
250
* entry = IndexEntry :: AssertWithIndex {
249
251
highest_index : index,
252
+ is_first_highest : true ,
250
253
asserted_len : * asserted_len,
251
254
assert_span : * assert_span,
252
255
slice,
@@ -256,18 +259,28 @@ fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Uni
256
259
}
257
260
} ,
258
261
IndexEntry :: IndexWithoutAssert {
259
- highest_index, indexes, ..
262
+ highest_index,
263
+ indexes,
264
+ is_first_highest,
265
+ ..
260
266
}
261
267
| IndexEntry :: AssertWithIndex {
262
- highest_index, indexes, ..
268
+ highest_index,
269
+ indexes,
270
+ is_first_highest,
271
+ ..
263
272
} => {
264
273
indexes. push ( expr. span ) ;
274
+ if * is_first_highest {
275
+ ( * is_first_highest) = * highest_index >= index;
276
+ }
265
277
* highest_index = ( * highest_index) . max ( index) ;
266
278
} ,
267
279
}
268
280
} else {
269
281
indexes. push ( IndexEntry :: IndexWithoutAssert {
270
282
highest_index : index,
283
+ is_first_highest : true ,
271
284
indexes : vec ! [ expr. span] ,
272
285
slice,
273
286
} ) ;
@@ -286,6 +299,7 @@ fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Un
286
299
if let Some ( entry) = entry {
287
300
if let IndexEntry :: IndexWithoutAssert {
288
301
highest_index,
302
+ is_first_highest,
289
303
indexes,
290
304
slice,
291
305
} = entry
@@ -294,6 +308,7 @@ fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Un
294
308
* entry = IndexEntry :: AssertWithIndex {
295
309
highest_index : * highest_index,
296
310
indexes : mem:: take ( indexes) ,
311
+ is_first_highest : * is_first_highest,
297
312
slice,
298
313
assert_span : expr. span ,
299
314
comparison,
@@ -328,12 +343,13 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>
328
343
match * entry {
329
344
IndexEntry :: AssertWithIndex {
330
345
highest_index,
346
+ is_first_highest,
331
347
asserted_len,
332
348
ref indexes,
333
349
comparison,
334
350
assert_span,
335
351
slice,
336
- } if indexes. len ( ) > 1 => {
352
+ } if indexes. len ( ) > 1 && !is_first_highest => {
337
353
// if we have found an `assert!`, let's also check that it's actually right
338
354
// and if it covers the highest index and if not, suggest the correct length
339
355
let sugg = match comparison {
@@ -381,8 +397,9 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>
381
397
IndexEntry :: IndexWithoutAssert {
382
398
ref indexes,
383
399
highest_index,
400
+ is_first_highest,
384
401
slice,
385
- } if indexes. len ( ) > 1 => {
402
+ } if indexes. len ( ) > 1 && !is_first_highest => {
386
403
// if there was no `assert!` but more than one index, suggest
387
404
// adding an `assert!` that covers the highest index
388
405
report_lint (
0 commit comments