]> git.lizzy.rs Git - rust.git/blob - src/docs/mut_range_bound.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / mut_range_bound.txt
1 ### What it does
2 Checks for loops which have a range bound that is a mutable variable
3
4 ### Why is this bad?
5 One might think that modifying the mutable variable changes the loop bounds
6
7 ### Known problems
8 False positive when mutation is followed by a `break`, but the `break` is not immediately
9 after the mutation:
10
11 ```
12 let mut x = 5;
13 for _ in 0..x {
14     x += 1; // x is a range bound that is mutated
15     ..; // some other expression
16     break; // leaves the loop, so mutation is not an issue
17 }
18 ```
19
20 False positive on nested loops ([#6072](https://github.com/rust-lang/rust-clippy/issues/6072))
21
22 ### Example
23 ```
24 let mut foo = 42;
25 for i in 0..foo {
26     foo -= 1;
27     println!("{}", i); // prints numbers from 0 to 42, not 0 to 21
28 }
29 ```