Skip to content

Commit 2deb61a

Browse files
committed
Fix consts.rs
1 parent 814c7ad commit 2deb61a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

clippy_lints/src/consts.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::float_cmp)]
22

3-
use crate::utils::{clip, sext, unsext};
3+
use crate::utils::{higher, clip, sext, unsext};
44
use if_chain::if_chain;
55
use rustc::hir::def::{DefKind, Res};
66
use rustc::hir::*;
@@ -15,7 +15,6 @@ use std::convert::TryFrom;
1515
use std::convert::TryInto;
1616
use std::hash::{Hash, Hasher};
1717
use syntax::ast::{FloatTy, LitKind};
18-
use syntax::ptr::P;
1918
use syntax_pos::symbol::{LocalInternedString, Symbol};
2019

2120
/// A `LitKind`-like enum to fold constant `Expr`s into.
@@ -222,10 +221,12 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
222221
impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
223222
/// Simple constant folding: Insert an expression, get a constant or none.
224223
pub fn expr(&mut self, e: &Expr) -> Option<Constant> {
224+
if let Some((ref cond, ref then, otherwise)) = higher::if_block(&e) {
225+
return self.ifthenelse(cond, then, otherwise);
226+
}
225227
match e.node {
226228
ExprKind::Path(ref qpath) => self.fetch_path(qpath, e.hir_id),
227229
ExprKind::Block(ref block, _) => self.block(block),
228-
ExprKind::If(ref cond, ref then, ref otherwise) => self.ifthenelse(cond, then, otherwise),
229230
ExprKind::Lit(ref lit) => Some(lit_to_constant(&lit.node, self.tables.expr_ty(e))),
230231
ExprKind::Array(ref vec) => self.multi(vec).map(Constant::Vec),
231232
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
@@ -358,10 +359,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
358359
}
359360
}
360361

361-
fn ifthenelse(&mut self, cond: &Expr, then: &P<Expr>, otherwise: &Option<P<Expr>>) -> Option<Constant> {
362+
fn ifthenelse(&mut self, cond: &Expr, then: &Expr, otherwise: Option<&Expr>) -> Option<Constant> {
362363
if let Some(Constant::Bool(b)) = self.expr(cond) {
363364
if b {
364-
self.expr(&**then)
365+
self.expr(&*then)
365366
} else {
366367
otherwise.as_ref().and_then(|expr| self.expr(expr))
367368
}

0 commit comments

Comments
 (0)