mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
lint transmuting references to pointers
This commit is contained in:
parent
083c57867a
commit
a469ee1061
2 changed files with 32 additions and 0 deletions
|
@ -72,6 +72,23 @@ impl LateLintPass for Transmute {
|
|||
e.span,
|
||||
&format!("transmute from a type (`{}`) to itself", from_ty),
|
||||
),
|
||||
(&TyRef(_, rty), &TyRawPtr(ptr_ty)) => span_lint_and_then(
|
||||
cx,
|
||||
USELESS_TRANSMUTE,
|
||||
e.span,
|
||||
"transmute from a reference to a pointer",
|
||||
|db| {
|
||||
if let Some(arg) = snippet_opt(cx, args[0].span) {
|
||||
let sugg = if ptr_ty == rty {
|
||||
format!("{} as {}", arg, to_ty)
|
||||
} else {
|
||||
format!("{} as {} as {}", arg, cx.tcx.mk_ptr(rty), to_ty)
|
||||
};
|
||||
|
||||
db.span_suggestion(e.span, "try", sugg);
|
||||
}
|
||||
},
|
||||
),
|
||||
(&TyRawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
|
||||
cx,
|
||||
CROSSPOINTER_TRANSMUTE,
|
||||
|
|
|
@ -21,6 +21,21 @@ unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
|
|||
//~^ ERROR transmute from a type (`&'a T`) to itself
|
||||
|
||||
let _: &'a U = core::intrinsics::transmute(t);
|
||||
|
||||
let _: *const T = core::intrinsics::transmute(t);
|
||||
//~^ ERROR transmute from a reference to a pointer
|
||||
//~| HELP try
|
||||
//~| SUGGESTION = t as *const T
|
||||
|
||||
let _: *mut T = core::intrinsics::transmute(t);
|
||||
//~^ ERROR transmute from a reference to a pointer
|
||||
//~| HELP try
|
||||
//~| SUGGESTION = t as *const T as *mut T
|
||||
|
||||
let _: *const U = core::intrinsics::transmute(t);
|
||||
//~^ ERROR transmute from a reference to a pointer
|
||||
//~| HELP try
|
||||
//~| SUGGESTION = t as *const T as *const U
|
||||
}
|
||||
|
||||
#[deny(transmute_ptr_to_ref)]
|
||||
|
|
Loading…
Reference in a new issue