Skip to content

Commit 6217671

Browse files
committed
Handle evaluating constant index expression
1 parent 2153abb commit 6217671

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

clippy_lints/src/consts.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
268268
}
269269
}
270270
},
271+
ExprKind::Index(ref arr, ref index) => self.index(arr, index),
271272
// TODO: add other expressions.
272273
_ => None,
273274
}
@@ -345,6 +346,20 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
345346
}
346347
}
347348

349+
fn index(&mut self, lhs: &'_ Expr<'_>, index: &'_ Expr<'_>) -> Option<Constant> {
350+
let lhs = self.expr(lhs);
351+
let index = self.expr(index);
352+
353+
match (lhs, index) {
354+
(Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec[index as usize] {
355+
Constant::F32(x) => Some(Constant::F32(x)),
356+
Constant::F64(x) => Some(Constant::F64(x)),
357+
_ => None,
358+
},
359+
_ => None,
360+
}
361+
}
362+
348363
/// A block can only yield a constant if it only has one constant expression.
349364
fn block(&mut self, block: &Block<'_>) -> Option<Constant> {
350365
if block.stmts.is_empty() {

0 commit comments

Comments
 (0)