Skip to content

Commit 911a752

Browse files
committed
Check pattern equality while checking declaration equality
1 parent aee138a commit 911a752

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

clippy_lints/src/utils/hir_utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
5454
match (&left.node, &right.node) {
5555
(&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
5656
if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
57-
both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
57+
self.eq_pat(&l.pat, &r.pat)
58+
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
59+
&& both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
5860
} else {
5961
false
6062
}

tests/ui/copies.rs

+11
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,17 @@ fn if_same_then_else() -> Result<&'static str, ()> {
335335
let foo = "";
336336
return Ok(&foo[0..]);
337337
}
338+
339+
// false positive if_same_then_else, let(x,y) vs let(y,x), see #3559
340+
if true {
341+
let foo = "";
342+
let (x, y) = (1, 2);
343+
return Ok(&foo[x..y]);
344+
} else {
345+
let foo = "";
346+
let (y, x) = (1, 2);
347+
return Ok(&foo[x..y]);
348+
}
338349
}
339350

340351
#[warn(clippy::ifs_same_cond)]

tests/ui/copies.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -352,38 +352,38 @@ LL | | } else {
352352
| |_____^
353353

354354
error: this `if` has the same condition as a previous if
355-
--> $DIR/copies.rs:347:15
355+
--> $DIR/copies.rs:358:15
356356
|
357357
LL | } else if b {
358358
| ^
359359
|
360360
= note: `-D clippy::ifs-same-cond` implied by `-D warnings`
361361
note: same as this
362-
--> $DIR/copies.rs:346:8
362+
--> $DIR/copies.rs:357:8
363363
|
364364
LL | if b {
365365
| ^
366366

367367
error: this `if` has the same condition as a previous if
368-
--> $DIR/copies.rs:352:15
368+
--> $DIR/copies.rs:363:15
369369
|
370370
LL | } else if a == 1 {
371371
| ^^^^^^
372372
|
373373
note: same as this
374-
--> $DIR/copies.rs:351:8
374+
--> $DIR/copies.rs:362:8
375375
|
376376
LL | if a == 1 {
377377
| ^^^^^^
378378

379379
error: this `if` has the same condition as a previous if
380-
--> $DIR/copies.rs:358:15
380+
--> $DIR/copies.rs:369:15
381381
|
382382
LL | } else if 2 * a == 1 {
383383
| ^^^^^^^^^^
384384
|
385385
note: same as this
386-
--> $DIR/copies.rs:356:8
386+
--> $DIR/copies.rs:367:8
387387
|
388388
LL | if 2 * a == 1 {
389389
| ^^^^^^^^^^

0 commit comments

Comments
 (0)