rust-lang / rust-lang/rust-clippy

int_plus_one changes program behavior in regards of overflows

Open
#9,268 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the int_plus_one lint and reproduce the behavior using the supplied comp and comp_clippyfixed examples with u8 values 254 and 255. Investigate how the suggestion changes overflow behavior; done means the lint no longer changes the program's overflow behavior and the regression is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.