1 use super::utils::get_type_snippet;
2 use super::TRANSMUTE_PTR_TO_REF;
3 use clippy_utils::diagnostics::span_lint_and_then;
4 use clippy_utils::sugg;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, Mutability, QPath};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty};
10 /// Checks for `transmute_ptr_to_ref` lint.
11 /// Returns `true` if it's triggered, otherwise returns `false`.
12 pub(super) fn check<'tcx>(
13 cx: &LateContext<'tcx>,
17 args: &'tcx [Expr<'_>],
18 qpath: &'tcx QPath<'_>,
20 match (&from_ty.kind(), &to_ty.kind()) {
21 (ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => {
27 "transmute from a pointer type (`{}`) to a reference type (`{}`)",
31 let arg = sugg::Sugg::hir(cx, &args[0], "..");
32 let (deref, cast) = if *mutbl == Mutability::Mut {
38 let arg = if from_ptr_ty.ty == *to_ref_ty {
41 arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty)))
47 sugg::make_unop(deref, arg).to_string(),
48 Applicability::Unspecified,