Skip to content

FP bool_to_int_with_if if let  #9897

Description

@matthiaskrgr

Summary

.

Lint Name

bool_to_int_with_if

Reproducer

I tried this code:

fn main() {}
const _: i32 = if let Some(true) = Some(false) { 0 } else { 1 };

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::bool_to_int_with_if

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected expression, found `let` statement
 --> src/main.rs:2:28
  |
2 | const _: i32 = i32::from(!(let Some(true) = Some(false)));
  |                            ^^^

error: `let` expressions are not supported here
 --> src/main.rs:2:28
  |
2 | const _: i32 = i32::from(!(let Some(true) = Some(false)));
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
 --> src/main.rs:2:28
  |
2 | const _: i32 = i32::from(!(let Some(true) = Some(false)));
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: `let` expressions in this position are unstable
 --> src/main.rs:2:28
  |
2 | const _: i32 = i32::from(!(let Some(true) = Some(false)));
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
  = help: add `#![feature(let_chains)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
Original diagnostics will follow.

warning: boolean to int conversion using if
 --> src/main.rs:2:16
  |
2 | const _: i32 = if let Some(true) = Some(false) { 0 } else { 1 };
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(!(let Some(true) = Some(false)))`
  |
  = note: `!(let Some(true) = Some(false)) as i32` or `(!(let Some(true) = Some(false))).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_if
  = note: requested on the command line with `-W clippy::bool-to-int-with-if`

warning: `clpy` (bin "clpy" test) generated 1 warning (run `cargo fix --bin "clpy" --tests` to apply 1 suggestion)
warning: `clpy` (bin "clpy") generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.35s

Version

rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions