RS.CLIPPY.REVERSED_EMPTY_RANGES
Reversing the limits of range expressions, resulting in empty ranges
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: reversed_empty_ranges. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for range expressions x..y where both x and y
are constant and x is greater to y. Also triggers if x is equal to y when they are conditions to a for loop.
Why is this bad?
Empty ranges yield no values so iterating them is a no-op. Moreover, trying to use a reversed range to index a slice will panic at run-time.
Example
fn main() {
(10..=0).for_each(|x| println!("{}", x));
let arr = [1, 2, 3, 4, 5];
let sub = &arr[3..1];
}
Use instead:
fn main() {
(0..=10).rev().for_each(|x| println!("{}", x));
let arr = [1, 2, 3, 4, 5];
let sub = &arr[1..3];
}
Past names
- reverse_range_loop