about summary refs log tree commit diff
path: root/clippy_lints/src/enum_clike.rs
blob: 8610bffa6ac41367273cf93b22bbbecb3ad51e6e (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! lint on C-like enums that are `repr(isize/usize)` and have values that
//! don't fit into an `i32`

use crate::consts::{miri_to_const, Constant};
use crate::utils::span_lint;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::mir::interpret::GlobalId;
use rustc::ty;
use rustc::ty::subst::InternalSubsts;
use rustc::ty::util::IntTypeExt;
use rustc::{declare_tool_lint, lint_array};
use syntax::ast::{IntTy, UintTy};

/// **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_clippy_lint! {
    pub ENUM_CLIKE_UNPORTABLE_VARIANT,
    correctness,
    "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)
    }

    fn name(&self) -> &'static str {
        "UnportableVariant"
    }
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
    #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
        if cx.tcx.data_layout.pointer_size.bits() != 64 {
            return;
        }
        if let ItemKind::Enum(ref def, _) = item.node {
            for var in &def.variants {
                let variant = &var.node;
                if let Some(ref anon_const) = variant.disr_expr {
                    let param_env = ty::ParamEnv::empty();
                    let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
                    let substs = InternalSubsts::identity_for_item(cx.tcx.global_tcx(), def_id);
                    let instance = ty::Instance::new(def_id, substs);
                    let c_id = GlobalId {
                        instance,
                        promoted: None,
                    };
                    let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
                    if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, &c)) {
                        let mut ty = cx.tcx.type_of(def_id);
                        if let ty::Adt(adt, _) = ty.sty {
                            if adt.is_enum() {
                                ty = adt.repr.discr_type().to_ty(cx.tcx);
                            }
                        }
                        match ty.sty {
                            ty::Int(IntTy::Isize) => {
                                let val = ((val as i128) << 64) >> 64;
                                if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
                                    continue;
                                }
                            },
                            ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
                            _ => continue,
                        }
                        span_lint(
                            cx,
                            ENUM_CLIKE_UNPORTABLE_VARIANT,
                            var.span,
                            "Clike enum variant discriminant is not portable to 32-bit targets",
                        );
                    };
                }
            }
        }
    }
}