Skip to content

mut_range_bound on immediately broken loop #7532

@SNCPlay42

Description

@SNCPlay42

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions