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

mut_range_bound on immediately broken loop #7532

Closed
SNCPlay42 opened this issue Aug 4, 2021 · 2 comments · Fixed by #7607
Closed

mut_range_bound on immediately broken loop #7532

SNCPlay42 opened this issue Aug 4, 2021 · 2 comments · Fixed by #7607
Assignees
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

@SNCPlay42
Copy link

I tried this code:

fn bytes_needed_for_level(_level: u32) -> usize {
    unimplemented!()
}

pub fn load(data: &[u8], mut level_count: u32) {
    let mut bytes_needed = 0;
    for level in 0..level_count {
        bytes_needed += bytes_needed_for_level(level);
        if bytes_needed > data.len() {
            eprintln!(
                "not enough bytes for the specified number of levels;
                loading only the first {} levels",
                level_count
            );
            level_count = level;
            break;
        }
    }

    // use level_count while loading the data...
    println!("level_count is {}", level_count);
}

I expected this to happen: no diagnostic. level_count is mutated for when it is used after the loop, and the loop is immediately broken, so the lack of effect changing the bound has on the number of iterations of the loop is moot.

Instead, this happened: the following diagnostic is emitted:

warning: attempt to mutate range bound within loop; note that the range of the loop is unchanged
  --> src/lib.rs:15:13
   |
15 |             level_count = level;
   |             ^^^^^^^^^^^
   |
   = note: `#[warn(clippy::mut_range_bound)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound

warning: `playground` (lib) generated 1 warning

Meta

clippy 0.1.56 (2021-08-03 a6ece56) on playground

@SNCPlay42 SNCPlay42 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 4, 2021
@SNCPlay42
Copy link
Author

I'm unsure to what extent this and #6072 are the same thing. In this code, the break provides a clear hint that the mutation was not expected to change the behaviour of the loop.

@dswij
Copy link
Member

dswij commented Aug 28, 2021

@rustbot claim

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

Successfully merging a pull request may close this issue.

2 participants