X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fenum_clike.rs;h=c4b368a6e2e8054ba32292d6ec373696142f2c8e;hb=e5a5b0a0774625eebbe7b29c67b49dc6431544d1;hp=cf921b6b94c2bdd6ee0e4d35326a7dda1e4f432a;hpb=26602ddff475ef75ed94803b05217a761a7f5523;p=rust.git diff --git a/clippy_lints/src/enum_clike.rs b/clippy_lints/src/enum_clike.rs index cf921b6b94c..c4b368a6e2e 100644 --- a/clippy_lints/src/enum_clike.rs +++ b/clippy_lints/src/enum_clike.rs @@ -1,55 +1,43 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - //! lint on C-like enums that are `repr(isize/usize)` and have values that //! don't fit into an `i32` use crate::consts::{miri_to_const, Constant}; -use crate::rustc::hir::*; -use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use crate::rustc::mir::interpret::GlobalId; -use crate::rustc::ty; -use crate::rustc::ty::subst::Substs; -use crate::rustc::ty::util::IntTypeExt; -use crate::rustc::{declare_tool_lint, lint_array}; -use crate::syntax::ast::{IntTy, UintTy}; use crate::utils::span_lint; +use rustc::declare_lint_pass; +use rustc::hir::*; +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; +use rustc::mir::interpret::GlobalId; +use rustc::ty; +use rustc::ty::subst::InternalSubsts; +use rustc::ty::util::IntTypeExt; +use rustc_session::declare_tool_lint; +use std::convert::TryFrom; +use syntax::ast::{IntTy, UintTy}; -/// **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, -/// } -/// ``` declare_clippy_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. + /// + /// **Example:** + /// ```rust + /// # #[cfg(target_pointer_width = "64")] + /// #[repr(usize)] + /// enum NonPortable { + /// X = 0x1_0000_0000, + /// Y = 0, + /// } + /// ``` pub ENUM_CLIKE_UNPORTABLE_VARIANT, correctness, "C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`" } -pub struct UnportableVariant; - -impl LintPass for UnportableVariant { - fn get_lints(&self) -> LintArray { - lint_array!(ENUM_CLIKE_UNPORTABLE_VARIANT) - } -} +declare_lint_pass!(UnportableVariant => [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)] @@ -57,30 +45,29 @@ fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { if cx.tcx.data_layout.pointer_size.bits() != 64 { return; } - if let ItemKind::Enum(ref def, _) = item.node { + if let ItemKind::Enum(def, _) = &item.kind { for var in &def.variants { - let variant = &var.node; - if let Some(ref anon_const) = variant.disr_expr { + if let Some(anon_const) = &var.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 def_id = cx.tcx.hir().body_owner_def_id(anon_const.body); + let substs = InternalSubsts::identity_for_item(cx.tcx, def_id); let instance = ty::Instance::new(def_id, substs); let c_id = GlobalId { instance, promoted: None, }; let constant = cx.tcx.const_eval(param_env.and(c_id)).ok(); - if let Some(Constant::Int(val)) = constant.and_then(|c| miri_to_const(cx.tcx, c)) { + if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) { let mut ty = cx.tcx.type_of(def_id); - if let ty::Adt(adt, _) = ty.sty { + if let ty::Adt(adt, _) = ty.kind { if adt.is_enum() { ty = adt.repr.discr_type().to_ty(cx.tcx); } } - match ty.sty { + match ty.kind { ty::Int(IntTy::Isize) => { let val = ((val as i128) << 64) >> 64; - if val <= i128::from(i32::max_value()) && val >= i128::from(i32::min_value()) { + if i32::try_from(val).is_ok() { continue; } },