Skip to content

Suggestion of explicit_infinite_iterator for OneSidedRange #14913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alpaylan opened this issue May 28, 2025 · 2 comments
Open

Suggestion of explicit_infinite_iterator for OneSidedRange #14913

alpaylan opened this issue May 28, 2025 · 2 comments
Labels
A-lint Area: New lints

Comments

@alpaylan
Copy link

What it does

For a given iterator using a one sided range (i..) where i: T it suggests changing it with (i..T::Max).cycle() in the interest of explicitness.

Advantage

  • Remove divergence between debug and release builds(right now, the following code fails with overflow in debug, but goes into infinite loop in release mode.
fn main() {
    for i in 0_u8.. {
        println!("{}", i);
    }
}
  • It prevents unintentional infinite loops, aiding infinite_iter and maybe_infinite_iter

Drawbacks

It suggests a change in behavior for the debug build.

Example

fn main() {
    for i in 0_u8.. {
        println!("{}", i);
    }
}

Could be written as:

fn main() {
    for i in (0_u8..u8::MAX).cycle() {
        println!("{}", i);
    }
}
@alpaylan alpaylan added the A-lint Area: New lints label May 28, 2025
@RGBCube
Copy link

RGBCube commented May 28, 2025

It suggests a change in behavior for the debug build.

Not just the debug build, as release mode will now actually terminate.

This would be a pretty neat "you probably didn't want to do that" lint

@kulkalkul
Copy link

It suggests a change in behavior for the debug build.

Not just the debug build, as release mode will now actually terminate.

This would be a pretty neat "you probably didn't want to do that" lint

Some extra: there is also a mention of "Range literal construction without an upper bound" in a Clippy issue: #9699. Existing maybe_infinite_iter isn't working as expected because it isn't implemented for loops. But I agree with @RGBCube, most basic case of 0.. should be its own lint on rustc lint with "you probably didn't want to do that" message & deny by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants