summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs
blob: 56207fe767c5716107cca75b48388463013e2a93 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::USELESS_TRANSMUTE;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::sugg;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};

/// Checks for `useless_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>,
    arg: &'tcx Expr<'_>,
) -> bool {
    match (&from_ty.kind(), &to_ty.kind()) {
        _ if from_ty == to_ty && !from_ty.has_erased_regions() => {
            span_lint(
                cx,
                USELESS_TRANSMUTE,
                e.span,
                &format!("transmute from a type (`{from_ty}`) to itself"),
            );
            true
        },
        (ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
            // No way to give the correct suggestion here. Avoid linting for now.
            if !rty.has_erased_regions() {
                span_lint_and_then(
                    cx,
                    USELESS_TRANSMUTE,
                    e.span,
                    "transmute from a reference to a pointer",
                    |diag| {
                        if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
                            let rty_and_mut = ty::TypeAndMut {
                                ty: *rty,
                                mutbl: *rty_mutbl,
                            };

                            let sugg = if *ptr_ty == rty_and_mut {
                                arg.as_ty(to_ty)
                            } else {
                                arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
                            };

                            diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
                        }
                    },
                );
            }
            true
        },
        (ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
            span_lint_and_then(
                cx,
                USELESS_TRANSMUTE,
                e.span,
                "transmute from an integer to a pointer",
                |diag| {
                    if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
                        diag.span_suggestion(e.span, "try", arg.as_ty(to_ty.to_string()), Applicability::Unspecified);
                    }
                },
            );
            true
        },
        _ => false,
    }
}