lint transmuting references to pointers

This commit is contained in:
Oliver Schneider 2016-06-27 16:12:48 +02:00
parent 083c57867a
commit a469ee1061
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
2 changed files with 32 additions and 0 deletions

View file

@ -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,

View file

@ -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)]