From c5e91e70d03ace2c9647e509540a5b6b88aaf801 Mon Sep 17 00:00:00 2001 From: mcarton Date: Mon, 4 Jul 2016 02:22:57 +0200 Subject: [PATCH] Use `sugg::Sugg` in transmute links --- clippy_lints/src/transmute.rs | 22 +++++++++++----------- clippy_lints/src/utils/sugg.rs | 6 ++++++ tests/compile-fail/transmute.rs | 7 +++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index d95fad8fc0f..88ed11d26e5 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -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, diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index f857c821e7b..4a9543d47c1 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -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> { 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 ` as ` suggestion. + pub fn as_ty(self, rhs: &str) -> Sugg<'static> { + make_assoc(AssocOp::As, &self, &Sugg::NonParen(rhs.into())) + } + /// Convenience method to create the `&` suggestion. pub fn addr(self) -> Sugg<'static> { make_unop("&", self) diff --git a/tests/compile-fail/transmute.rs b/tests/compile-fail/transmute.rs index 0dbd58b1308..1344858996e 100644 --- a/tests/compile-fail/transmute.rs +++ b/tests/compile-fail/transmute.rs @@ -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; } } -- 2.44.0