]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Auto merge of #4088 - pJunger:check1, r=oli-obk
[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_lint_pass, declare_tool_lint};
13 use std::convert::TryFrom;
14 use syntax::ast::{IntTy, UintTy};
15
16 declare_clippy_lint! {
17     /// **What it does:** Checks for C-like enumerations that are
18     /// `repr(isize/usize)` and have values that don't fit into an `i32`.
19     ///
20     /// **Why is this bad?** This will truncate the variant value on 32 bit
21     /// architectures, but works fine on 64 bit.
22     ///
23     /// **Known problems:** None.
24     ///
25     /// **Example:**
26     /// ```rust
27     /// #[repr(usize)]
28     /// enum NonPortable {
29     ///     X = 0x1_0000_0000,
30     ///     Y = 0,
31     /// }
32     /// ```
33     pub ENUM_CLIKE_UNPORTABLE_VARIANT,
34     correctness,
35     "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
36 }
37
38 declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
41     #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
42     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
43         if cx.tcx.data_layout.pointer_size.bits() != 64 {
44             return;
45         }
46         if let ItemKind::Enum(ref def, _) = item.node {
47             for var in &def.variants {
48                 let variant = &var.node;
49                 if let Some(ref anon_const) = variant.disr_expr {
50                     let param_env = ty::ParamEnv::empty();
51                     let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
52                     let substs = InternalSubsts::identity_for_item(cx.tcx.global_tcx(), def_id);
53                     let instance = ty::Instance::new(def_id, substs);
54                     let c_id = GlobalId {
55                         instance,
56                         promoted: None,
57                     };
58                     let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
59                     if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, &c)) {
60                         let mut ty = cx.tcx.type_of(def_id);
61                         if let ty::Adt(adt, _) = ty.sty {
62                             if adt.is_enum() {
63                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
64                             }
65                         }
66                         match ty.sty {
67                             ty::Int(IntTy::Isize) => {
68                                 let val = ((val as i128) << 64) >> 64;
69                                 if i32::try_from(val).is_ok() {
70                                     continue;
71                                 }
72                             },
73                             ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
74                             _ => continue,
75                         }
76                         span_lint(
77                             cx,
78                             ENUM_CLIKE_UNPORTABLE_VARIANT,
79                             var.span,
80                             "Clike enum variant discriminant is not portable to 32-bit targets",
81                         );
82                     };
83                 }
84             }
85         }
86     }
87 }