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

FP bool-to-int-with-if in const context #10452

Closed
matthiaskrgr opened this issue Mar 5, 2023 · 0 comments · Fixed by #10679
Closed

FP bool-to-int-with-if in const context #10452

matthiaskrgr opened this issue Mar 5, 2023 · 0 comments · Fixed by #10679
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

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

bool-to-int-with-if

Reproducer

I tried this code:

fn main() {
    let _ = [0; { 
         if false { 0 } else { 1 }
    }];
}

I saw this happen:

 --> feature-gate-const-if-match.rs:3:10
  |
3 |          if false { 0 } else { 1 }
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `usize::from(!false)`

The problem here is that the lint probably just checks "am I inside a const fn" but this is some which is false, BUT we are inside an array which still is some sort of const context and the lint misses this.
So the if false { 0 } else { 1 } is probably const-eval'd but this does not work with

fn main() {
    let _ = [0; { 
         usize::from(!false)
    }];
}
error[E0015]: cannot call non-const fn `<usize as std::convert::From<bool>>::from` in constants
 --> feature-gate-const-if-match.rs:3:10
  |
3 |          usize::from(!false)
  |          ^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants
  = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable

Version

rustc 1.69.0-nightly (44cfafe2f 2023-03-03)
binary: rustc
commit-hash: 44cfafe2fafe816395d3acc434663a45d5178c41
commit-date: 2023-03-03
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 5, 2023
@bors bors closed this as completed in 496c110 Apr 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 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.

1 participant