Skip to content

If-same-then-else bug for unequal const generic argument #8680

Closed
@CurryPseudo

Description

@CurryPseudo

Summary

Unequal const generic argument trigger if-same-then-else lint.

Lint Name

If-same-then-else

Reproducer

Given

pub struct Foo<const I: i32> {}
impl<const I: i32> Foo<I> {
    pub const Y: i32 = I;
}

pub fn bar<const A: i32, const B: i32>() {
    let i = 0;
    let a = if i > 1 { Foo::<A>::Y } else { Foo::<B>::Y };
    println!("{}", a);
}

Got

    Checking playground v0.1.0 (/root/playground)
error: this `if` has identical blocks
 --> src/lib.rs:8:22
  |
8 |     let a = if i > 1 { Foo::<A>::Y } else { Foo::<B>::Y };
  |                      ^^^^^^^^^^^^^^^
  |
  = note: `#[deny(clippy::if_same_then_else)]` on by default
note: same as this
 --> src/lib.rs:8:43
  |
8 |     let a = if i > 1 { Foo::<A>::Y } else { Foo::<B>::Y };
  |                                           ^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else

error: could not compile `playground` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed

Version

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-unknown-linux-gnu
release: 1.60.0
LLVM version: 14.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    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