Skip to content

int_plus_one changes program behavior in regards of overflows #9268

Description

@matthiaskrgr

Summary

int_plus_one suggestions prevent overflows from happening because it removes a potential case where we could run into T::max() + 1

Reproducer

I tried this code:

fn main() {
    dbg!(comp(254));
    dbg!(comp(255)); // overflow/panic

    dbg!(comp_clippyfixed(254));
    dbg!(comp_clippyfixed(255)); // no panic

}

fn comp(x: u8) -> bool {
    1 >= x + 1
}

fn comp_clippyfixed(x: u8) -> bool {
    1 > x 
}

Version

rustc 1.64.0-nightly (0f4bcadb4 2022-07-30)
binary: rustc
commit-hash: 0f4bcadb46006bc484dad85616b484f93879ca4e
commit-date: 2022-07-30
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions