Skip to content

cast_possible_truncation false positive: const defined in the same crate #9613

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

Open
dtolnay opened this issue Oct 8, 2022 · 2 comments
Open
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

@dtolnay
Copy link
Member

dtolnay commented Oct 8, 2022

Summary

I believe cast_possible_truncation should not trigger when using as to cast a constant defined in the same crate, and clippy can tell for sure that the cast will not truncate. It's a false positive to indicate that the cast may truncate.

If the programmer later changes the value of the const such that truncation does occur (this would be an extremely unusual scenario) then the developer will be alerted about the old code at that point by clippy, so there is no risk.

Lint Name

cast_possible_truncation

Reproducer

const CHUNK: usize = 64;

fn main() {
    // CHUNK is almost always used as usize
    let _empty_chunk = [0u8; CHUNK];

    // In a few places it is used as u32
    for i in 0..(u32::from(char::MAX) + 1) / CHUNK as u32 {
        for j in 0..CHUNK as u32 {
            let _ch = i * CHUNK as u32 + j;
            // ...
        }
    }
}
$ cargo clippy -- -Dclippy::cast_possible_truncation
error: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers
 --> src/main.rs:8:46
  |
8 |     for i in 0..(u32::from(char::MAX) + 1) / CHUNK as u32 {
  |                                              ^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
  = note: requested on the command line with `-D clippy::cast-possible-truncation`

error: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers
 --> src/main.rs:9:21
  |
9 |         for j in 0..CHUNK as u32 {
  |                     ^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation

error: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers
  --> src/main.rs:10:27
   |
10 |             let _ch = i * CHUNK as u32 + j;
   |                           ^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation

Version

rustc 1.66.0-nightly (8b0c05d9a 2022-10-07)
binary: rustc
commit-hash: 8b0c05d9ad7121cdb97600f261bcd5f04c8db20d
commit-date: 2022-10-07
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2

Additional Labels

No response

@dtolnay dtolnay 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 8, 2022
dtolnay added a commit to dtolnay/unicode-ident that referenced this issue Oct 8, 2022
@kraktus
Copy link
Contributor

kraktus commented Oct 11, 2022

The lint should probably not warn when variable can be resolved to a literal for which truncation does not occur

@wmmc88
Copy link

wmmc88 commented Aug 9, 2023

False positive is also thrown when a const fn returns a usize:

struct A{
  x: u32,
  y: u32,
}
core::mem::size_of::<A>() as u32

bors added a commit that referenced this issue Aug 4, 2024
cast_possible_truncation: Fix some false-positive cases

Fix partially  #7486 (comment).
Fix #9613.
changelog: [`cast_possible_truncation`]: fix some false-positive on % operator, valid constants, and size_of
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

Successfully merging a pull request may close this issue.

3 participants