mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 04:04:06 +00:00
Minor cleanup on transmute lints
This commit is contained in:
parent
7bb69c0ae0
commit
8ef87455b0
@ -372,10 +372,9 @@ declare_lint_pass!(Transmute => [
|
||||
]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for Transmute {
|
||||
#[allow(clippy::similar_names, clippy::too_many_lines)]
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::Call(path_expr, args) = e.kind;
|
||||
if let ExprKind::Call(path_expr, [arg]) = e.kind;
|
||||
if let ExprKind::Path(ref qpath) = path_expr.kind;
|
||||
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
|
||||
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
|
||||
@ -385,28 +384,28 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
|
||||
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
|
||||
let const_context = in_constant(cx, e.hir_id);
|
||||
|
||||
let from_ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let from_ty = cx.typeck_results().expr_ty(arg);
|
||||
let to_ty = cx.typeck_results().expr_ty(e);
|
||||
|
||||
// If useless_transmute is triggered, the other lints can be skipped.
|
||||
if useless_transmute::check(cx, e, from_ty, to_ty, args) {
|
||||
if useless_transmute::check(cx, e, from_ty, to_ty, arg) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut linted = wrong_transmute::check(cx, e, from_ty, to_ty);
|
||||
linted |= crosspointer_transmute::check(cx, e, from_ty, to_ty);
|
||||
linted |= transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, args, qpath);
|
||||
linted |= transmute_int_to_char::check(cx, e, from_ty, to_ty, args);
|
||||
linted |= transmute_ref_to_ref::check(cx, e, from_ty, to_ty, args, const_context);
|
||||
linted |= transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args);
|
||||
linted |= transmute_int_to_bool::check(cx, e, from_ty, to_ty, args);
|
||||
linted |= transmute_int_to_float::check(cx, e, from_ty, to_ty, args, const_context);
|
||||
linted |= transmute_float_to_int::check(cx, e, from_ty, to_ty, args, const_context);
|
||||
linted |= transmute_num_to_bytes::check(cx, e, from_ty, to_ty, args, const_context);
|
||||
linted |= unsound_collection_transmute::check(cx, e, from_ty, to_ty);
|
||||
let linted = wrong_transmute::check(cx, e, from_ty, to_ty)
|
||||
| crosspointer_transmute::check(cx, e, from_ty, to_ty)
|
||||
| transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, qpath)
|
||||
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg)
|
||||
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
|
||||
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg)
|
||||
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
|
||||
| transmute_int_to_float::check(cx, e, from_ty, to_ty, arg, const_context)
|
||||
| transmute_float_to_int::check(cx, e, from_ty, to_ty, arg, const_context)
|
||||
| transmute_num_to_bytes::check(cx, e, from_ty, to_ty, arg, const_context)
|
||||
| unsound_collection_transmute::check(cx, e, from_ty, to_ty);
|
||||
|
||||
if !linted {
|
||||
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, args);
|
||||
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
const_context: bool,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
@ -26,7 +26,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|
||||
|diag| {
|
||||
let mut expr = &args[0];
|
||||
let mut expr = arg;
|
||||
let mut arg = sugg::Sugg::hir(cx, expr, "..");
|
||||
|
||||
if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {
|
||||
|
@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
|
||||
@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `bool`", from_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
let zero = sugg::Sugg::NonParen(Cow::from("0"));
|
||||
diag.span_suggestion(
|
||||
e.span,
|
||||
|
@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
|
||||
@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `char`", from_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
let arg = if let ty::Int(_) = from_ty.kind() {
|
||||
arg.as_ty(ast::UintTy::U32.name_str())
|
||||
} else {
|
||||
|
@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
const_context: bool,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
let arg = if let ty::Int(int_ty) = from_ty.kind() {
|
||||
arg.as_ty(format!(
|
||||
"u{}",
|
||||
|
@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
const_context: bool,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
@ -33,7 +33,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
diag.span_suggestion(
|
||||
e.span,
|
||||
"consider using `to_ne_bytes()`",
|
||||
|
@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
|
||||
@ -23,7 +23,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
"transmute from a pointer to a pointer",
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
|
||||
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
qpath: &'tcx QPath<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
|
||||
from_ty, to_ty
|
||||
),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = sugg::Sugg::hir(cx, arg, "..");
|
||||
let (deref, cast) = if *mutbl == Mutability::Mut {
|
||||
("&mut *", "*mut")
|
||||
} else {
|
||||
|
@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
const_context: bool,
|
||||
) -> bool {
|
||||
let mut triggered = false;
|
||||
@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
|
||||
format!(
|
||||
"std::str::from_utf8{}({}).unwrap()",
|
||||
postfix,
|
||||
snippet(cx, args[0].span, ".."),
|
||||
snippet(cx, arg.span, ".."),
|
||||
),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
@ -54,7 +54,7 @@ pub(super) fn check<'tcx>(
|
||||
TRANSMUTE_PTR_TO_PTR,
|
||||
e.span,
|
||||
"transmute from a reference to a reference",
|
||||
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
|
||||
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let ty_from_and_mut = ty::TypeAndMut {
|
||||
ty: ty_from,
|
||||
mutbl: *from_mutbl
|
||||
|
@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
|
||||
span_lint_and_then(
|
||||
@ -26,7 +26,7 @@ pub(super) fn check<'tcx>(
|
||||
from_ty, to_ty
|
||||
),
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
|
||||
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
|
||||
e: &'tcx Expr<'_>,
|
||||
from_ty: Ty<'tcx>,
|
||||
to_ty: Ty<'tcx>,
|
||||
args: &'tcx [Expr<'_>],
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
_ if from_ty == to_ty => {
|
||||
@ -32,7 +32,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
"transmute from a reference to a pointer",
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let rty_and_mut = ty::TypeAndMut {
|
||||
ty: rty,
|
||||
mutbl: *rty_mutbl,
|
||||
@ -57,7 +57,7 @@ pub(super) fn check<'tcx>(
|
||||
e.span,
|
||||
"transmute from an integer to a pointer",
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
diag.span_suggestion(
|
||||
e.span,
|
||||
"try",
|
||||
|
Loading…
Reference in New Issue
Block a user