]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_clike.rs
Auto merge of #3626 - phansch:rustfix_works, r=oli-obk
[rust.git] / clippy_lints / src / enum_clike.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! lint on C-like enums that are `repr(isize/usize)` and have values that
11 //! don't fit into an `i32`
12
13 use crate::consts::{miri_to_const, Constant};
14 use crate::utils::span_lint;
15 use rustc::hir::*;
16 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
17 use rustc::mir::interpret::GlobalId;
18 use rustc::ty;
19 use rustc::ty::subst::Substs;
20 use rustc::ty::util::IntTypeExt;
21 use rustc::{declare_tool_lint, lint_array};
22 use syntax::ast::{IntTy, UintTy};
23
24 /// **What it does:** Checks for C-like enumerations that are
25 /// `repr(isize/usize)` and have values that don't fit into an `i32`.
26 ///
27 /// **Why is this bad?** This will truncate the variant value on 32 bit
28 /// architectures, but works fine on 64 bit.
29 ///
30 /// **Known problems:** None.
31 ///
32 /// **Example:**
33 /// ```rust
34 /// #[repr(usize)]
35 /// enum NonPortable {
36 ///     X = 0x1_0000_0000,
37 ///     Y = 0,
38 /// }
39 /// ```
40 declare_clippy_lint! {
41     pub ENUM_CLIKE_UNPORTABLE_VARIANT,
42     correctness,
43     "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
44 }
45
46 pub struct UnportableVariant;
47
48 impl LintPass for UnportableVariant {
49     fn get_lints(&self) -> LintArray {
50         lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
51     }
52 }
53
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
55     #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
56     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
57         if cx.tcx.data_layout.pointer_size.bits() != 64 {
58             return;
59         }
60         if let ItemKind::Enum(ref def, _) = item.node {
61             for var in &def.variants {
62                 let variant = &var.node;
63                 if let Some(ref anon_const) = variant.disr_expr {
64                     let param_env = ty::ParamEnv::empty();
65                     let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
66                     let substs = Substs::identity_for_item(cx.tcx.global_tcx(), def_id);
67                     let instance = ty::Instance::new(def_id, substs);
68                     let c_id = GlobalId {
69                         instance,
70                         promoted: None,
71                     };
72                     let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
73                     if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, &c)) {
74                         let mut ty = cx.tcx.type_of(def_id);
75                         if let ty::Adt(adt, _) = ty.sty {
76                             if adt.is_enum() {
77                                 ty = adt.repr.discr_type().to_ty(cx.tcx);
78                             }
79                         }
80                         match ty.sty {
81                             ty::Int(IntTy::Isize) => {
82                                 let val = ((val as i128) << 64) >> 64;
83                                 if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
84                                     continue;
85                                 }
86                             },
87                             ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
88                             _ => continue,
89                         }
90                         span_lint(
91                             cx,
92                             ENUM_CLIKE_UNPORTABLE_VARIANT,
93                             var.span,
94                             "Clike enum variant discriminant is not portable to 32-bit targets",
95                         );
96                     };
97                 }
98             }
99         }
100     }
101 }