Skip to content
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

false positive: comparison_chain lint in const function even though cmp cannot be used #7082

Closed
ABouttefeux opened this issue Apr 15, 2021 · 1 comment · Fixed by #7118
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@ABouttefeux
Copy link
Contributor

ABouttefeux commented Apr 15, 2021

Lint name: comparison_chain

I tried this code:

pub enum Sign {
    Negative,
    Positive,
    Zero
}

impl Sign {
    pub const fn sign_i8(n: i8) -> Self {
        if n == 0 {
            Sign::Zero
        }
        else if n > 0 {
            Sign::Positive
        }
        else {
            Sign::Negative
        }
    }
}

I expected to see this happen: no warning: cmp method cannot be used in const function

Instead, this happened:

warning: `if` chain can be rewritten with `match`
   --> $DIR
    |
175 | /         if n == 0 {
176 | |             Sign::Zero
177 | |         }
178 | |         else if n > 0 {
...   |
182 | |             Sign::Negative
183 | |         }
    | |_________^
    |
    = note: `#[warn(clippy::comparison_chain)]` on by default
    = help: Consider rewriting the `if` chain to use `cmp` and `match`.
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain

Meta

  • cargo clippy -V: clippy 0.1.51 (2fd73fab 2021-03-23)
  • rustc -Vv:
rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-pc-windows-msvc
release: 1.51.0
LLVM version: 11.0.1
@ABouttefeux ABouttefeux added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 15, 2021
@ABouttefeux ABouttefeux changed the title comparison_chain lint in const function even though cmp cannot be used false negative: comparison_chain lint in const function even though cmp cannot be used Apr 15, 2021
@ABouttefeux ABouttefeux changed the title false negative: comparison_chain lint in const function even though cmp cannot be used false positive: comparison_chain lint in const function even though cmp cannot be used Apr 15, 2021
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Apr 16, 2021

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants