Skip to content

Suggestion of explicit_infinite_iterator for OneSidedRange #14913

@alpaylan

Description

@alpaylan

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions