Skip to content

unnecessary_cast on as u64 for a c_ulong #10555

Open
@joshtriplett

Description

@joshtriplett

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions