Skip to content

Commit

Permalink
Check pattern equality while checking declaration equality
Browse files Browse the repository at this point in the history
  • Loading branch information
jorpic committed Dec 27, 2018
1 parent dfe1232 commit bc93633
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 3 additions & 1 deletion clippy_lints/src/utils/hir_utils.rs
Expand Up @@ -54,7 +54,9 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
match (&left.node, &right.node) {
(&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
self.eq_pat(&l.pat, &r.pat)
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
&& both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
} else {
false
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/copies.rs
Expand Up @@ -335,6 +335,17 @@ fn if_same_then_else() -> Result<&'static str, ()> {
let foo = "";
return Ok(&foo[0..]);
}

// See #3559
if true {
let foo = "";
let (x, y) = (1, 2);
return Ok(&foo[x..y]);
} else {
let foo = "";
let (y, x) = (1, 2);
return Ok(&foo[x..y]);
}
}

#[warn(clippy::ifs_same_cond)]
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/copies.stderr
Expand Up @@ -352,40 +352,40 @@ note: same as this
| |_____^

error: this `if` has the same condition as a previous if
--> $DIR/copies.rs:347:15
--> $DIR/copies.rs:358:15
|
347 | } else if b {
358 | } else if b {
| ^
|
= note: `-D clippy::ifs-same-cond` implied by `-D warnings`
note: same as this
--> $DIR/copies.rs:346:8
--> $DIR/copies.rs:357:8
|
346 | if b {
357 | if b {
| ^

error: this `if` has the same condition as a previous if
--> $DIR/copies.rs:352:15
--> $DIR/copies.rs:363:15
|
352 | } else if a == 1 {
363 | } else if a == 1 {
| ^^^^^^
|
note: same as this
--> $DIR/copies.rs:351:8
--> $DIR/copies.rs:362:8
|
351 | if a == 1 {
362 | if a == 1 {
| ^^^^^^

error: this `if` has the same condition as a previous if
--> $DIR/copies.rs:358:15
--> $DIR/copies.rs:369:15
|
358 | } else if 2 * a == 1 {
369 | } else if 2 * a == 1 {
| ^^^^^^^^^^
|
note: same as this
--> $DIR/copies.rs:356:8
--> $DIR/copies.rs:367:8
|
356 | if 2 * a == 1 {
367 | if 2 * a == 1 {
| ^^^^^^^^^^

error: aborting due to 20 previous errors
Expand Down

0 comments on commit bc93633

Please sign in to comment.