2021-03-12 08:30:50 -06:00
|
|
|
use super::CROSSPOINTER_TRANSMUTE;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty::{self, 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 {
|
2024-03-21 16:42:46 -05:00
|
|
|
match (*from_ty.kind(), *to_ty.kind()) {
|
|
|
|
(ty::RawPtr(from_ptr_ty, _), _) if from_ptr_ty == to_ty => {
|
2021-03-12 08:30:50 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
CROSSPOINTER_TRANSMUTE,
|
|
|
|
e.span,
|
2024-03-23 00:52:11 -05:00
|
|
|
format!("transmute from a type (`{from_ty}`) to the type that it points to (`{to_ty}`)"),
|
2021-03-12 08:30:50 -06:00
|
|
|
);
|
|
|
|
true
|
|
|
|
},
|
2024-03-21 16:42:46 -05:00
|
|
|
(_, ty::RawPtr(to_ptr_ty, _)) if to_ptr_ty == from_ty => {
|
2021-03-12 08:30:50 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
CROSSPOINTER_TRANSMUTE,
|
|
|
|
e.span,
|
2024-03-23 00:52:11 -05:00
|
|
|
format!("transmute from a type (`{from_ty}`) to a pointer to that type (`{to_ty}`)"),
|
2021-03-12 08:30:50 -06:00
|
|
|
);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|