Why is this an issue?

Empty ranges result in no iterations, making any loop a no-op. Trying to index slices using reversed ranges will result in runtime panics. Both scenarios lead to bugs or logical errors in the code.

Code examples

Noncompliant code example

fn main() {
    (10..=0).for_each(|x| println!("{}", x)); // Noncompliant: Empty range
    let arr = [1, 2, 3, 4, 5];
    let sub = &arr[3..1]; // Noncompliant: Reversed slice indexing
}

Compliant solution

fn main() {
    (0..=10).rev().for_each(|x| println!("{}", x)); // Compliant: Properly reversed range for iteration
    let arr = [1, 2, 3, 4, 5];
    let sub = &arr[1..3]; // Compliant: Valid slice indexing
}

Resources

Documentation