]> git.lizzy.rs Git - rust.git/blob - src/docs/range_plus_one.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / range_plus_one.txt
1 ### What it does
2 Checks for exclusive ranges where 1 is added to the
3 upper bound, e.g., `x..(y+1)`.
4
5 ### Why is this bad?
6 The code is more readable with an inclusive range
7 like `x..=y`.
8
9 ### Known problems
10 Will add unnecessary pair of parentheses when the
11 expression is not wrapped in a pair but starts with an opening parenthesis
12 and ends with a closing one.
13 I.e., `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`.
14
15 Also in many cases, inclusive ranges are still slower to run than
16 exclusive ranges, because they essentially add an extra branch that
17 LLVM may fail to hoist out of the loop.
18
19 This will cause a warning that cannot be fixed if the consumer of the
20 range only accepts a specific range type, instead of the generic
21 `RangeBounds` trait
22 ([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)).
23
24 ### Example
25 ```
26 for i in x..(y+1) {
27     // ..
28 }
29 ```
30
31 Use instead:
32 ```
33 for i in x..=y {
34     // ..
35 }
36 ```