diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index b38b906b4a6..3b624ff3d6c 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -53,21 +53,6 @@ pub enum Constant { Tuple(Vec), } -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 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { 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 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { _ => None, } } - - - fn binop_apply(&mut self, left: &Expr, right: &Expr, op: F) -> Option - where F: Fn(Constant, Constant) -> Option - { - 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 index 00000000000..d53d3a1cc75 --- /dev/null +++ b/tests/run-pass/ice-1588.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(clippy)] +#![allow(clippy)] + +fn main() { + match 1 { + 1 => {} + 2 => { + [0; 1]; + } + _ => {} + } +}