Move transmute_ptr_to_ptr to its own module

This commit is contained in:
Yusuke Tanaka 2021-02-11 00:50:09 +09:00 committed by flip1995
parent 7af3458b54
commit f8bc0e249c
No known key found for this signature in database
GPG Key ID: 1CA0DF2AF59D68A5
2 changed files with 41 additions and 12 deletions

View File

@ -1,5 +1,6 @@
mod crosspointer_transmute;
mod transmute_int_to_char;
mod transmute_ptr_to_ptr;
mod transmute_ptr_to_ref;
mod transmute_ref_to_ref;
mod useless_transmute;
@ -375,20 +376,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
if triggered {
return;
}
let triggered = transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args);
if triggered {
return;
}
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(_), ty::RawPtr(to_ty)) => span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,
e.span,
"transmute from a pointer to a pointer",
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
}
},
),
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
span_lint_and_then(
cx,

View File

@ -0,0 +1,36 @@
use super::TRANSMUTE_PTR_TO_PTR;
use crate::utils::{span_lint_and_then, sugg};
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::Ty;
/// Checks for `transmute_ptr_to_ptr` 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>,
args: &'tcx [Expr<'_>],
) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,
e.span,
"transmute from a pointer to a pointer",
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
}
},
);
true
},
_ => false,
}
}