Add parentheses when necessary in transmute suggestion (fixes #1049)

This commit is contained in:
Manish Goregaokar 2016-06-28 12:18:44 +05:30
parent 910a62444c
commit cdce78a4be
No known key found for this signature in database
GPG Key ID: 3BBF4D3E2EF79F98
2 changed files with 12 additions and 1 deletions

View File

@ -122,7 +122,13 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
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)
};

View File

@ -58,6 +58,11 @@ unsafe fn _ptr_to_ref<T, U>(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