Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kennykerr opened this issue Jul 6, 2023 · 6 comments · Fixed by #11152
Closed

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

kennykerr opened this issue Jul 6, 2023 · 6 comments · Fixed by #11152
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@kennykerr
Copy link

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

@kennykerr kennykerr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 6, 2023
@Alexendoo
Copy link
Member

We should make it not automatically apply the suggestions in --fix since as can guide inference

Linting here seems correct to me though since it is casting a *mut Vtbl to a *mut Vtbl unless I'm missing something

@Alexendoo Alexendoo added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 6, 2023
@kennykerr
Copy link
Author

That's what clippy seems to think as well but no, it's casting a *mut c_void to a *mut *mut c_void, dereferencing that, and then casting that to a *mut Vtbl.

This lets the us hop through to the implementation of a virtual function table.

@Alexendoo
Copy link
Member

The cast self.object as *mut *mut _ is inferred to be self.object as *mut *mut Vtbl - https://godbolt.org/z/TdGj7secs

@kennykerr
Copy link
Author

kennykerr commented Jul 6, 2023

Ah, that's very cool compiler flag. 😏

Thanks! So then the clippy suggestion is just slightly off as it doesn't work as suggested?

@Alexendoo
Copy link
Member

Yeah the suggestion needs some human help to keep the type inference working in this case, so cargo clippy --fix shouldn't apply it automatically

@bors bors closed this as completed in bafde54 Jul 14, 2023
@kennykerr
Copy link
Author

Thanks for the fix! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants