Skip to content

Commit 750204e

Browse files
committed
fix a bug that caused internal test fail
1 parent 6bfc112 commit 750204e

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

clippy_lints/src/matches/nop_match.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,29 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>) {
8080
}
8181

8282
fn check_if_let(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool {
83-
if let Some(else_block) = if_let.if_else {
83+
if let Some(if_else) = if_let.if_else {
8484
if !pat_same_as_expr(if_let.let_pat, peel_blocks_with_stmt(if_let.if_then)) {
8585
return false;
8686
}
8787

88-
let else_expr = peel_blocks_with_stmt(else_block);
8988
// Recurrsively check for each `else if let` phrase,
90-
if let Some(ref nested_if_let) = higher::IfLet::hir(cx, else_expr) {
89+
if let Some(ref nested_if_let) = higher::IfLet::hir(cx, if_else) {
9190
return check_if_let(cx, nested_if_let);
9291
}
93-
let ret = strip_return(else_expr);
94-
let let_expr_ty = cx.typeck_results().expr_ty(if_let.let_expr);
95-
if is_type_diagnostic_item(cx, let_expr_ty, sym::Option) {
96-
if let ExprKind::Path(ref qpath) = ret.kind {
97-
return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
92+
93+
if matches!(if_else.kind, ExprKind::Block(..)) {
94+
let else_expr = peel_blocks_with_stmt(if_else);
95+
let ret = strip_return(else_expr);
96+
let let_expr_ty = cx.typeck_results().expr_ty(if_let.let_expr);
97+
if is_type_diagnostic_item(cx, let_expr_ty, sym::Option) {
98+
if let ExprKind::Path(ref qpath) = ret.kind {
99+
return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
100+
}
101+
} else {
102+
return eq_expr_value(cx, if_let.let_expr, ret);
98103
}
99-
} else {
100-
return eq_expr_value(cx, if_let.let_expr, ret);
104+
return true;
101105
}
102-
return true;
103106
}
104107
false
105108
}

tests/ui/nop_match.fixed

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::manual_map)]
44
#![allow(dead_code)]
55

6+
#[derive(Clone, Copy)]
67
enum Choice {
78
A,
89
B,
@@ -60,8 +61,16 @@ fn if_let_result(x: Result<(), i32>) {
6061
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
6162
}
6263

63-
fn custom_enum_a(x: Choice) -> Choice {
64-
x
64+
fn if_let_custom_enum(x: Choice) {
65+
let _: Choice = x;
66+
// Don't trigger
67+
let _: Choice = if let Choice::A = x {
68+
Choice::A
69+
} else if true {
70+
Choice::B
71+
} else {
72+
x
73+
};
6574
}
6675

6776
fn main() {}

tests/ui/nop_match.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::manual_map)]
44
#![allow(dead_code)]
55

6+
#[derive(Clone, Copy)]
67
enum Choice {
78
A,
89
B,
@@ -79,16 +80,24 @@ fn if_let_result(x: Result<(), i32>) {
7980
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
8081
}
8182

82-
fn custom_enum_a(x: Choice) -> Choice {
83-
if let Choice::A = x {
83+
fn if_let_custom_enum(x: Choice) {
84+
let _: Choice = if let Choice::A = x {
8485
Choice::A
8586
} else if let Choice::B = x {
8687
Choice::B
8788
} else if let Choice::C = x {
8889
Choice::C
8990
} else {
9091
x
91-
}
92+
};
93+
// Don't trigger
94+
let _: Choice = if let Choice::A = x {
95+
Choice::A
96+
} else if true {
97+
Choice::B
98+
} else {
99+
x
100+
};
92101
}
93102

94103
fn main() {}

tests/ui/nop_match.stderr

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this match expression is unnecessary
2-
--> $DIR/nop_match.rs:14:18
2+
--> $DIR/nop_match.rs:15:18
33
|
44
LL | let _: i32 = match x {
55
| __________________^
@@ -13,7 +13,7 @@ LL | | };
1313
= note: `-D clippy::nop-match` implied by `-D warnings`
1414

1515
error: this match expression is unnecessary
16-
--> $DIR/nop_match.rs:23:21
16+
--> $DIR/nop_match.rs:24:21
1717
|
1818
LL | let _: Choice = match se {
1919
| _____________________^
@@ -25,7 +25,7 @@ LL | | };
2525
| |_____^ help: replace it with: `se`
2626

2727
error: this match expression is unnecessary
28-
--> $DIR/nop_match.rs:45:26
28+
--> $DIR/nop_match.rs:46:26
2929
|
3030
LL | let _: Option<i32> = match x {
3131
| __________________________^
@@ -35,7 +35,7 @@ LL | | };
3535
| |_____^ help: replace it with: `x`
3636

3737
error: this match expression is unnecessary
38-
--> $DIR/nop_match.rs:61:31
38+
--> $DIR/nop_match.rs:62:31
3939
|
4040
LL | let _: Result<i32, i32> = match Ok(1) {
4141
| _______________________________^
@@ -45,7 +45,7 @@ LL | | };
4545
| |_____^ help: replace it with: `Ok(1)`
4646

4747
error: this match expression is unnecessary
48-
--> $DIR/nop_match.rs:65:31
48+
--> $DIR/nop_match.rs:66:31
4949
|
5050
LL | let _: Result<i32, i32> = match func_ret_err(0_i32) {
5151
| _______________________________^
@@ -55,33 +55,34 @@ LL | | };
5555
| |_____^ help: replace it with: `func_ret_err(0_i32)`
5656

5757
error: this if-let expression is unnecessary
58-
--> $DIR/nop_match.rs:72:5
58+
--> $DIR/nop_match.rs:73:5
5959
|
6060
LL | if let Some(a) = Some(1) { Some(a) } else { None }
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(1)`
6262

6363
error: this if-let expression is unnecessary
64-
--> $DIR/nop_match.rs:76:30
64+
--> $DIR/nop_match.rs:77:30
6565
|
6666
LL | let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
6868

6969
error: this if-let expression is unnecessary
70-
--> $DIR/nop_match.rs:77:30
70+
--> $DIR/nop_match.rs:78:30
7171
|
7272
LL | let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
7474

7575
error: this if-let expression is unnecessary
76-
--> $DIR/nop_match.rs:83:5
76+
--> $DIR/nop_match.rs:84:21
7777
|
78-
LL | / if let Choice::A = x {
78+
LL | let _: Choice = if let Choice::A = x {
79+
| _____________________^
7980
LL | | Choice::A
8081
LL | | } else if let Choice::B = x {
8182
LL | | Choice::B
8283
... |
8384
LL | | x
84-
LL | | }
85+
LL | | };
8586
| |_____^ help: replace it with: `x`
8687

8788
error: aborting due to 9 previous errors

0 commit comments

Comments
 (0)