]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #1053 from oli-obk/char_float_transmute
authorOliver Schneider <oli-obk@users.noreply.github.com>
Tue, 28 Jun 2016 12:50:39 +0000 (14:50 +0200)
committerGitHub <noreply@github.com>
Tue, 28 Jun 2016 12:50:39 +0000 (14:50 +0200)
lint on unnecessary and plain wrong transmutes

README.md
clippy_lints/src/transmute.rs
tests/compile-fail/transmute.rs

index d5e80238695012741eefbcaeefd4961250c5bd35..5096ee953b20c8b694ff3eaae1b28e0b0838a00a 100644 (file)
--- a/README.md
+++ b/README.md
@@ -273,7 +273,7 @@ In your `Cargo.toml`:
 clippy = {version = "*", optional = true}
 
 [features]
-default=[]
+default = []
 ```
 
 And, in your `main.rs` or `lib.rs`:
index 61c5a7b2339d3db4d3beec93bc70d1f989d52ca7..bf6af4411b892d9fce7879344ee65f6d2c9ea630 100644 (file)
@@ -157,7 +157,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)
                                     };
index f2762b1767a9a806233e4415dd91e1d23f6a4bf8..4a120a6eeddec868122efeae6c0c58d785a19521 100644 (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