2017-08-09 07:30:56 +00:00
|
|
|
//! lint on C-like enums that are `repr(isize/usize)` and have values that
|
|
|
|
//! don't fit into an `i32`
|
2016-02-29 08:36:13 +00:00
|
|
|
|
|
|
|
use rustc::lint::*;
|
2016-03-31 15:05:43 +00:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
|
|
|
use rustc_const_math::*;
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2017-07-31 10:37:38 +00:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc::traits::Reveal;
|
|
|
|
use rustc::ty::subst::Substs;
|
2016-02-29 08:36:13 +00:00
|
|
|
use utils::span_lint;
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for C-like enumerations that are
|
|
|
|
/// `repr(isize/usize)` and have values that don't fit into an `i32`.
|
2016-02-29 08:36:13 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** This will truncate the variant value on 32 bit
|
|
|
|
/// architectures, but works fine on 64 bit.
|
2016-02-29 08:36:13 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** None.
|
2016-02-29 08:36:13 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// #[repr(usize)]
|
|
|
|
/// enum NonPortable {
|
|
|
|
/// X = 0x1_0000_0000,
|
|
|
|
/// Y = 0
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-02-29 08:36:13 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub ENUM_CLIKE_UNPORTABLE_VARIANT,
|
|
|
|
Warn,
|
|
|
|
"C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
|
2016-02-29 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
pub struct UnportableVariant;
|
2016-02-29 08:36:13 +00:00
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl LintPass for UnportableVariant {
|
2016-02-29 08:36:13 +00:00
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
|
2016-02-29 08:36:13 +00:00
|
|
|
#[allow(cast_possible_truncation, cast_sign_loss)]
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2016-02-29 08:36:13 +00:00
|
|
|
if let ItemEnum(ref def, _) = item.node {
|
|
|
|
for var in &def.variants {
|
|
|
|
let variant = &var.node;
|
2017-01-04 22:18:11 +00:00
|
|
|
if let Some(body_id) = variant.disr_expr {
|
2017-07-31 10:37:38 +00:00
|
|
|
let expr = &cx.tcx.hir.body(body_id).value;
|
|
|
|
let did = cx.tcx.hir.body_owner_def_id(body_id);
|
|
|
|
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
|
|
|
|
let substs = Substs::identity_for_item(cx.tcx.global_tcx(), did);
|
2017-09-05 09:33:04 +00:00
|
|
|
let bad = match cx.tcx
|
|
|
|
.at(expr.span)
|
|
|
|
.const_eval(param_env.and((did, substs)))
|
|
|
|
{
|
2017-09-13 13:34:04 +00:00
|
|
|
Ok(&ty::Const { val: ConstVal::Integral(Usize(Us64(i))), .. }) => u64::from(i as u32) != i,
|
|
|
|
Ok(&ty::Const { val: ConstVal::Integral(Isize(Is64(i))), .. }) => i64::from(i as i32) != i,
|
2016-02-29 08:36:13 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if bad {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
ENUM_CLIKE_UNPORTABLE_VARIANT,
|
|
|
|
var.span,
|
|
|
|
"Clike enum variant discriminant is not portable to 32-bit targets",
|
|
|
|
);
|
2016-02-29 08:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|