Move crosspointer_transmute to its own module

This commit is contained in:
Yusuke Tanaka 2021-02-11 00:02:21 +09:00 committed by flip1995
parent ef97764dcc
commit afc9275928
No known key found for this signature in database
GPG key ID: 1CA0DF2AF59D68A5
2 changed files with 43 additions and 18 deletions

View file

@ -0,0 +1,38 @@
use super::CROSSPOINTER_TRANSMUTE;
use crate::utils::span_lint;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::Ty;
/// Checks for `crosspointer_transmute` lint.
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to the type that it points to (`{}`)",
from_ty, to_ty
),
);
true
},
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
from_ty, to_ty
),
);
true
},
_ => false,
}
}

View file

@ -1,3 +1,4 @@
mod crosspointer_transmute;
mod useless_transmute;
mod utils;
mod wrong_transmute;
@ -355,26 +356,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
if triggered {
return;
}
let triggered = crosspointer_transmute::check(cx, e, from_ty, to_ty);
if triggered {
return;
}
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to the type that it points to (`{}`)",
from_ty, to_ty
),
),
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
from_ty, to_ty
),
),
(ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_REF,