]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Reintroduce `extern crate` for non-Cargo dependencies.
[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::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use crate::rustc::{declare_tool_lint, lint_array};
6 use crate::rustc::hir::*;
7 use crate::rustc::ty;
8 use crate::rustc::ty::subst::Substs;
9 use crate::syntax::ast::{IntTy, UintTy};
10 use crate::utils::span_lint;
11 use crate::consts::{Constant, miri_to_const};
12 use crate::rustc::ty::util::IntTypeExt;
13 use crate::rustc::mir::interpret::GlobalId;
14
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 /// #[repr(usize)]
26 /// enum NonPortable {
27 ///     X = 0x1_0000_0000,
28 ///     Y = 0
29 /// }
30 /// ```
31 declare_clippy_lint! {
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
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
46     #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
47     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
48         if cx.tcx.data_layout.pointer_size.bits() != 64 {
49             return;
50         }
51         if let ItemKind::Enum(ref def, _) = item.node {
52             for var in &def.variants {
53                 let variant = &var.node;
54                 if let Some(ref anon_const) = variant.disr_expr {
55                     let param_env = ty::ParamEnv::empty();
56                     let did = cx.tcx.hir.body_owner_def_id(anon_const.body);
57                     let substs = Substs::identity_for_item(cx.tcx.global_tcx(), did);
58                     let instance = ty::Instance::new(did, substs);
59                     let cid = GlobalId {
60                         instance,
61                         promoted: None
62                     };
63                     let constant = cx.tcx.const_eval(param_env.and(cid)).ok();
64                     if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) {
65                         let mut ty = cx.tcx.type_of(did);
66                         if let ty::Adt(adt, _) = ty.sty {
67                             if adt.is_enum() {
68                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
69                             }
70                         }
71                         match ty.sty {
72                             ty::Int(IntTy::Isize) => {
73                                 let val = ((val as i128) << 64) >> 64;
74                                 if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
75                                     continue;
76                                 }
77                             }
78                             ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
79                             _ => continue,
80                         }
81                         span_lint(
82                             cx,
83                             ENUM_CLIKE_UNPORTABLE_VARIANT,
84                             var.span,
85                             "Clike enum variant discriminant is not portable to 32-bit targets",
86                         );
87                     };
88                 }
89             }
90         }
91     }
92 }