Skip to content

Possible cast_possible_truncation false positive #8316

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

Closed
leonardo-m opened this issue Jan 20, 2022 · 2 comments
Closed

Possible cast_possible_truncation false positive #8316

leonardo-m opened this issue Jan 20, 2022 · 2 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

@leonardo-m
Copy link

Summary

This code:

#![warn(clippy::pedantic)]

fn main() {
    const X: usize = 100_000;
    const X2: u32 = X as _;
    dbg!(X);
    dbg!(X2);
}

Gives:

warning: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers
 --> src/main.rs:5:21
  |
5 |     const X2: u32 = X as _;
  |                     ^^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::pedantic)]
  |         ^^^^^^^^^^^^^^^^
  = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation

Perhaps this pedantic warning could be generated only if there's an actual truncation. Here 100_000 fits in 32 bits.

Lint Name

No response

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version

No response

Additional Labels

No response

@leonardo-m leonardo-m 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 Jan 20, 2022
@jyn514
Copy link
Member

jyn514 commented Dec 6, 2023

I think this is a duplicate of #9613.

@leonardo-m
Copy link
Author

Right.

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

2 participants