mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Use sugg::Sugg
in transmute links
This commit is contained in:
parent
9b79b1022c
commit
c5e91e70d0
3 changed files with 24 additions and 11 deletions
|
@ -2,7 +2,7 @@ use rustc::lint::*;
|
|||
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 @@ impl LateLintPass for Transmute {
|
|||
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 @@ impl LateLintPass for Transmute {
|
|||
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 @@ impl LateLintPass for Transmute {
|
|||
};
|
||||
|
||||
|
||||
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,
|
||||
|
|
|
@ -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> {
|
||||
pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> {
|
||||
snippet_opt(cx, expr.span).map(|snippet| {
|
||||
|
@ -124,6 +125,11 @@ impl<'a> Sugg<'a> {
|
|||
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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue