Skip to content

Invalid warning to cast raw pointer to the same type  #11113

@kennykerr

Description

@kennykerr

Summary

This is a minimal repro from a new warning that popped up in windows-rs.

Lint Name

unnecessary_cast

Reproducer

I tried this code:

#[repr(C)]
struct Vtbl {
    query: unsafe extern "system" fn(),
}

struct TearOff {
    object: *mut std::ffi::c_void,
}

impl TearOff {
    unsafe fn query(&self) {
        ((*(*(self.object as *mut *mut _) as *mut crate::Vtbl)).query)()
    }
}

fn main() {
    if let Some(tear_off) = Option::<TearOff>::None {
        unsafe { tear_off.query() };
    }
}

I saw this happen:

warning: casting raw pointers to the same type and constness is unnecessary (`*mut Vtbl` -> `*mut Vtbl`)
  --> src\main.rs:12:12
   |
12 |         ((*(*(self.object as *mut *mut _) as *mut crate::Vtbl)).query)()
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*(self.object as *mut *mut _)`
   |
   = 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

warning: `empty` (bin "empty") generated 1 warning (run `cargo clippy --fix --bin "empty"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s

I expected to see this happen:

No warnings! 😉

And if you ask clippy to fix it then it blows up:

D:\git\empty>cargo clippy --fix --allow-dirty        
    Checking empty v0.0.0 (D:\git\empty)
warning: failed to automatically apply fixes suggested by rustc to crate `empty`

after fixes were automatically applied the compiler reported errors within these files:

  * src\main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed
  --> src\main.rs:12:10
   |
12 |         ((**(self.object as *mut *mut _)).query)()
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: casting raw pointers to the same type and constness is unnecessary (`*mut Vtbl` -> `*mut Vtbl`)
  --> src\main.rs:12:12
   |
12 |         ((*(*(self.object as *mut *mut _) as *mut crate::Vtbl)).query)()
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*(self.object as *mut *mut _)`
   |
   = 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

warning: `empty` (bin "empty") generated 1 warning (run `cargo clippy --fix --bin "empty"` to apply 1 suggestion)
warning: `empty` (bin "empty" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s

Version

rustc 1.72.0-nightly (d9c13cd45 2023-07-05)
binary: rustc
commit-hash: d9c13cd4531649c2028a8384cb4d4e54f985380e
commit-date: 2023-07-05
host: x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions