1 //! lint on C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`
4 use rustc::middle::const_val::ConstVal;
5 use rustc_const_math::*;
9 /// **What it does:** Checks for C-like enumerations that are
10 /// `repr(isize/usize)` and have values that don't fit into an `i32`.
12 /// **Why is this bad?** This will truncate the variant value on 32 bit
13 /// architectures, but works fine on 64 bit.
15 /// **Known problems:** None.
20 /// enum NonPortable {
21 /// X = 0x1_0000_0000,
26 pub ENUM_CLIKE_UNPORTABLE_VARIANT,
28 "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
31 pub struct UnportableVariant;
33 impl LintPass for UnportableVariant {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
40 #[allow(cast_possible_truncation, cast_sign_loss)]
41 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
42 if let ItemEnum(ref def, _) = item.node {
43 for var in &def.variants {
44 let variant = &var.node;
45 if let Some(body_id) = variant.disr_expr {
46 use rustc_const_eval::*;
47 let bad = match eval_const_expr_partial(cx.tcx,
48 &cx.tcx.map.body(body_id).value,
49 EvalHint::ExprTypeChecked,
51 Ok(ConstVal::Integral(Usize(Us64(i)))) => i as u32 as u64 != i,
52 Ok(ConstVal::Integral(Isize(Is64(i)))) => i as i32 as i64 != i,
57 ENUM_CLIKE_UNPORTABLE_VARIANT,
59 "Clike enum variant discriminant is not portable to 32-bit targets");