From 49e2501c633d519d56352609afda8103dbe8a2e8 Mon Sep 17 00:00:00 2001 From: mcarton Date: Sat, 13 Feb 2016 15:36:57 +0100 Subject: [PATCH] Fix false positive for `ifs_same_cond` and `cfg!` --- src/utils/hir.rs | 5 +++++ tests/compile-fail/copies.rs | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/utils/hir.rs b/src/utils/hir.rs index f8695956f..e527f63eb 100644 --- a/src/utils/hir.rs +++ b/src/utils/hir.rs @@ -4,6 +4,7 @@ use rustc_front::hir::*; use std::hash::{Hash, Hasher, SipHasher}; use syntax::ast::Name; use syntax::ptr::P; +use utils::differing_macro_contexts; /// Type used to check whether two ast are the same. This is different from the operator /// `==` on ast types as this operator would compare true equality with ID and span. @@ -53,6 +54,10 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> { // ok, it’s a big function, but mostly one big match with simples cases #[allow(cyclomatic_complexity)] pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool { + if self.ignore_fn && differing_macro_contexts(left.span, right.span) { + return false; + } + if let (Some(l), Some(r)) = (constant(self.cx, left), constant(self.cx, right)) { if l == r { return true; diff --git a/tests/compile-fail/copies.rs b/tests/compile-fail/copies.rs index 623f9967b..7a17b345f 100755 --- a/tests/compile-fail/copies.rs +++ b/tests/compile-fail/copies.rs @@ -12,7 +12,7 @@ fn foo() -> bool { unimplemented!() } #[deny(if_same_then_else)] #[deny(match_same_arms)] -fn if_same_then_else() -> &'static str { +fn if_same_then_else() -> Result<&'static str, ()> { if true { foo(); } @@ -129,17 +129,24 @@ fn if_same_then_else() -> &'static str { _ => (), } + if true { + try!(Ok("foo")); + } + else { //~ERROR this `if` has identical blocks + try!(Ok("foo")); + } + if true { let foo = ""; - return &foo[0..]; + return Ok(&foo[0..]); } else if false { let foo = "bar"; - return &foo[0..]; + return Ok(&foo[0..]); } else { //~ERROR this `if` has identical blocks let foo = ""; - return &foo[0..]; + return Ok(&foo[0..]); } } @@ -168,6 +175,15 @@ fn ifs_same_cond() { else if a == 1 { } + // See #659 + if cfg!(feature = "feature1-659") { + 1 + } else if cfg!(feature = "feature2-659") { + 2 + } else { + 3 + }; + let mut v = vec![1]; if v.pop() == None { // ok, functions }