Closed
Description
Summary
When searching for instances of same_functions_in_if_condition
, Clippy appears to treat two expressions (namely the conditions inside of if/else chains) as if they were the same even if they differ by const parameter.
In the example given below, Clippy wrongly assumes that foo::<{ T::A }>(x) == 0
and foo::<{ T::B }>(x) == 0
mean the same. Obviously, T::A
≠ T::B
but also foo::<{ T::A }>(x)
≠ foo::<{ T::B }>(x)
in general.
Lint Name
same_functions_in_if_condition
Reproducer
Of course, the reproducer is but a heavily boiled-down version of real code.
I tried this code (playground):
#![feature(adt_const_params)]
#![allow(incomplete_features)]
#![warn(clippy::same_functions_in_if_condition)]
fn main() {
let x = 1; // the program prints "B"
// let x = -1; // the program prints "A" if this line is uncommented
// therefore, the branches are not only
// *syntactically* different but also
// *semantically* different
if foo::<{ T::A }>(x) == 0 {
println!("A");
} else if foo::<{ T::B }>(x) == 0 {
println!("B");
}
}
#[derive(PartialEq, Eq)]
enum T {
A,
B,
}
fn foo<const P: T>(x: i32) -> i32 {
match P {
T::A => x + 1,
T::B => x - 1,
}
}
I saw this happen:
warning: this `if` has the same function call as a previous `if`
--> src/main.rs:14:15
|
14 | } else if foo::<{ T::B }>(x) == 0 {
| ^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> src/main.rs:3:9
|
3 | #![warn(clippy::same_functions_in_if_condition)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: same as this
--> src/main.rs:12:8
|
12 | if foo::<{ T::A }>(x) == 0 {
| ^^^^^^^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#same_functions_in_if_condition
I expected to see this happen:
No warning being printed to stderr.
Version
rustc 1.59.0-nightly (48a5999fc 2021-12-01)
binary: rustc
commit-hash: 48a5999fceeea84a8971634355287faa349909d4
commit-date: 2021-12-01
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
Additional Labels
@rustbot label +T-const-generics