summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/enum_clike.rs
blob: da67888827d199a31de66d410b2e5e0c8a4f27ca (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
73
74
75
76
77
78
79
80
81
//! lint on C-like enums that are `repr(isize/usize)` and have values that
//! don't fit into an `i32`

use clippy_utils::consts::{miri_to_const, Constant};
use clippy_utils::diagnostics::span_lint;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, IntTy, UintTy};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
    /// ### What it does
    /// Checks for C-like enumerations that are
    /// `repr(isize/usize)` and have values that don't fit into an `i32`.
    ///
    /// ### Why is this bad?
    /// This will truncate the variant value on 32 bit
    /// architectures, but works fine on 64 bit.
    ///
    /// ### Example
    /// ```rust
    /// # #[cfg(target_pointer_width = "64")]
    /// #[repr(usize)]
    /// enum NonPortable {
    ///     X = 0x1_0000_0000,
    ///     Y = 0,
    /// }
    /// ```
    #[clippy::version = "pre 1.29.0"]
    pub ENUM_CLIKE_UNPORTABLE_VARIANT,
    correctness,
    "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
}

declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);

impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
    #[expect(clippy::cast_possible_wrap)]
    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
        if cx.tcx.data_layout.pointer_size.bits() != 64 {
            return;
        }
        if let ItemKind::Enum(def, _) = &item.kind {
            for var in def.variants {
                if let Some(anon_const) = &var.disr_expr {
                    let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
                    let mut ty = cx.tcx.type_of(def_id.to_def_id());
                    let constant = cx
                        .tcx
                        .const_eval_poly(def_id.to_def_id())
                        .ok()
                        .map(|val| rustc_middle::mir::ConstantKind::from_value(val, ty));
                    if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) {
                        if let ty::Adt(adt, _) = ty.kind() {
                            if adt.is_enum() {
                                ty = adt.repr().discr_type().to_ty(cx.tcx);
                            }
                        }
                        match ty.kind() {
                            ty::Int(IntTy::Isize) => {
                                let val = ((val as i128) << 64) >> 64;
                                if i32::try_from(val).is_ok() {
                                    continue;
                                }
                            },
                            ty::Uint(UintTy::Usize) if val > u128::from(u32::MAX) => {},
                            _ => continue,
                        }
                        span_lint(
                            cx,
                            ENUM_CLIKE_UNPORTABLE_VARIANT,
                            var.span,
                            "C-like enum variant discriminant is not portable to 32-bit targets",
                        );
                    };
                }
            }
        }
    }
}