diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index c64c00134e8..c25200c3338 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -268,6 +268,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { } } }, + ExprKind::Index(ref arr, ref index) => self.index(arr, index), // TODO: add other expressions. _ => None, } @@ -345,6 +346,20 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { } } + fn index(&mut self, lhs: &'_ Expr<'_>, index: &'_ Expr<'_>) -> Option { + let lhs = self.expr(lhs); + let index = self.expr(index); + + match (lhs, index) { + (Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec[index as usize] { + Constant::F32(x) => Some(Constant::F32(x)), + Constant::F64(x) => Some(Constant::F64(x)), + _ => None, + }, + _ => None, + } + } + /// A block can only yield a constant if it only has one constant expression. fn block(&mut self, block: &Block<'_>) -> Option { if block.stmts.is_empty() {