Skip to content

Commit c5e91e7

Browse files
committed
Use sugg::Sugg in transmute links
1 parent 9b79b10 commit c5e91e7

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

clippy_lints/src/transmute.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc::lint::*;
22
use rustc::ty::TypeVariants::{TyRawPtr, TyRef};
33
use rustc::ty;
44
use rustc::hir::*;
5-
use utils::{match_def_path, paths, snippet_opt, span_lint, span_lint_and_then};
5+
use utils::{match_def_path, paths, span_lint, span_lint_and_then};
66
use utils::sugg;
77

88
/// **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 {
9393
e.span,
9494
"transmute from a reference to a pointer",
9595
|db| {
96-
if let Some(arg) = snippet_opt(cx, args[0].span) {
96+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
9797
let sugg = if ptr_ty == rty {
98-
format!("{} as {}", arg, to_ty)
98+
arg.as_ty(&to_ty.to_string())
9999
} else {
100-
format!("{} as {} as {}", arg, cx.tcx.mk_ptr(rty), to_ty)
100+
arg.as_ty(&format!("{} as {}", cx.tcx.mk_ptr(rty), to_ty))
101101
};
102102

103-
db.span_suggestion(e.span, "try", sugg);
103+
db.span_suggestion(e.span, "try", sugg.to_string());
104104
}
105105
},
106106
),
@@ -111,8 +111,8 @@ impl LateLintPass for Transmute {
111111
e.span,
112112
"transmute from an integer to a pointer",
113113
|db| {
114-
if let Some(arg) = snippet_opt(cx, args[0].span) {
115-
db.span_suggestion(e.span, "try", format!("{} as {}", arg, to_ty));
114+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &*args[0]) {
115+
db.span_suggestion(e.span, "try", arg.as_ty(&to_ty.to_string()).to_string());
116116
}
117117
},
118118
),
@@ -157,13 +157,13 @@ impl LateLintPass for Transmute {
157157
};
158158

159159

160-
let sugg = if from_pty.ty == to_rty.ty {
161-
sugg::make_unop(deref, arg).to_string()
160+
let arg = if from_pty.ty == to_rty.ty {
161+
arg
162162
} else {
163-
format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
163+
arg.as_ty(&format!("{} {}", cast, to_rty.ty))
164164
};
165165

166-
db.span_suggestion(e.span, "try", sugg);
166+
db.span_suggestion(e.span, "try", sugg::make_unop(deref, arg).to_string());
167167
},
168168
),
169169
_ => return,

clippy_lints/src/utils/sugg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl<'a> std::fmt::Display for Sugg<'a> {
3030
}
3131
}
3232

33+
#[allow(wrong_self_convention)] // ok, because of the function `as_ty` method
3334
impl<'a> Sugg<'a> {
3435
pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> {
3536
snippet_opt(cx, expr.span).map(|snippet| {
@@ -124,6 +125,11 @@ impl<'a> Sugg<'a> {
124125
make_binop(ast::BinOpKind::And, &self, &rhs)
125126
}
126127

128+
/// Convenience method to create the `<lhs> as <rhs>` suggestion.
129+
pub fn as_ty(self, rhs: &str) -> Sugg<'static> {
130+
make_assoc(AssocOp::As, &self, &Sugg::NonParen(rhs.into()))
131+
}
132+
127133
/// Convenience method to create the `&<expr>` suggestion.
128134
pub fn addr(self) -> Sugg<'static> {
129135
make_unop("&", self)

tests/compile-fail/transmute.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ fn useless() {
111111
//~^ ERROR transmute from an integer to a pointer
112112
//~| HELP try
113113
//~| SUGGESTION 5_isize as *const usize
114+
let _ = 5_isize as *const usize;
115+
116+
let _: *const usize = std::mem::transmute(1+1usize);
117+
//~^ ERROR transmute from an integer to a pointer
118+
//~| HELP try
119+
//~| SUGGESTION (1+1usize) as *const usize
120+
let _ = (1+1_usize) as *const usize;
114121
}
115122
}
116123

0 commit comments

Comments
 (0)