diff options
| author | Marcin Serwin <toxyxer@gmail.com> | 2020-03-19 16:54:19 +0100 |
|---|---|---|
| committer | Marcin Serwin <toxyxer@gmail.com> | 2020-04-09 08:05:51 +0200 |
| commit | 621767136eae13b2d9ebd462d3b33d6f1ad6b4e9 (patch) | |
| tree | 76b127b2bab092405d63ef8c23e947894813634c | |
| parent | 2153abb4124fd3dca018d4adb4e79693f1a9fedd (diff) | |
| download | rust-621767136eae13b2d9ebd462d3b33d6f1ad6b4e9.tar.gz rust-621767136eae13b2d9ebd462d3b33d6f1ad6b4e9.zip | |
Handle evaluating constant index expression
| -rw-r--r-- | clippy_lints/src/consts.rs | 15 |
1 files changed, 15 insertions, 0 deletions
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<Constant> { + 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<Constant> { if block.stmts.is_empty() { |
