2021-02-11 01:27:44 +09:00
|
|
|
use super::utils::can_be_expressed_as_pointer_cast;
|
|
|
|
use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS;
|
2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::sugg;
|
2021-02-11 01:27:44 +09:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty::Ty;
|
|
|
|
|
|
|
|
/// Checks for `transmutes_expressible_as_ptr_casts` 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>,
|
2022-02-01 10:53:25 -05:00
|
|
|
arg: &'tcx Expr<'_>,
|
2021-02-11 01:27:44 +09:00
|
|
|
) -> bool {
|
|
|
|
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
|
|
|
|
e.span,
|
2022-09-23 13:42:59 -04:00
|
|
|
&format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"),
|
2021-02-11 01:27:44 +09:00
|
|
|
|diag| {
|
2022-02-01 10:53:25 -05:00
|
|
|
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
2022-10-12 04:34:39 -04:00
|
|
|
let sugg = arg.as_ty(to_ty.to_string()).to_string();
|
2021-02-11 01:27:44 +09:00
|
|
|
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|