rust-lang / rust-lang/rust-clippy

`cast_possible_truncation` could leverage simple patterns

Open
#12,781 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Rust playground reproducer and tracing the cast_possible_truncation lint in rust-clippy. Investigate how the lint handles the shown 1..=255 pattern and verify that the warning is suppressed only when the cast is provably safe. Done means the reproducer no longer warns while genuinely truncating casts still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.