mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-02 06:28:20 +03:00
Use util::sugg in TRANSMUTE_PTR_TO_REF
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
use rustc::ty;
|
||||
use rustc::hir::*;
|
||||
use utils::{match_def_path, paths, snippet_opt, 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
|
||||
///
|
||||
@@ -148,28 +149,21 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
|
||||
from_ty,
|
||||
to_ty),
|
||||
|db| {
|
||||
if let Some(arg) = snippet_opt(cx, args[0].span) {
|
||||
let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
|
||||
("&mut *", "*mut")
|
||||
} else {
|
||||
("&*", "*const")
|
||||
};
|
||||
let arg = &sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let (deref, cast) = if to_rty.mutbl == Mutability::MutMutable {
|
||||
("&mut *", "*mut")
|
||||
} else {
|
||||
("&*", "*const")
|
||||
};
|
||||
|
||||
|
||||
let sugg = if from_pty.ty == to_rty.ty {
|
||||
// Put things in parentheses if they are more complex
|
||||
match args[0].node {
|
||||
ExprPath(..) | ExprCall(..) | ExprMethodCall(..) | ExprBlock(..) => {
|
||||
format!("{}{}", deref, arg)
|
||||
}
|
||||
_ => format!("{}({})", deref, arg)
|
||||
}
|
||||
} else {
|
||||
format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
|
||||
};
|
||||
let sugg = if from_pty.ty == to_rty.ty {
|
||||
sugg::make_unop(deref, arg).to_string()
|
||||
} else {
|
||||
format!("{}({} as {} {})", deref, arg, cast, to_rty.ty)
|
||||
};
|
||||
|
||||
db.span_suggestion(e.span, "try", sugg);
|
||||
}
|
||||
db.span_suggestion(e.span, "try", sugg);
|
||||
},
|
||||
),
|
||||
_ => return,
|
||||
|
||||
@@ -62,6 +62,7 @@ unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
|
||||
//~^ ERROR transmute from a pointer type (`*mut T`) to a reference type (`&mut T`)
|
||||
//~| HELP try
|
||||
//~| SUGGESTION = &mut *(p as *mut T);
|
||||
let _ = &mut *(p as *mut T);
|
||||
|
||||
let _: &T = std::mem::transmute(o);
|
||||
//~^ ERROR transmute from a pointer type (`*const U`) to a reference type (`&T`)
|
||||
|
||||
Reference in New Issue
Block a user