Skip to content

Commit ea36a9d

Browse files
committed
Set unnecessary_cast suggestion to MaybeIncorrect for pointer casts
Removing casts may cause type inference to stop working which requires manual intervention
1 parent 26edd5a commit ea36a9d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
5656
&format!("casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"),
5757
"try",
5858
cast_str.clone(),
59-
Applicability::MachineApplicable,
59+
Applicability::MaybeIncorrect,
6060
);
6161
}
6262
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![warn(clippy::unnecessary_cast)]
2+
3+
fn main() {
4+
let _ = std::ptr::null() as *const u8;
5+
}
6+
7+
mod issue11113 {
8+
#[repr(C)]
9+
struct Vtbl {
10+
query: unsafe extern "system" fn(),
11+
}
12+
13+
struct TearOff {
14+
object: *mut std::ffi::c_void,
15+
}
16+
17+
impl TearOff {
18+
unsafe fn query(&self) {
19+
((*(*(self.object as *mut *mut _) as *mut Vtbl)).query)()
20+
}
21+
}
22+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
2+
--> $DIR/unnecessary_cast_unfixable.rs:4:13
3+
|
4+
LL | let _ = std::ptr::null() as *const u8;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null()`
6+
|
7+
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
8+
9+
error: casting raw pointers to the same type and constness is unnecessary (`*mut issue11113::Vtbl` -> `*mut issue11113::Vtbl`)
10+
--> $DIR/unnecessary_cast_unfixable.rs:19:16
11+
|
12+
LL | ((*(*(self.object as *mut *mut _) as *mut Vtbl)).query)()
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*(self.object as *mut *mut _)`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)