Skip to content

Commit 49e2501

Browse files
committed
Fix false positive for ifs_same_cond and cfg!
1 parent 1e176ca commit 49e2501

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Diff for: src/utils/hir.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_front::hir::*;
44
use std::hash::{Hash, Hasher, SipHasher};
55
use syntax::ast::Name;
66
use syntax::ptr::P;
7+
use utils::differing_macro_contexts;
78

89
/// Type used to check whether two ast are the same. This is different from the operator
910
/// `==` 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> {
5354
// ok, it’s a big function, but mostly one big match with simples cases
5455
#[allow(cyclomatic_complexity)]
5556
pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
57+
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
58+
return false;
59+
}
60+
5661
if let (Some(l), Some(r)) = (constant(self.cx, left), constant(self.cx, right)) {
5762
if l == r {
5863
return true;

Diff for: tests/compile-fail/copies.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn foo() -> bool { unimplemented!() }
1212

1313
#[deny(if_same_then_else)]
1414
#[deny(match_same_arms)]
15-
fn if_same_then_else() -> &'static str {
15+
fn if_same_then_else() -> Result<&'static str, ()> {
1616
if true {
1717
foo();
1818
}
@@ -129,17 +129,24 @@ fn if_same_then_else() -> &'static str {
129129
_ => (),
130130
}
131131

132+
if true {
133+
try!(Ok("foo"));
134+
}
135+
else { //~ERROR this `if` has identical blocks
136+
try!(Ok("foo"));
137+
}
138+
132139
if true {
133140
let foo = "";
134-
return &foo[0..];
141+
return Ok(&foo[0..]);
135142
}
136143
else if false {
137144
let foo = "bar";
138-
return &foo[0..];
145+
return Ok(&foo[0..]);
139146
}
140147
else { //~ERROR this `if` has identical blocks
141148
let foo = "";
142-
return &foo[0..];
149+
return Ok(&foo[0..]);
143150
}
144151
}
145152

@@ -168,6 +175,15 @@ fn ifs_same_cond() {
168175
else if a == 1 {
169176
}
170177

178+
// See #659
179+
if cfg!(feature = "feature1-659") {
180+
1
181+
} else if cfg!(feature = "feature2-659") {
182+
2
183+
} else {
184+
3
185+
};
186+
171187
let mut v = vec![1];
172188
if v.pop() == None { // ok, functions
173189
}

0 commit comments

Comments
 (0)