rust/clippy_lints/src/enum_clike.rs

102 lines
4.0 KiB
Rust
Raw Normal View History

2018-10-06 11:18:06 -05:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
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`
2018-11-27 14:14:15 -06:00
use crate::consts::{miri_to_const, Constant};
use crate::rustc::hir::*;
2018-11-27 14:14:15 -06:00
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::mir::interpret::GlobalId;
use crate::rustc::ty;
use crate::rustc::ty::subst::Substs;
2018-11-27 14:14:15 -06:00
use crate::rustc::ty::util::IntTypeExt;
use crate::rustc::{declare_tool_lint, lint_array};
use crate::syntax::ast::{IntTy, UintTy};
2018-05-30 03:15:50 -05:00
use crate::utils::span_lint;
/// **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.
///
2016-07-15 17:25:44 -05:00
/// **Example:**
/// ```rust
/// #[repr(usize)]
/// enum NonPortable {
/// X = 0x1_0000_0000,
2018-11-27 14:14:15 -06:00
/// Y = 0,
2016-07-15 17:25:44 -05:00
/// }
/// ```
2018-03-28 08:24:26 -05:00
declare_clippy_lint! {
pub ENUM_CLIKE_UNPORTABLE_VARIANT,
2018-03-28 08:24:26 -05:00
correctness,
"C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`"
}
2016-06-10 09:17:20 -05:00
pub struct UnportableVariant;
2016-06-10 09:17:20 -05:00
impl LintPass for UnportableVariant {
fn get_lints(&self) -> LintArray {
lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnportableVariant {
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
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 {
for var in &def.variants {
let variant = &var.node;
if let Some(ref anon_const) = variant.disr_expr {
let param_env = ty::ParamEnv::empty();
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
let substs = Substs::identity_for_item(cx.tcx.global_tcx(), def_id);
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,
};
let constant = cx.tcx.const_eval(param_env.and(c_id)).ok();
2018-03-13 05:38:11 -05:00
if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) {
let mut ty = cx.tcx.type_of(def_id);
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 {
ty::Int(IntTy::Isize) => {
2018-03-13 05:38:11 -05:00
let val = ((val as i128) << 64) >> 64;
if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) {
2018-03-13 05:38:11 -05:00
continue;
}
2018-11-27 14:14:15 -06: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
};
}
}
}
}
}