]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs
rustc_typeck to rustc_hir_analysis
[rust.git] / src / tools / clippy / clippy_lints / src / transmute / transmute_ptr_to_ptr.rs
1 use super::TRANSMUTE_PTR_TO_PTR;
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::sugg;
4 use rustc_errors::Applicability;
5 use rustc_hir::Expr;
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
8
9 /// Checks for `transmute_ptr_to_ptr` lint.
10 /// Returns `true` if it's triggered, otherwise returns `false`.
11 pub(super) fn check<'tcx>(
12     cx: &LateContext<'tcx>,
13     e: &'tcx Expr<'_>,
14     from_ty: Ty<'tcx>,
15     to_ty: Ty<'tcx>,
16     arg: &'tcx Expr<'_>,
17 ) -> bool {
18     match (&from_ty.kind(), &to_ty.kind()) {
19         (ty::RawPtr(_), ty::RawPtr(to_ty)) => {
20             span_lint_and_then(
21                 cx,
22                 TRANSMUTE_PTR_TO_PTR,
23                 e.span,
24                 "transmute from a pointer to a pointer",
25                 |diag| {
26                     if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
27                         let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
28                         diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
29                     }
30                 },
31             );
32             true
33         },
34         _ => false,
35     }
36 }