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
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
use clippy_utils::consts::{miri_to_const, Constant};
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::util::IntTypeExt;
|
2021-01-30 17:06:34 +00:00
|
|
|
use rustc_middle::ty::{self, IntTy, UintTy};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2016-02-29 08:36:13 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for C-like enumerations that are
|
2019-03-05 16:50:33 +00:00
|
|
|
/// `repr(isize/usize)` and have values that don't fit into an `i32`.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This will truncate the variant value on 32 bit
|
2019-03-05 16:50:33 +00:00
|
|
|
/// architectures, but works fine on 64 bit.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-10-25 15:17:13 +00:00
|
|
|
/// # #[cfg(target_pointer_width = "64")]
|
2019-03-05 16:50:33 +00:00
|
|
|
/// #[repr(usize)]
|
|
|
|
/// enum NonPortable {
|
|
|
|
/// X = 0x1_0000_0000,
|
|
|
|
/// Y = 0,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(UnportableVariant => [ENUM_CLIKE_UNPORTABLE_VARIANT]);
|
2016-02-29 08:36:13 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
|
2022-05-21 11:24:00 +00:00
|
|
|
#[expect(clippy::cast_possible_wrap)]
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2018-03-13 10:38:11 +00:00
|
|
|
if cx.tcx.data_layout.pointer_size.bits() != 64 {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ItemKind::Enum(def, _) = &item.kind {
|
2019-12-22 14:56:34 +00:00
|
|
|
for var in def.variants {
|
2019-08-15 07:59:08 +00:00
|
|
|
if let Some(anon_const) = &var.disr_expr {
|
2018-12-08 00:56:03 +00:00
|
|
|
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
|
2020-04-11 08:01:23 +00:00
|
|
|
let mut ty = cx.tcx.type_of(def_id.to_def_id());
|
2020-02-18 22:33:19 +00:00
|
|
|
let constant = cx
|
|
|
|
.tcx
|
2022-06-02 13:15:01 +00:00
|
|
|
.const_eval_poly(def_id.to_def_id())
|
2020-02-18 22:33:19 +00:00
|
|
|
.ok()
|
2022-06-03 18:42:35 +00:00
|
|
|
.map(|val| rustc_middle::mir::ConstantKind::from_value(val, ty));
|
2022-02-16 09:56:01 +00:00
|
|
|
if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Adt(adt, _) = ty.kind() {
|
2018-03-13 10:38:11 +00:00
|
|
|
if adt.is_enum() {
|
2022-03-04 20:28:41 +00:00
|
|
|
ty = adt.repr().discr_type().to_ty(cx.tcx);
|
2018-03-13 10:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 22:18:29 +00:00
|
|
|
match ty.kind() {
|
2018-08-22 21:34:52 +00:00
|
|
|
ty::Int(IntTy::Isize) => {
|
2018-03-13 10:38:11 +00:00
|
|
|
let val = ((val as i128) << 64) >> 64;
|
2019-05-12 19:53:28 +00:00
|
|
|
if i32::try_from(val).is_ok() {
|
2018-03-13 10:38:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2020-06-02 07:59:11 +00:00
|
|
|
ty::Uint(UintTy::Usize) if val > u128::from(u32::MAX) => {},
|
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,
|
2020-08-28 14:10:16 +00:00
|
|
|
"C-like enum variant discriminant is not portable to 32-bit targets",
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2018-03-13 10:38:11 +00:00
|
|
|
};
|
2016-02-29 08:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|