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