Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix if_same_then_else false positive #3590

Merged
merged 1 commit into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ fn if_same_then_else() -> Result<&'static str, ()> {
let foo = "";
return Ok(&foo[0..]);
}

// false positive if_same_then_else, let(x,y) vs let(y,x), 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
12 changes: 6 additions & 6 deletions tests/ui/copies.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -352,38 +352,38 @@ LL | | } else {
| |_____^

error: this `if` has the same condition as a previous if
--> $DIR/copies.rs:347:15
--> $DIR/copies.rs:358:15
|
LL | } 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
|
LL | if b {
| ^

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

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