summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/transmute/crosspointer_transmute.rs
blob: c4b9d82fc735bb05cbacea7481aee9f8c31bebd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use super::CROSSPOINTER_TRANSMUTE;
use clippy_utils::diagnostics::span_lint;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};

/// Checks for `crosspointer_transmute` lint.
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
    match (&from_ty.kind(), &to_ty.kind()) {
        (ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
            span_lint(
                cx,
                CROSSPOINTER_TRANSMUTE,
                e.span,
                &format!("transmute from a type (`{from_ty}`) to the type that it points to (`{to_ty}`)"),
            );
            true
        },
        (_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
            span_lint(
                cx,
                CROSSPOINTER_TRANSMUTE,
                e.span,
                &format!("transmute from a type (`{from_ty}`) to a pointer to that type (`{to_ty}`)"),
            );
            true
        },
        _ => false,
    }
}