Range with literal bounds is empty because it starts at or past its end

ID

rust.correctness.reversed_empty_ranges

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Suspicious Construct

Language

Rust

Description

Detects range expressions whose bounds are both numeric literals and whose start is at or beyond the end — 5..1, 3..=1, 0..0 — so the range yields no values at all.

Rationale

Rust ranges do not count backwards, so a reversed range is silently empty: the loop body never runs and the slice comes back with zero elements. Nothing fails at the point of the mistake, so the missing work is usually diagnosed far away from its cause.

The following code illustrates the pattern detected by this rule:

pub fn countdown() {
    // FLAGGED: Range with literal bounds is empty because it starts at or past its end
    for i in 5..1 {
        println!("{}", i);
    }
    // FLAGGED: Range with literal bounds is empty because it starts at or past its end
    for i in 3..=1 {
        println!("{}", i);
    }
}

Remediation

Put the smaller bound first. To walk the values in descending order call .rev() on the forward range ((1..5).rev()), and where the bounds are computed, order them explicitly with min and max before building the range.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved