diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index bf6af4411b89..12d2184693a4 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -3,6 +3,7 @@ use rustc::ty::TypeVariants::{TyRawPtr, TyRef}; use rustc::ty; use rustc::hir::*; use utils::{match_def_path, paths, snippet_opt, span_lint, span_lint_and_then}; +use utils::sugg; /// **What it does:** This lint checks for transmutes that can't ever be correct on any architecture /// @@ -148,28 +149,21 @@ impl LateLintPass for Transmute { from_ty, to_ty), |db| { - if let Some(arg) = snippet_opt(cx, args[0].span) { - let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable { - ("&mut *", "*mut") - } else { - ("&*", "*const") - }; + let arg = &sugg::Sugg::hir(cx, &args[0], ".."); + let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable { + ("&mut *", "*mut") + } else { + ("&*", "*const") + }; - let sugg = if from_pty.ty == to_rty.ty { - // Put things in parentheses if they are more complex - match args[0].node { - ExprPath(..) | ExprCall(..) | ExprMethodCall(..) | ExprBlock(..) => { - format!("{}{}", deref, arg) - } - _ => format!("{}({})", deref, arg) - } - } else { - format!("{}({} as {} {})", deref, arg, cast, to_rty.ty) - }; + let sugg = if from_pty.ty == to_rty.ty { + sugg::make_unop(deref, arg).to_string() + } else { + format!("{}({} as {} {})", deref, arg, cast, to_rty.ty) + }; - db.span_suggestion(e.span, "try", sugg); - } + db.span_suggestion(e.span, "try", sugg); }, ), _ => return, diff --git a/tests/compile-fail/transmute.rs b/tests/compile-fail/transmute.rs index 4a120a6eedde..0dbd58b13089 100644 --- a/tests/compile-fail/transmute.rs +++ b/tests/compile-fail/transmute.rs @@ -62,6 +62,7 @@ unsafe fn _ptr_to_ref(p: *const T, m: *mut T, o: *const U, om: *mut U) { //~^ ERROR transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) //~| HELP try //~| SUGGESTION = &mut *(p as *mut T); + let _ = &mut *(p as *mut T); let _: &T = std::mem::transmute(o); //~^ ERROR transmute from a pointer type (`*const U`) to a reference type (`&T`)