2017-08-09 02:30:56 -05:00
|
|
|
//! lint on C-like enums that are `repr(isize/usize)` and have values that
|
|
|
|
//! don't fit into an `i32`
|
2016-02-29 02:36:13 -06:00
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::consts::{miri_to_const, Constant};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::span_lint;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::mir::interpret::GlobalId;
|
|
|
|
use rustc::ty;
|
2019-02-27 00:20:49 -06:00
|
|
|
use rustc::ty::subst::InternalSubsts;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::ty::util::IntTypeExt;
|
2019-04-08 15:43:55 -05:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2019-05-12 14:53:28 -05:00
|
|
|
use std::convert::TryFrom;
|
2018-12-29 09:04:45 -06:00
|
|
|
use syntax::ast::{IntTy, UintTy};
|
2016-02-29 02:36:13 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **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,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-08-06 03:18:36 -05:00
|
|
|
pub ENUM_CLIKE_UNPORTABLE_VARIANT,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2016-08-06 03:18:36 -05:00
|
|
|
"C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
|
2016-02-29 02:36:13 -06:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);
|
2016-02-29 02:36:13 -06:00
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
|
2018-10-09 00:34:10 -05:00
|
|
|
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2018-03-13 05:38:11 -05:00
|
|
|
if cx.tcx.data_layout.pointer_size.bits() != 64 {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 08:07:39 -05:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.node {
|
2016-02-29 02:36:13 -06:00
|
|
|
for var in &def.variants {
|
|
|
|
let variant = &var.node;
|
2018-05-22 08:45:14 -05:00
|
|
|
if let Some(ref anon_const) = variant.disr_expr {
|
2018-03-15 04:25:40 -05:00
|
|
|
let param_env = ty::ParamEnv::empty();
|
2018-12-07 18:56:03 -06:00
|
|
|
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
|
2019-02-27 00:20:49 -06:00
|
|
|
let substs = InternalSubsts::identity_for_item(cx.tcx.global_tcx(), def_id);
|
2018-10-11 17:18:58 -05:00
|
|
|
let instance = ty::Instance::new(def_id, substs);
|
|
|
|
let c_id = GlobalId {
|
2018-03-13 05:38:11 -05:00
|
|
|
instance,
|
2018-11-27 14:14:15 -06:00
|
|
|
promoted: None,
|
2016-02-29 02:36:13 -06:00
|
|
|
};
|
2018-10-11 17:18:58 -05:00
|
|
|
let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
|
2019-05-26 09:47:26 -05:00
|
|
|
if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) {
|
2018-10-11 17:18:58 -05:00
|
|
|
let mut ty = cx.tcx.type_of(def_id);
|
2018-08-22 16:34:52 -05:00
|
|
|
if let ty::Adt(adt, _) = ty.sty {
|
2018-03-13 05:38:11 -05:00
|
|
|
if adt.is_enum() {
|
|
|
|
ty = adt.repr.discr_type().to_ty(cx.tcx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match ty.sty {
|
2018-08-22 16:34:52 -05:00
|
|
|
ty::Int(IntTy::Isize) => {
|
2018-03-13 05:38:11 -05:00
|
|
|
let val = ((val as i128) << 64) >> 64;
|
2019-05-12 14:53:28 -05:00
|
|
|
if i32::try_from(val).is_ok() {
|
2018-03-13 05:38:11 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
2018-08-22 16:34:52 -05:00
|
|
|
ty::Uint(UintTy::Usize) if val > u128::from(u32::max_value()) => {},
|
2018-03-13 05:38:11 -05:00
|
|
|
_ => continue,
|
|
|
|
}
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
ENUM_CLIKE_UNPORTABLE_VARIANT,
|
|
|
|
var.span,
|
|
|
|
"Clike enum variant discriminant is not portable to 32-bit targets",
|
|
|
|
);
|
2018-03-13 05:38:11 -05:00
|
|
|
};
|
2016-02-29 02:36:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|