diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index 0d68789e0be8..074e4bffec13 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -122,7 +122,13 @@ impl LateLintPass for Transmute { let sugg = if from_pty.ty == to_rty.ty { - format!("{}{}", deref, arg) + // 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) }; diff --git a/tests/compile-fail/transmute.rs b/tests/compile-fail/transmute.rs index 4cd19f9bec9c..a0f7ba0ccdea 100644 --- a/tests/compile-fail/transmute.rs +++ b/tests/compile-fail/transmute.rs @@ -58,6 +58,11 @@ unsafe fn _ptr_to_ref(p: *const T, m: *mut T, o: *const U, om: *mut U) { //~| SUGGESTION = &*m; let _: &T = &*m; + let _: &mut T = std::mem::transmute(p as *mut T); + //~^ ERROR transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) + //~| HELP try + //~| SUGGESTION = &mut *(p as *mut T); + let _: &T = std::mem::transmute(o); //~^ ERROR transmute from a pointer type (`*const U`) to a reference type (`&T`) //~| HELP try