]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Auto merge of #76136 - CDirkx:const-result, r=dtolnay
[rust.git] / clippy_lints / src / enum_clike.rs
1 //! lint on C-like enums that are `repr(isize/usize)` and have values that
2 //! don't fit into an `i32`
3
4 use crate::consts::{miri_to_const, Constant};
5 use crate::utils::span_lint;
6 use rustc_ast::ast::{IntTy, UintTy};
7 use rustc_hir::{Item, ItemKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::ty;
10 use rustc_middle::ty::util::IntTypeExt;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use std::convert::TryFrom;
13
14 declare_clippy_lint! {
15     /// **What it does:** Checks for C-like enumerations that are
16     /// `repr(isize/usize)` and have values that don't fit into an `i32`.
17     ///
18     /// **Why is this bad?** This will truncate the variant value on 32 bit
19     /// architectures, but works fine on 64 bit.
20     ///
21     /// **Known problems:** None.
22     ///
23     /// **Example:**
24     /// ```rust
25     /// # #[cfg(target_pointer_width = "64")]
26     /// #[repr(usize)]
27     /// enum NonPortable {
28     ///     X = 0x1_0000_0000,
29     ///     Y = 0,
30     /// }
31     /// ```
32     pub ENUM_CLIKE_UNPORTABLE_VARIANT,
33     correctness,
34     "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
35 }
36
37 declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);
38
39 impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
40     #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
41     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
42         if cx.tcx.data_layout.pointer_size.bits() != 64 {
43             return;
44         }
45         if let ItemKind::Enum(def, _) = &item.kind {
46             for var in def.variants {
47                 if let Some(anon_const) = &var.disr_expr {
48                     let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
49                     let mut ty = cx.tcx.type_of(def_id.to_def_id());
50                     let constant = cx
51                         .tcx
52                         .const_eval_poly(def_id.to_def_id())
53                         .ok()
54                         .map(|val| rustc_middle::ty::Const::from_value(cx.tcx, val, ty));
55                     if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) {
56                         if let ty::Adt(adt, _) = ty.kind() {
57                             if adt.is_enum() {
58                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
59                             }
60                         }
61                         match ty.kind() {
62                             ty::Int(IntTy::Isize) => {
63                                 let val = ((val as i128) << 64) >> 64;
64                                 if i32::try_from(val).is_ok() {
65                                     continue;
66                                 }
67                             },
68                             ty::Uint(UintTy::Usize) if val > u128::from(u32::MAX) => {},
69                             _ => continue,
70                         }
71                         span_lint(
72                             cx,
73                             ENUM_CLIKE_UNPORTABLE_VARIANT,
74                             var.span,
75                             "C-like enum variant discriminant is not portable to 32-bit targets",
76                         );
77                     };
78                 }
79             }
80         }
81     }
82 }