]> git.lizzy.rs Git - rust.git/commitdiff
Use `util::sugg` in `TRANSMUTE_PTR_TO_REF`
authormcarton <cartonmartin+git@gmail.com>
Wed, 29 Jun 2016 21:02:15 +0000 (23:02 +0200)
committermcarton <cartonmartin+git@gmail.com>
Fri, 1 Jul 2016 15:12:48 +0000 (17:12 +0200)
clippy_lints/src/transmute.rs
tests/compile-fail/transmute.rs

index bf6af4411b892d9fce7879344ee65f6d2c9ea630..12d2184693a4fb354034e06540f14792fe4bd144 100644 (file)
@@ -3,6 +3,7 @@
 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 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
                                     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,
index 4a120a6eeddec868122efeae6c0c58d785a19521..0dbd58b13089345458fcb1747bff38c235777c17 100644 (file)
@@ -62,6 +62,7 @@ unsafe fn _ptr_to_ref<T, U>(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`)