From 288ea799637dee4b2250069af41b44a8facaa52f Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 24 Mar 2016 09:37:16 +0100 Subject: [PATCH] treat macros as terminals to prevent `cfg!` from giving platform specific hints --- src/booleans.rs | 37 ++++++++++++++++++---------------- tests/compile-fail/booleans.rs | 3 +++ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/booleans.rs b/src/booleans.rs index e9197cf85c6..1b4d661da84 100644 --- a/src/booleans.rs +++ b/src/booleans.rs @@ -54,23 +54,26 @@ fn extract(&mut self, op: BinOp_, a: &[&'v Expr], mut v: Vec) -> Result Result { - match e.node { - ExprUnary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)), - ExprBinary(binop, ref lhs, ref rhs) => { - match binop.node { - BiOr => return Ok(Bool::Or(self.extract(BiOr, &[lhs, rhs], Vec::new())?)), - BiAnd => return Ok(Bool::And(self.extract(BiAnd, &[lhs, rhs], Vec::new())?)), - _ => {}, - } - }, - ExprLit(ref lit) => { - match lit.node { - LitKind::Bool(true) => return Ok(Bool::True), - LitKind::Bool(false) => return Ok(Bool::False), - _ => {}, - } - }, - _ => {}, + // prevent folding of `cfg!` macros and the like + if !in_macro(self.cx, e.span) { + match e.node { + ExprUnary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)), + ExprBinary(binop, ref lhs, ref rhs) => { + match binop.node { + BiOr => return Ok(Bool::Or(self.extract(BiOr, &[lhs, rhs], Vec::new())?)), + BiAnd => return Ok(Bool::And(self.extract(BiAnd, &[lhs, rhs], Vec::new())?)), + _ => {}, + } + }, + ExprLit(ref lit) => { + match lit.node { + LitKind::Bool(true) => return Ok(Bool::True), + LitKind::Bool(false) => return Ok(Bool::False), + _ => {}, + } + }, + _ => {}, + } } if let Some((n, _)) = self.terminals .iter() diff --git a/tests/compile-fail/booleans.rs b/tests/compile-fail/booleans.rs index 8130e535773..f9bf1a15f18 100644 --- a/tests/compile-fail/booleans.rs +++ b/tests/compile-fail/booleans.rs @@ -29,4 +29,7 @@ fn main() { let _ = false || a; //~ ERROR this boolean expression can be simplified //|~ HELP for further information visit //|~ SUGGESTION let _ = a; + + // don't lint on cfgs + let _ = cfg!(you_shall_not_not_pass) && a; } -- 2.44.0