rust-lang / rust-lang/rust-clippy

cast_possible_wrap false negative

Open
#17,755 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

I think there is an issue somewhere between shas

2c5e3d3c5ce9ff2a2d77fc7481c612a843cb008a

and

01dfd311b7cd4853e07d715bc17ec27f3ab06014

from https://github.com/rust-lang/rust-clippy/pull/16866/commits

Lint Name

cast_possible_wrap

Reproducer

I tried this code:

use libc::pid_t;

type MM = i32;

fn main() {
    let x: libc::pid_t = std::process::id() as pid_t;

    let w: MM = std::process::id() as MM;

    let y: i32 = std::process::id() as i32;
    println!("Hello, world {x}:{y}:{w}!");
}

I expected to see this happen:

warning: casting `u32` to `i32` may wrap around the value
 --> src/main.rs:6:26
  |
6 |     let x: libc::pid_t = std::process::id() as pid_t;
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `std::process::id().cast_signed()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
  = note: `-W clippy::cast-possible-wrap` implied by `-W clippy::pedantic`
  = help: to override `-W clippy::pedantic` add `#[allow(clippy::cast_possible_wrap)]`

warning: casting `u32` to `i32` may wrap around the value
 --> src/main.rs:8:17
  |
8 |     let w: MM = std::process::id() as MM;
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `std::process::id().cast_signed()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap

warning: casting `u32` to `i32` may wrap around the value
  --> src/main.rs:10:18
   |
10 |     let y: i32 = std::process::id() as i32;
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `std::process::id().cast_signed()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap

warning: `id` (bin "id") generated 3 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.59s

Instead, this happened:

warning: casting `u32` to `i32` may wrap around the value
 --> src/main.rs:8:17
  |
8 |     let w: MM = std::process::id() as MM;
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `std::process::id().cast_signed()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
  = note: `-W clippy::cast-possible-wrap` implied by `-W clippy::pedantic`
  = help: to override `-W clippy::pedantic` add `#[allow(clippy::cast_possible_wrap)]`

warning: casting `u32` to `i32` may wrap around the value
  --> src/main.rs:10:18
   |
10 |     let y: i32 = std::process::id() as i32;
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `std::process::id().cast_signed()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap

warning: `id` (bin "id") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
Version
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1
LLVM version: 22.1.8

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 with the cast_possible_wrap lint and compare commits 2c5e3d3c5ce9ff2a2d77fc7481c612a843cb008a and 01dfd311b7cd4853e07d715bc17ec27f3ab06014. Use the reproducer in src/main.rs to investigate why the libc::pid_t alias is missed; done means warnings are emitted for x, w, and y as shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.