]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Merge remote-tracking branch 'origin/rust-1.31.0' into HEAD
[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 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 declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);
38
39 impl<'a, 'tcx> LateLintPass<'a, '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<'a, 'tcx>, item: &'tcx Item) {
42         if cx.tcx.data_layout.pointer_size.bits() != 64 {
43             return;
44         }
45         if let ItemKind::Enum(ref def, _) = item.node {
46             for var in &def.variants {
47                 let variant = &var.node;
48                 if let Some(ref anon_const) = variant.disr_expr {
49                     let param_env = ty::ParamEnv::empty();
50                     let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
51                     let substs = InternalSubsts::identity_for_item(cx.tcx.global_tcx(), def_id);
52                     let instance = ty::Instance::new(def_id, substs);
53                     let c_id = GlobalId {
54                         instance,
55                         promoted: None,
56                     };
57                     let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
58                     if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, &c)) {
59                         let mut ty = cx.tcx.type_of(def_id);
60                         if let ty::Adt(adt, _) = ty.sty {
61                             if adt.is_enum() {
62                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
63                             }
64                         }
65                         match ty.sty {
66                             ty::Int(IntTy::Isize) => {
67                                 let val = ((val as i128) << 64) >> 64;
68                                 if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
69                                     continue;
70                                 }
71                             },
72                             ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
73                             _ => continue,
74                         }
75                         span_lint(
76                             cx,
77                             ENUM_CLIKE_UNPORTABLE_VARIANT,
78                             var.span,
79                             "Clike enum variant discriminant is not portable to 32-bit targets",
80                         );
81                     };
82                 }
83             }
84         }
85     }
86 }