]> git.lizzy.rs Git - rust.git/commitdiff
Use `sugg::Sugg` in transmute links
authormcarton <cartonmartin+git@gmail.com>
Mon, 4 Jul 2016 00:22:57 +0000 (02:22 +0200)
committermcarton <cartonmartin+git@gmail.com>
Mon, 4 Jul 2016 00:22:57 +0000 (02:22 +0200)
clippy_lints/src/transmute.rs
clippy_lints/src/utils/sugg.rs
tests/compile-fail/transmute.rs

index d95fad8fc0f0f0b0502c6217bb41bafec12a0fd2..88ed11d26e581b57333c0f223e4e398265d81e52 100644 (file)
@@ -2,7 +2,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::{match_def_path, paths, 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
@@ -93,14 +93,14 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
                             e.span,
                             "transmute from a reference to a pointer",
                             |db| {
-                                if let Some(arg) = snippet_opt(cx, args[0].span) {
+                                if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
                                     let sugg = if ptr_ty == rty {
-                                        format!("{} as {}", arg, to_ty)
+                                        arg.as_ty(&to_ty.to_string())
                                     } else {
-                                        format!("{} as {} as {}", arg, cx.tcx.mk_ptr(rty), to_ty)
+                                        arg.as_ty(&format!("{} as {}", cx.tcx.mk_ptr(rty), to_ty))
                                     };
 
-                                    db.span_suggestion(e.span, "try", sugg);
+                                    db.span_suggestion(e.span, "try", sugg.to_string());
                                 }
                             },
                         ),
@@ -111,8 +111,8 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
                             e.span,
                             "transmute from an integer to a pointer",
                             |db| {
-                                if let Some(arg) = snippet_opt(cx, args[0].span) {
-                                    db.span_suggestion(e.span, "try", format!("{} as {}", arg, to_ty));
+                                if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
+                                    db.span_suggestion(e.span, "try", arg.as_ty(&to_ty.to_string()).to_string());
                                 }
                             },
                         ),
@@ -157,13 +157,13 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
                                 };
 
 
-                                let sugg = if from_pty.ty == to_rty.ty {
-                                    sugg::make_unop(deref, arg).to_string()
+                                let arg = if from_pty.ty == to_rty.ty {
+                                    arg
                                 } else {
-                                    format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
+                                    arg.as_ty(&format!("{} {}", cast, to_rty.ty))
                                 };
 
-                                db.span_suggestion(e.span, "try", sugg);
+                                db.span_suggestion(e.span, "try", sugg::make_unop(deref, arg).to_string());
                             },
                         ),
                         _ => return,
index f857c821e7ba2ee2b789016b8385b102a25dfb2d..4a9543d47c1cbf793b8589847843fa8c07154397 100644 (file)
@@ -30,6 +30,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
     }
 }
 
+#[allow(wrong_self_convention)] // ok, because of the function `as_ty` method
 impl<'a> Sugg<'a> {
     pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> {
         snippet_opt(cx, expr.span).map(|snippet| {
@@ -124,6 +125,11 @@ pub fn and(self, rhs: Self) -> Sugg<'static> {
         make_binop(ast::BinOpKind::And, &self, &rhs)
     }
 
+    /// Convenience method to create the `<lhs> as <rhs>` suggestion.
+    pub fn as_ty(self, rhs: &str) -> Sugg<'static> {
+        make_assoc(AssocOp::As, &self, &Sugg::NonParen(rhs.into()))
+    }
+
     /// Convenience method to create the `&<expr>` suggestion.
     pub fn addr(self) -> Sugg<'static> {
         make_unop("&", self)
index 0dbd58b13089345458fcb1747bff38c235777c17..1344858996e999c7355d2cf740a4ee4d6cd93911 100644 (file)
@@ -111,6 +111,13 @@ fn useless() {
         //~^ ERROR transmute from an integer to a pointer
         //~| HELP try
         //~| SUGGESTION 5_isize as *const usize
+        let _  = 5_isize as *const usize;
+
+        let _: *const usize = std::mem::transmute(1+1usize);
+        //~^ ERROR transmute from an integer to a pointer
+        //~| HELP try
+        //~| SUGGESTION (1+1usize) as *const usize
+        let _  = (1+1_usize) as *const usize;
     }
 }