mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Set unnecessary_cast
suggestion to MaybeIncorrect
for pointer casts
Removing casts may cause type inference to stop working which requires manual intervention
This commit is contained in:
parent
26edd5a2ab
commit
ea36a9df75
3 changed files with 39 additions and 1 deletions
|
@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
|
||||||
&format!("casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"),
|
&format!("casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"),
|
||||||
"try",
|
"try",
|
||||||
cast_str.clone(),
|
cast_str.clone(),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
22
tests/ui/unnecessary_cast_unfixable.rs
Normal file
22
tests/ui/unnecessary_cast_unfixable.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#![warn(clippy::unnecessary_cast)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = std::ptr::null() as *const u8;
|
||||||
|
}
|
||||||
|
|
||||||
|
mod issue11113 {
|
||||||
|
#[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 Vtbl)).query)()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
tests/ui/unnecessary_cast_unfixable.stderr
Normal file
16
tests/ui/unnecessary_cast_unfixable.stderr
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
|
||||||
|
--> $DIR/unnecessary_cast_unfixable.rs:4:13
|
||||||
|
|
|
||||||
|
LL | let _ = std::ptr::null() as *const u8;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null()`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: casting raw pointers to the same type and constness is unnecessary (`*mut issue11113::Vtbl` -> `*mut issue11113::Vtbl`)
|
||||||
|
--> $DIR/unnecessary_cast_unfixable.rs:19:16
|
||||||
|
|
|
||||||
|
LL | ((*(*(self.object as *mut *mut _) as *mut Vtbl)).query)()
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*(self.object as *mut *mut _)`
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Reference in a new issue