2021-02-10 15:50:09 +00:00
|
|
|
use super::TRANSMUTE_PTR_TO_PTR;
|
2021-03-16 00:55:45 +00:00
|
|
|
use crate::utils::sugg;
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-02-10 15:50:09 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
2021-02-10 16:29:45 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-02-10 15:50:09 +00:00
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
}
|