Skip to content
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

bool_to_int_with_if triggers in const contexts #9737

Closed
madsmtm opened this issue Oct 28, 2022 · 1 comment · Fixed by #9738
Closed

bool_to_int_with_if triggers in const contexts #9737

madsmtm opened this issue Oct 28, 2022 · 1 comment · Fixed by #9738
Assignees
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@madsmtm
Copy link

madsmtm commented Oct 28, 2022

Summary

The From/Into traits are not allowed in const contexts, so the lint triggering in those situations is undesirable.

Lint Name

bool_to_int_with_if

Reproducer

I tried this code:

pub const FOO: usize = if true {
    1
} else {
    0
};

I saw this happen:

warning: boolean to int conversion using if
 --> src/lib.rs:1:24
  |
1 |   pub const FOO: usize = if true {
  |  ________________________^
2 | |     1
3 | | } else {
4 | |     0
5 | | };
  | |_^ help: replace with from: `usize::from(true)`
  |
  = note: `true as usize` or `true.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: `#[warn(clippy::bool_to_int_with_if)]` on by default

I expected to see this happen:
The lint shouldn't trigger.

Version

rustc 1.66.0-nightly (1898c34e9 2022-10-26)
binary: rustc
commit-hash: 1898c34e923bad763e723c68dd9f23a09f9eb0fc
commit-date: 2022-10-26
host: x86_64-apple-darwin
release: 1.66.0-nightly
LLVM version: 15.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

@madsmtm madsmtm 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 Oct 28, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 28, 2022
@kraktus
Copy link
Contributor

kraktus commented Oct 28, 2022

@rustbot claim

bors added a commit that referenced this issue Oct 29, 2022
[`bool_to_int_with_if`] do not lint in const context

changelog: [`bool_to_int_with_if`] do not lint in const context

fix #9737
@bors bors closed this as completed in 8e19251 Oct 29, 2022
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants