]> git.lizzy.rs Git - rust.git/commitdiff
Fix ice
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 3 Mar 2017 13:46:33 +0000 (14:46 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 3 Mar 2017 13:46:33 +0000 (14:46 +0100)
clippy_lints/src/consts.rs
tests/run-pass/ice-1588.rs [new file with mode: 0644]

index b38b906b4a65659befc57f17196cdf433cb29490..3b624ff3d6cde0c2d3c12d5bab5bd2a395a991f9 100644 (file)
@@ -53,21 +53,6 @@ pub enum Constant {
     Tuple(Vec<Constant>),
 }
 
-impl Constant {
-    /// Convert to `u64` if possible.
-    ///
-    /// # panics
-    ///
-    /// If the constant could not be converted to `u64` losslessly.
-    fn as_u64(&self) -> u64 {
-        if let Constant::Int(val) = *self {
-            val.to_u64().expect("negative constant can't be casted to `u64`")
-        } else {
-            panic!("Could not convert a `{:?}` to `u64`", self);
-        }
-    }
-}
-
 impl PartialEq for Constant {
     fn eq(&self, other: &Constant) -> bool {
         match (self, other) {
@@ -266,11 +251,12 @@ fn expr(&mut self, e: &Expr) -> Option<Constant> {
             ExprLit(ref lit) => Some(lit_to_constant(&lit.node, self.tcx, self.tables.expr_ty(e))),
             ExprArray(ref vec) => self.multi(vec).map(Constant::Vec),
             ExprTup(ref tup) => self.multi(tup).map(Constant::Tuple),
-            ExprRepeat(ref value, number_id) => {
-                let val = &self.tcx.hir.body(number_id).value;
-                self.binop_apply(value,
-                                 val,
-                                 |v, n| Some(Constant::Repeat(Box::new(v), n.as_u64() as usize)))
+            ExprRepeat(ref value, _) => {
+                let n = match self.tables.expr_ty(e).sty {
+                    ty::TyArray(_, n) => n,
+                    _ => span_bug!(e.span, "typeck error"),
+                };
+                self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
             },
             ExprUnary(op, ref operand) => {
                 self.expr(operand).and_then(|o| match op {
@@ -375,15 +361,4 @@ fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
             _ => None,
         }
     }
-
-
-    fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
-        where F: Fn(Constant, Constant) -> Option<Constant>
-    {
-        if let (Some(lc), Some(rc)) = (self.expr(left), self.expr(right)) {
-            op(lc, rc)
-        } else {
-            None
-        }
-    }
 }
diff --git a/tests/run-pass/ice-1588.rs b/tests/run-pass/ice-1588.rs
new file mode 100644 (file)
index 0000000..d53d3a1
--- /dev/null
@@ -0,0 +1,13 @@
+#![feature(plugin)]
+#![plugin(clippy)]
+#![allow(clippy)]
+
+fn main() {
+    match 1 {
+        1 => {}
+        2 => {
+            [0; 1];
+        }
+        _ => {}
+    }
+}