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

cast_possible_truncation could leverage simple patterns #12781

Open
clarfonthey opened this issue May 8, 2024 · 0 comments
Open

cast_possible_truncation could leverage simple patterns #12781

clarfonthey opened this issue May 8, 2024 · 0 comments
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

@clarfonthey
Copy link
Contributor

clarfonthey commented May 8, 2024

Summary

When a cast is followed by a trivial check, either by a pattern or condition, we could avoid emitting this link since clippy can know that the cast will always succeed.

Lint Name

cast_possible_truncation

Reproducer

I tried this code: playground link

#![warn(clippy::cast_possible_truncation)]
use core::num::NonZeroU8;
struct Test<const N: usize>;
impl<const N: usize> Test<N> {
    const MY_NUM: NonZeroU8 = match N {
        0 => panic!("Test<0> is not allowed"),
        1..=255 => match NonZeroU8::new(N as u8) {
            Some(n) => n,
            None => unreachable!(),
        },
        _ => panic!("Test<N> where N >= 256 is not allowed"),
    };
}

I saw this happen:

warning: casting `usize` to `u8` may truncate the value
 --> src/lib.rs:7:41
  |
7 |         1..=255 => match NonZeroU8::new(N as u8) {
  |                                         ^^^^^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::cast_possible_truncation)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ... or use `try_from` and handle the error accordingly
  |
7 |         1..=255 => match NonZeroU8::new(u8::try_from(N)) {
  |                                         ~~~~~~~~~~~~~~~

Clippy should not emit this warning, since it should be able to understand from the trivial pattern 1..=255 that the cast will succeed. Note that because this occurs in a constant and TryFrom is unstable in constants, TryFrom cannot be used here.

Version

Nightly channel
Build using the Nightly version: 1.80.0-nightly
(2024-05-07 faefc618cf48bd794cbc)

Additional Labels

@rustbot label +I-false-positive

@clarfonthey clarfonthey 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 May 8, 2024
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

No branches or pull requests

1 participant