Skip to content

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

Closed
@ABouttefeux

Description

@ABouttefeux

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions