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
|
|
|
|
2021-05-27 08:49:37 -05:00
|
|
|
use clippy_utils::consts::{miri_to_const, Constant};
|
2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty::util::IntTypeExt;
|
2021-01-30 08:51:16 -06:00
|
|
|
use rustc_middle::ty::{self, IntTy, UintTy};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-05-12 14:53:28 -05:00
|
|
|
use std::convert::TryFrom;
|
2016-02-29 02:36:13 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for C-like enumerations that are
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `repr(isize/usize)` and have values that don't fit into an `i32`.
|
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This will truncate the variant value on 32 bit
|
2019-03-05 10:50:33 -06:00
|
|
|
/// architectures, but works fine on 64 bit.
|
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-10-25 10:17:13 -05:00
|
|
|
/// # #[cfg(target_pointer_width = "64")]
|
2019-03-05 10:50:33 -06:00
|
|
|
/// #[repr(usize)]
|
|
|
|
/// enum NonPortable {
|
|
|
|
/// X = 0x1_0000_0000,
|
|
|
|
/// Y = 0,
|
|
|
|
/// }
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.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
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
|
2018-10-09 00:34:10 -05:00
|
|
|
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2018-03-13 05:38:11 -05:00
|
|
|
if cx.tcx.data_layout.pointer_size.bits() != 64 {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Enum(def, _) = &item.kind {
|
2019-12-22 08:56:34 -06:00
|
|
|
for var in def.variants {
|
2019-08-15 02:59:08 -05:00
|
|
|
if let Some(anon_const) = &var.disr_expr {
|
2018-12-07 18:56:03 -06:00
|
|
|
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
|
2020-04-11 03:01:23 -05:00
|
|
|
let mut ty = cx.tcx.type_of(def_id.to_def_id());
|
2020-02-18 16:33:19 -06:00
|
|
|
let constant = cx
|
|
|
|
.tcx
|
2020-04-11 03:01:23 -05:00
|
|
|
.const_eval_poly(def_id.to_def_id())
|
2020-02-18 16:33:19 -06:00
|
|
|
.ok()
|
2020-03-30 04:02:14 -05:00
|
|
|
.map(|val| rustc_middle::ty::Const::from_value(cx.tcx, val, ty));
|
2019-05-26 09:47:26 -05:00
|
|
|
if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) {
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(adt, _) = ty.kind() {
|
2018-03-13 05:38:11 -05:00
|
|
|
if adt.is_enum() {
|
|
|
|
ty = adt.repr.discr_type().to_ty(cx.tcx);
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 17:18:29 -05:00
|
|
|
match ty.kind() {
|
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
|
|
|
},
|
2020-06-02 02:59:11 -05:00
|
|
|
ty::Uint(UintTy::Usize) if val > u128::from(u32::MAX) => {},
|
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,
|
2020-08-28 09:10:16 -05:00
|
|
|
"C-like enum variant discriminant is not portable to 32-bit targets",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2018-03-13 05:38:11 -05:00
|
|
|
};
|
2016-02-29 02:36:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|