Fix false positive for ifs_same_cond and cfg!

This commit is contained in:
mcarton 2016-02-13 15:36:57 +01:00
parent 1e176cae5d
commit 49e2501c63
2 changed files with 25 additions and 4 deletions

View file

@ -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, its 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;

View file

@ -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
}