]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/consts.rs
Merge pull request #3294 from mikerite/fix-3276
[rust.git] / clippy_lints / src / consts.rs
index 0690d6934e51c8130de518a7275adefbfc84369d..5d509ef76f3f2c071ef37fda6d6188ef5017089b 100644 (file)
@@ -1,3 +1,13 @@
+// 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.
+
+
 #![allow(clippy::float_cmp)]
 
 use crate::rustc::lint::LateContext;
@@ -8,6 +18,7 @@
 use crate::rustc::ty::subst::{Subst, Substs};
 use std::cmp::Ordering::{self, Equal};
 use std::cmp::PartialOrd;
+use std::convert::TryInto;
 use std::hash::{Hash, Hasher};
 use std::mem;
 use std::rc::Rc;
@@ -219,6 +230,7 @@ pub fn expr(&mut self, e: &Expr) -> Option<Constant> {
         }
     }
 
+    #[allow(clippy::cast_possible_wrap)]
     fn constant_not(&self, o: &Constant, ty: ty::Ty<'_>) -> Option<Constant> {
         use self::Constant::*;
         match *o {
@@ -265,6 +277,8 @@ fn multi(&mut self, vec: &[Expr]) -> Option<Vec<Constant>> {
 
     /// lookup a possibly constant expression from a ExprKind::Path
     fn fetch_path(&mut self, qpath: &QPath, id: HirId) -> Option<Constant> {
+        use crate::rustc::mir::interpret::GlobalId;
+
         let def = self.tables.qpath_def(qpath, id);
         match def {
             Def::Const(def_id) | Def::AssociatedConst(def_id) => {
@@ -279,7 +293,7 @@ fn fetch_path(&mut self, qpath: &QPath, id: HirId) -> Option<Constant> {
                     instance,
                     promoted: None,
                 };
-                use crate::rustc::mir::interpret::GlobalId;
+                
                 let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?;
                 let ret = miri_to_const(self.tcx, result);
                 if ret.is_some() {
@@ -329,8 +343,12 @@ fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
                             BinOpKind::Mul => l.checked_mul(r).map(zext),
                             BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
                             BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
-                            BinOpKind::Shr => l.checked_shr(r as u128 as u32).map(zext),
-                            BinOpKind::Shl => l.checked_shl(r as u128 as u32).map(zext),
+                            BinOpKind::Shr => l.checked_shr(
+                                    r.try_into().expect("invalid shift")
+                                ).map(zext),
+                            BinOpKind::Shl => l.checked_shl(
+                                    r.try_into().expect("invalid shift")
+                                ).map(zext),
                             BinOpKind::BitXor => Some(zext(l ^ r)),
                             BinOpKind::BitOr => Some(zext(l | r)),
                             BinOpKind::BitAnd => Some(zext(l & r)),
@@ -350,8 +368,12 @@ fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
                             BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
                             BinOpKind::Div => l.checked_div(r).map(Constant::Int),
                             BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
-                            BinOpKind::Shr => l.checked_shr(r as u32).map(Constant::Int),
-                            BinOpKind::Shl => l.checked_shl(r as u32).map(Constant::Int),
+                            BinOpKind::Shr => l.checked_shr(
+                                    r.try_into().expect("shift too large")
+                                ).map(Constant::Int),
+                            BinOpKind::Shl => l.checked_shl(
+                                    r.try_into().expect("shift too large")
+                                ).map(Constant::Int),
                             BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
                             BinOpKind::BitOr => Some(Constant::Int(l | r)),
                             BinOpKind::BitAnd => Some(Constant::Int(l & r)),
@@ -409,26 +431,29 @@ fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
 }
 
 pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'tcx>) -> Option<Constant> {
-    use crate::rustc::mir::interpret::{Scalar, ScalarMaybeUndef, ConstValue};
+    use crate::rustc::mir::interpret::{Scalar, ConstValue};
     match result.val {
         ConstValue::Scalar(Scalar::Bits{ bits: b, ..}) => match result.ty.sty {
             ty::Bool => Some(Constant::Bool(b == 1)),
             ty::Uint(_) | ty::Int(_) => Some(Constant::Int(b)),
-            ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(b as u32))),
-            ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(b as u64))),
+            ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
+                b.try_into().expect("invalid f32 bit representation")
+            ))),
+            ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
+                b.try_into().expect("invalid f64 bit representation")
+            ))),
             // FIXME: implement other conversion
             _ => None,
         },
         ConstValue::ScalarPair(Scalar::Ptr(ptr),
-                               ScalarMaybeUndef::Scalar(
-                                Scalar::Bits { bits: n, .. })) => match result.ty.sty {
+                                Scalar::Bits { bits: n, .. }) => match result.ty.sty {
             ty::Ref(_, tam, _) => match tam.sty {
                 ty::Str => {
                     let alloc = tcx
                         .alloc_map
                         .lock()
                         .unwrap_memory(ptr.alloc_id);
-                    let offset = ptr.offset.bytes() as usize;
+                    let offset = ptr.offset.bytes().try_into().expect("too-large pointer offset");
                     let n = n as usize;
                     String::from_utf8(alloc.bytes[offset..(offset + n)].to_owned()).ok().map(Constant::Str)
                 },