]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / crosspointer_transmute.rs
CommitLineData
f20569fa 1use super::CROSSPOINTER_TRANSMUTE;
cdc7bbd5 2use clippy_utils::diagnostics::span_lint;
f20569fa
XL
3use rustc_hir::Expr;
4use rustc_lint::LateContext;
5use rustc_middle::ty::{self, Ty};
6
7/// Checks for `crosspointer_transmute` lint.
8/// Returns `true` if it's triggered, otherwise returns `false`.
9pub(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}