Use sugg::Sugg in transmute links

This commit is contained in:
mcarton
2016-07-04 02:22:57 +02:00
parent 9b79b1022c
commit c5e91e70d0
3 changed files with 24 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ use rustc::lint::*;
use rustc::ty::TypeVariants::{TyRawPtr, TyRef}; use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
use rustc::ty; use rustc::ty;
use rustc::hir::*; 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; use utils::sugg;
/// **What it does:** This lint checks for transmutes that can't ever be correct on any architecture /// **What it does:** This lint checks for transmutes that can't ever be correct on any architecture
@@ -93,14 +93,14 @@ impl LateLintPass for Transmute {
e.span, e.span,
"transmute from a reference to a pointer", "transmute from a reference to a pointer",
|db| { |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 { let sugg = if ptr_ty == rty {
format!("{} as {}", arg, to_ty) arg.as_ty(&to_ty.to_string())
} else { } 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 @@ impl LateLintPass for Transmute {
e.span, e.span,
"transmute from an integer to a pointer", "transmute from an integer to a pointer",
|db| { |db| {
if let Some(arg) = snippet_opt(cx, args[0].span) { if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
db.span_suggestion(e.span, "try", format!("{} as {}", arg, to_ty)); db.span_suggestion(e.span, "try", arg.as_ty(&to_ty.to_string()).to_string());
} }
}, },
), ),
@@ -157,13 +157,13 @@ impl LateLintPass for Transmute {
}; };
let sugg = if from_pty.ty == to_rty.ty { let arg = if from_pty.ty == to_rty.ty {
sugg::make_unop(deref, arg).to_string() arg
} else { } 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, _ => return,

View File

@@ -30,6 +30,7 @@ impl<'a> std::fmt::Display for Sugg<'a> {
} }
} }
#[allow(wrong_self_convention)] // ok, because of the function `as_ty` method
impl<'a> Sugg<'a> { impl<'a> Sugg<'a> {
pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> { pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> {
snippet_opt(cx, expr.span).map(|snippet| { snippet_opt(cx, expr.span).map(|snippet| {
@@ -124,6 +125,11 @@ impl<'a> Sugg<'a> {
make_binop(ast::BinOpKind::And, &self, &rhs) 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. /// Convenience method to create the `&<expr>` suggestion.
pub fn addr(self) -> Sugg<'static> { pub fn addr(self) -> Sugg<'static> {
make_unop("&", self) make_unop("&", self)

View File

@@ -111,6 +111,13 @@ fn useless() {
//~^ ERROR transmute from an integer to a pointer //~^ ERROR transmute from an integer to a pointer
//~| HELP try //~| HELP try
//~| SUGGESTION 5_isize as *const usize //~| 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;
} }
} }