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:** Lints on C-like enumerations that are `repr(isize/usize)` and have values
10 /// that don't fit into an `i32`.
12 /// **Why is this bad?** This will truncate the variant value on 32 bit architectures, but works
15 /// **Known problems:** None
20 /// enum NonPortable {
21 /// X = 0x1_0000_0000,
26 pub ENUM_CLIKE_UNPORTABLE_VARIANT, Warn,
27 "finds C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
30 pub struct UnportableVariant;
32 impl LintPass for UnportableVariant {
33 fn get_lints(&self) -> LintArray {
34 lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
38 impl LateLintPass for UnportableVariant {
39 #[allow(cast_possible_truncation, cast_sign_loss)]
40 fn check_item(&mut self, cx: &LateContext, item: &Item) {
41 if let ItemEnum(ref def, _) = item.node {
42 for var in &def.variants {
43 let variant = &var.node;
44 if let Some(ref disr) = variant.disr_expr {
45 use rustc_const_eval::*;
46 let bad = match eval_const_expr_partial(cx.tcx, &**disr, EvalHint::ExprTypeChecked, None) {
47 Ok(ConstVal::Integral(Usize(Us64(i)))) => i as u32 as u64 != i,
48 Ok(ConstVal::Integral(Isize(Is64(i)))) => i as i32 as i64 != i,
53 ENUM_CLIKE_UNPORTABLE_VARIANT,
55 "Clike enum variant discriminant is not portable to 32-bit targets");