needless_range_loop suggests loop that stops earlier #9352
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
Summary
I ran clippy on some code that included a loop over a range of the form
for i in start ..= end
, which indexed into a slice of bytes, (bytes[i]
) whereend
happened to be the length of the slice. Note that the original code includes an iteration of the loop wherei
is equal tobytes.len()
, but that there was an additional condition checking for that case, preventing the out of bounds indexing.In that case, clippy suggested an iterator expression that included calling
.take(end + 1)
on an iterator overbytes
. Sinceend == bytes.len()
, thetake
call does not actually do anything meaningful, since the iterator will run out before then. More worrying though is the fact that the previous behaviour of running the loop an additional time withi
having a value ofend
was removed if the suggestion was used.The example below has been reduced from the actual code, to remove irrelevant details.
Playground link
See related, but non-identical #6930
Lint Name
needless_range_loop
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
Either no lint emitted, or a suggestion that had identical observed behaviour.
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: