Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

needless_range_loop suggests loop that stops earlier #9352

Open
Ryan1729 opened this issue Aug 20, 2022 · 0 comments
Open

needless_range_loop suggests loop that stops earlier #9352

Ryan1729 opened this issue Aug 20, 2022 · 0 comments
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

Comments

@Ryan1729
Copy link
Contributor

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]) where end happened to be the length of the slice. Note that the original code includes an iteration of the loop where i is equal to bytes.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 over bytes. Since end == bytes.len(), the take 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 with i having a value of end 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:

fn foo(bytes: &[u8], in_i: usize) -> bool {
    let len = bytes.len();

    for i in in_i + 1..=len {
        if i == len || bytes[i].is_ascii_whitespace() {
            return true
        }
    }

    false
}

I saw this happen:

warning: the loop variable `i` is used to index `bytes`
 --> src/main.rs:4:14
  |
4 |     for i in in_i + 1..=len {
  |              ^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::needless_range_loop)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
help: consider using an iterator
  |
4 |     for (i, <item>) in bytes.iter().enumerate().take(len + 1).skip(in_i + 1) {
  |         ~~~~~~~~~~~    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I expected to see this happen:
Either no lint emitted, or a suggestion that had identical observed behaviour.

Version

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5

I was also able to replicate this on the playground with clippy 0.1.65 (2022-08-16 86c6ebe)

Additional Labels

No response

@Ryan1729 Ryan1729 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

1 participant