]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ref.rs
Rollup merge of #102888 - GuillaumeGomez:improve-search-color-check, r=notriddle
[rust.git] / src / tools / clippy / clippy_lints / src / transmute / transmute_ptr_to_ref.rs
1 use super::TRANSMUTE_PTR_TO_REF;
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::source::snippet_with_applicability;
4 use clippy_utils::{meets_msrv, msrvs, sugg};
5 use rustc_errors::Applicability;
6 use rustc_hir::{self as hir, Expr, GenericArg, Mutability, Path, TyKind};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty, TypeVisitable};
9 use rustc_semver::RustcVersion;
10
11 /// Checks for `transmute_ptr_to_ref` lint.
12 /// Returns `true` if it's triggered, otherwise returns `false`.
13 pub(super) fn check<'tcx>(
14     cx: &LateContext<'tcx>,
15     e: &'tcx Expr<'_>,
16     from_ty: Ty<'tcx>,
17     to_ty: Ty<'tcx>,
18     arg: &'tcx Expr<'_>,
19     path: &'tcx Path<'_>,
20     msrv: Option<RustcVersion>,
21 ) -> bool {
22     match (&from_ty.kind(), &to_ty.kind()) {
23         (ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => {
24             span_lint_and_then(
25                 cx,
26                 TRANSMUTE_PTR_TO_REF,
27                 e.span,
28                 &format!("transmute from a pointer type (`{from_ty}`) to a reference type (`{to_ty}`)"),
29                 |diag| {
30                     let arg = sugg::Sugg::hir(cx, arg, "..");
31                     let (deref, cast) = if *mutbl == Mutability::Mut {
32                         ("&mut *", "*mut")
33                     } else {
34                         ("&*", "*const")
35                     };
36                     let mut app = Applicability::MachineApplicable;
37
38                     let sugg = if let Some(ty) = get_explicit_type(path) {
39                         let ty_snip = snippet_with_applicability(cx, ty.span, "..", &mut app);
40                         if meets_msrv(msrv, msrvs::POINTER_CAST) {
41                             format!("{deref}{}.cast::<{ty_snip}>()", arg.maybe_par())
42                         } else if from_ptr_ty.has_erased_regions() {
43                             sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {ty_snip}"))).to_string()
44                         } else {
45                             sugg::make_unop(deref, arg.as_ty(format!("{cast} {ty_snip}"))).to_string()
46                         }
47                     } else if from_ptr_ty.ty == *to_ref_ty {
48                         if from_ptr_ty.has_erased_regions() {
49                             if meets_msrv(msrv, msrvs::POINTER_CAST) {
50                                 format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_par())
51                             } else {
52                                 sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {to_ref_ty}")))
53                                     .to_string()
54                             }
55                         } else {
56                             sugg::make_unop(deref, arg).to_string()
57                         }
58                     } else {
59                         sugg::make_unop(deref, arg.as_ty(format!("{cast} {to_ref_ty}"))).to_string()
60                     };
61
62                     diag.span_suggestion(e.span, "try", sugg, app);
63                 },
64             );
65             true
66         },
67         _ => false,
68     }
69 }
70
71 /// Gets the type `Bar` in `…::transmute<Foo, &Bar>`.
72 fn get_explicit_type<'tcx>(path: &'tcx Path<'tcx>) -> Option<&'tcx hir::Ty<'tcx>> {
73     if let GenericArg::Type(ty) = path.segments.last()?.args?.args.get(1)?
74         && let TyKind::Rptr(_, ty) = &ty.kind
75     {
76         Some(ty.ty)
77     } else {
78         None
79     }
80 }