-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Description
Not sure if this is a bug in one of the lints or in their interplay or normal behavior but I found it strange that it requires multiple runs of cargo clippy --fix
, does not perform it's own suggestion and then outputs an error.
Applying cargo clippy
on an unoptimized bool to integer conversion version 1...
#![warn(clippy::pedantic)]
fn main() {
let _ = |b| match b { true => 1, false => 0};
}
Warns about:
warning: `match` on a boolean expression
--> src/main.rs:3:17
|
3 | let _ = |b| match b { true => 1, false => 0};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using an `if`/`else` expression: `if b { 1 } else { 0 }`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
Applying cargo clippy --fix
leads to this (only showing the relevant line from now on):
let _ = |b| if b { 1 } else { 0 };
However this still causes warnings with cargo clippy
:
warning: boolean to int conversion using if
--> src/main.rs:3:17
|
3 | let _ = |b| if b { 1 } else { 0 };
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(b)`
|
= note: `b as i32` or `b.into()` can also be valid options
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_i
So lets cargo clippy --fix again
, but somehow it does not apply it's own suggestion to use i32::from(b), instead we get an error:
error[E0283]: type annotations needed
--> src/main.rs:3:14
|
3 | let _ = |b| i32::from(b);
| ^ --- type must be known at this point
|
= note: multiple `impl`s satisfying `i32: std::convert::From<_>` found in the `core` crate:
- impl std::convert::From<bool> for i32;
- impl std::convert::From<i16> for i32;
- impl std::convert::From<i8> for i32;
- impl std::convert::From<u16> for i32;
- impl std::convert::From<u8> for i32;
help: consider giving this closure parameter an explicit type
|
3 | let _ = |b: /* Type */| i32::from(b);
| ++++++++++++
error: aborting due to 1 previous error
OK so we manually change it to let _ : fn(bool) -> u8 = |b| if b { 1 } else { 0 };
and cargo clippy --fix
again and finally arrive at a stable solution where clippy does not give any warnings anymore:
let _ : fn(bool) -> u8 = |b| u8::from(b);
Version
rustc 1.90.0-nightly (a84ab0ce6 2025-07-06)
binary: rustc
commit-hash: a84ab0ce6c4557a2f01a3a6c3fdb0f92098db78d
commit-date: 2025-07-06
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7
Additional Labels
No response