Skip to content

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

Closed
@ABouttefeux

Description

@ABouttefeux
Contributor

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

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Apr 15, 2021
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[/+] on Apr 15, 2021
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[/+] on Apr 15, 2021
TaKO8Ki

TaKO8Ki commented on Apr 16, 2021

@TaKO8Ki
Member

@rustbot claim

added a commit that references this issue on Apr 30, 2021

Auto merge of #7118 - TaKO8Ki:fix-false-positive-in-comparison-chain,…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @TaKO8Ki@ABouttefeux

    Issue actions

      false positive: comparison_chain lint in const function even though cmp cannot be used · Issue #7082 · rust-lang/rust-clippy