rust-lang / rust-lang/rust-clippy

unnecessary_cast on `as u64` for a c_ulong

Open
#10,555 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

unnecessary_cast triggers on casts from C FFI type aliases, even though the type aliases may not always match the type being cast to.

Lint Name

unnecessary_cast

Reproducer

I tried this code:

use core::ffi::c_ulong;

fn main() {
    let x: c_ulong = 0;
    let y = x as u64;
    println!("Hello, {y}!");
}

I saw this happen:

warning: casting to the same type is unnecessary (`u64` -> `u64`)
 --> src/main.rs:5:13
  |
5 |     let y = x as u64;
  |             ^^^^^^^^ help: try: `x`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
  = note: `#[warn(clippy::unnecessary_cast)]` on by default

I expected to not get an error, because c_ulong is a type alias that isn't always u64.

I thought https://github.com/rust-lang/rust-clippy/pull/8596 was supposed to fix this.

Version
rustc 1.68.1 (8460ca823 2023-03-20)
binary: rustc
commit-hash: 8460ca823e8367a30dda430efda790588b8c84d3
commit-date: 2023-03-20
host: x86_64-unknown-linux-gnu
release: 1.68.1
LLVM version: 15.0.6
Additional Labels

No response

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 reproducer and reviewing the unnecessary_cast lint, then compare its behavior with pull request #8596. Done means c_ulong as u64 is not incorrectly reported as an unnecessary cast on targets where the aliases differ, while genuine same-type casts remain covered.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.