Skip to content

Wrong if_same_then_else lint error when having two different integer #11213

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

Closed
YouKnow-sys opened this issue Jul 23, 2023 · 2 comments · Fixed by #11214
Closed

Wrong if_same_then_else lint error when having two different integer #11213

YouKnow-sys opened this issue Jul 23, 2023 · 2 comments · Fixed by #11214
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

@YouKnow-sys
Copy link

Summary

if we call a trait method on two different integer type in a if else condition and specify the types like 0_u8, 0u16 and... this lint will give us a error about both if and else are the same, when they're not...

Lint Name

if_same_then_else

Reproducer

I tried this code:

let test = if true { 0_u16.is_power_of_two() } else { 0_u8.is_power_of_two() };

I saw this happen:

error: this `if` has identical blocks
  --> src.rs:44:28
   |
44 |         let test = if true { 0_u16.is_power_of_two() } else { 0_u8.is_power_of_two() };
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: same as this
  --> src.rs:44:61
   |
44 |         let test = if true { 0_u16.is_power_of_two() } else { 0_u8.is_power_of_two() };
   |                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                           

I didn't expected to see any lint error here because I'm calling this trait method on two different type.

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-pc-windows-msvc
release: 1.71.0
LLVM version: 16.0.5

Additional Labels

No response

@YouKnow-sys YouKnow-sys 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 Jul 23, 2023
@y21
Copy link
Member

y21 commented Jul 23, 2023

From a quick look, it seems like SpanlessEq's way of checking if two integer literal expressions are equal is wrong because it disregards the types.

if let Some((typeck_lhs, typeck_rhs)) = self.inner.maybe_typeck_results {
if let (Some(l), Some(r)) = (
constant_simple(self.inner.cx, typeck_lhs, left),
constant_simple(self.inner.cx, typeck_rhs, right),
) {
if l == r {
return true;
}
}
}

Looks like it folds 0_u8 and 0_u16 to the constant 0 and concludes that the expressions must be equal

@YouKnow-sys
Copy link
Author

YouKnow-sys commented Jul 23, 2023

From a quick look, it seems like SpanlessEq's way of checking if two integer literal expressions are equal is wrong because it disregards the types.

if let Some((typeck_lhs, typeck_rhs)) = self.inner.maybe_typeck_results {
if let (Some(l), Some(r)) = (
constant_simple(self.inner.cx, typeck_lhs, left),
constant_simple(self.inner.cx, typeck_rhs, right),
) {
if l == r {
return true;
}
}
}

Looks like it folds 0_u8 and 0_u16 to the constant 0 and concludes that the expressions must be equal

Well that only make sense if we are returning just a single integer literal (and even in that case returning different type from each arm is a error)
So by default it should check the type as well
Thanks for finding the problem

@bors bors closed this as completed in 5877504 Jul 23, 2023
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