Skip to content

Warn on useless comparisons (like GCC's -Wtype-limits) #3833

Closed
@veddan

Description

@veddan

Consider this program:

fn main() {
        let x = ~[1, 2, 3, 4, 5];
        let mut i = x.len() - 1;
        while i >= 0 {
                if x[i] <= 3 {
                        io::println("At most three");
                }
                i -= 1;
        }
}

The behaviour one might expect here is for the program to print "At most three" three times and then exiting. The actual program output is:

At most three
At most three
At most three
rust: task failed at 'index out of bounds: the len is 5 but the index
is -1', uint.rs:6
rust: domain main @0xdd9c10 root task failed

The problem is that i is unsigned, so the comparison i >= 0 will always be true. With the addition of "-Wtype-limits" (GCC manual) this could easily be avoided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions