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::*;
|
2018-07-19 07:53:23 +00:00
|
|
|
use rustc::{declare_lint, lint_array};
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2018-03-15 12:24:51 +00:00
|
|
|
use rustc::ty;
|
2017-07-31 10:37:38 +00:00
|
|
|
use rustc::ty::subst::Substs;
|
2018-03-13 10:38:11 +00:00
|
|
|
use syntax::ast::{IntTy, UintTy};
|
2018-05-30 08:15:50 +00:00
|
|
|
use crate::utils::span_lint;
|
|
|
|
use crate::consts::{Constant, miri_to_const};
|
2018-03-13 10:38:11 +00:00
|
|
|
use rustc::ty::util::IntTypeExt;
|
|
|
|
use rustc::mir::interpret::GlobalId;
|
2016-02-29 08:36:13 +00:00
|
|
|
|
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
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub ENUM_CLIKE_UNPORTABLE_VARIANT,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2016-08-06 08:18:36 +00:00
|
|
|
"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) {
|
2018-03-13 10:38:11 +00:00
|
|
|
if cx.tcx.data_layout.pointer_size.bits() != 64 {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 13:07:39 +00:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.node {
|
2016-02-29 08:36:13 +00:00
|
|
|
for var in &def.variants {
|
|
|
|
let variant = &var.node;
|
2018-05-22 13:45:14 +00:00
|
|
|
if let Some(ref anon_const) = variant.disr_expr {
|
2018-03-15 09:25:40 +00:00
|
|
|
let param_env = ty::ParamEnv::empty();
|
2018-05-22 13:45:14 +00:00
|
|
|
let did = cx.tcx.hir.body_owner_def_id(anon_const.body);
|
2017-07-31 10:37:38 +00:00
|
|
|
let substs = Substs::identity_for_item(cx.tcx.global_tcx(), did);
|
2018-03-13 10:38:11 +00:00
|
|
|
let instance = ty::Instance::new(did, substs);
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None
|
2016-02-29 08:36:13 +00:00
|
|
|
};
|
2018-03-13 10:38:11 +00:00
|
|
|
let constant = cx.tcx.const_eval(param_env.and(cid)).ok();
|
|
|
|
if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) {
|
|
|
|
let mut ty = cx.tcx.type_of(did);
|
|
|
|
if let ty::TyAdt(adt, _) = ty.sty {
|
|
|
|
if adt.is_enum() {
|
|
|
|
ty = adt.repr.discr_type().to_ty(cx.tcx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match ty.sty {
|
|
|
|
ty::TyInt(IntTy::Isize) => {
|
|
|
|
let val = ((val as i128) << 64) >> 64;
|
2018-03-15 15:07:15 +00:00
|
|
|
if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
|
2018-03-13 10:38:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 15:07:15 +00:00
|
|
|
ty::TyUint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
|
2018-03-13 10:38:11 +00:00
|
|
|
_ => continue,
|
|
|
|
}
|
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",
|
|
|
|
);
|
2018-03-13 10:38:11 +00:00
|
|
|
};
|
2016-02-29 08:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|