]>
Commit | Line | Data |
---|---|---|
1 | use super::CROSSPOINTER_TRANSMUTE; | |
2 | use clippy_utils::diagnostics::span_lint; | |
3 | use rustc_hir::Expr; | |
4 | use rustc_lint::LateContext; | |
5 | use rustc_middle::ty::{self, Ty}; | |
6 | ||
7 | /// Checks for `crosspointer_transmute` lint. | |
8 | /// Returns `true` if it's triggered, otherwise returns `false`. | |
9 | pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool { | |
10 | match (&from_ty.kind(), &to_ty.kind()) { | |
11 | (ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => { | |
12 | span_lint( | |
13 | cx, | |
14 | CROSSPOINTER_TRANSMUTE, | |
15 | e.span, | |
16 | &format!( | |
17 | "transmute from a type (`{}`) to the type that it points to (`{}`)", | |
18 | from_ty, to_ty | |
19 | ), | |
20 | ); | |
21 | true | |
22 | }, | |
23 | (_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => { | |
24 | span_lint( | |
25 | cx, | |
26 | CROSSPOINTER_TRANSMUTE, | |
27 | e.span, | |
28 | &format!( | |
29 | "transmute from a type (`{}`) to a pointer to that type (`{}`)", | |
30 | from_ty, to_ty | |
31 | ), | |
32 | ); | |
33 | true | |
34 | }, | |
35 | _ => false, | |
36 | } | |
37 | } |