]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[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::hir::*;
7 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
8 use rustc::mir::interpret::GlobalId;
9 use rustc::ty;
10 use rustc::ty::subst::InternalSubsts;
11 use rustc::ty::util::IntTypeExt;
12 use rustc::{declare_tool_lint, lint_array};
13 use syntax::ast::{IntTy, UintTy};
14
15 declare_clippy_lint! {
16     /// **What it does:** Checks for C-like enumerations that are
17     /// `repr(isize/usize)` and have values that don't fit into an `i32`.
18     ///
19     /// **Why is this bad?** This will truncate the variant value on 32 bit
20     /// architectures, but works fine on 64 bit.
21     ///
22     /// **Known problems:** None.
23     ///
24     /// **Example:**
25     /// ```rust
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 pub struct UnportableVariant;
38
39 impl LintPass for UnportableVariant {
40     fn get_lints(&self) -> LintArray {
41         lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
42     }
43
44     fn name(&self) -> &'static str {
45         "UnportableVariant"
46     }
47 }
48
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
50     #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
51     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
52         if cx.tcx.data_layout.pointer_size.bits() != 64 {
53             return;
54         }
55         if let ItemKind::Enum(ref def, _) = item.node {
56             for var in &def.variants {
57                 let variant = &var.node;
58                 if let Some(ref anon_const) = variant.disr_expr {
59                     let param_env = ty::ParamEnv::empty();
60                     let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
61                     let substs = InternalSubsts::identity_for_item(cx.tcx.global_tcx(), def_id);
62                     let instance = ty::Instance::new(def_id, substs);
63                     let c_id = GlobalId {
64                         instance,
65                         promoted: None,
66                     };
67                     let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
68                     if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, &c)) {
69                         let mut ty = cx.tcx.type_of(def_id);
70                         if let ty::Adt(adt, _) = ty.sty {
71                             if adt.is_enum() {
72                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
73                             }
74                         }
75                         match ty.sty {
76                             ty::Int(IntTy::Isize) => {
77                                 let val = ((val as i128) << 64) >> 64;
78                                 if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
79                                     continue;
80                                 }
81                             },
82                             ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
83                             _ => continue,
84                         }
85                         span_lint(
86                             cx,
87                             ENUM_CLIKE_UNPORTABLE_VARIANT,
88                             var.span,
89                             "Clike enum variant discriminant is not portable to 32-bit targets",
90                         );
91                     };
92                 }
93             }
94         }
95     }
96 }