1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! lint on C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`

use rustc::lint::*;
use rustc::middle::const_val::ConstVal;
use rustc_const_math::*;
use rustc::hir::*;
use utils::span_lint;

/// **What it does:** Checks for C-like enumerations that are
/// `repr(isize/usize)` and have values that don't fit into an `i32`.
///
/// **Why is this bad?** This will truncate the variant value on 32 bit
/// architectures, but works fine on 64 bit.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// #[repr(usize)]
/// enum NonPortable {
///     X = 0x1_0000_0000,
///     Y = 0
/// }
/// ```
declare_lint! {
    pub ENUM_CLIKE_UNPORTABLE_VARIANT,
    Warn,
    "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
}

pub struct UnportableVariant;

impl LintPass for UnportableVariant {
    fn get_lints(&self) -> LintArray {
        lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
    }
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
    #[allow(cast_possible_truncation, cast_sign_loss)]
    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
        if let ItemEnum(ref def, _) = item.node {
            for var in &def.variants {
                let variant = &var.node;
                if let Some(body_id) = variant.disr_expr {
                    use rustc_const_eval::*;
                    let bad = match eval_const_expr_partial(cx.tcx,
                                                            &cx.tcx.map.body(body_id).value,
                                                            EvalHint::ExprTypeChecked,
                                                            None) {
                        Ok(ConstVal::Integral(Usize(Us64(i)))) => i as u32 as u64 != i,
                        Ok(ConstVal::Integral(Isize(Is64(i)))) => i as i32 as i64 != i,
                        _ => false,
                    };
                    if bad {
                        span_lint(cx,
                                  ENUM_CLIKE_UNPORTABLE_VARIANT,
                                  var.span,
                                  "Clike enum variant discriminant is not portable to 32-bit targets");
                    }
                }
            }
        }
    }
}