RS.CLIPPY.RANGE_PLUS_ONE
`x..(y+1)` reads better as `x..=y`
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: range_plus_one. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for exclusive ranges where 1 is added to the
upper bound, e.g., x..(y+1).
Why is this bad?
The code is more readable with an inclusive range
like x..=y.
Known problems
Will add unnecessary pair of parentheses when the
expression is not wrapped in a pair but starts with an opening parenthesis
and ends with a closing one.
I.e., let _ = (f()+1)..(f()+1) results in let _ = ((f()+1)..=f()).
Also in many cases, inclusive ranges are still slower to run than exclusive ranges, because they essentially add an extra branch that LLVM may fail to hoist out of the loop.
This will cause a warning that cannot be fixed if the consumer of the
range only accepts a specific range type, instead of the generic
RangeBounds trait
(#3307).
Example
for i in x..(y+1) {
// ..
}
Use instead:
for i in x..=y {
// ..
}