1 use super::USELESS_TRANSMUTE;
2 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
3 use clippy_utils::sugg;
4 use rustc_errors::Applicability;
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
9 /// Checks for `useless_transmute` lint.
10 /// Returns `true` if it's triggered, otherwise returns `false`.
11 pub(super) fn check<'tcx>(
12 cx: &LateContext<'tcx>,
16 args: &'tcx [Expr<'_>],
18 match (&from_ty.kind(), &to_ty.kind()) {
19 _ if from_ty == to_ty => {
24 &format!("transmute from a type (`{}`) to itself", from_ty),
28 (ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
33 "transmute from a reference to a pointer",
35 if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
36 let rty_and_mut = ty::TypeAndMut {
41 let sugg = if *ptr_ty == rty_and_mut {
44 arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
47 diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
53 (ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
58 "transmute from an integer to a pointer",
60 if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
64 arg.as_ty(&to_ty.to_string()).to_string(),
65 Applicability::Unspecified,