Skip to content

Commit f99ad82

Browse files
committed
Auto merge of #8700 - youknowone:needless_match-false-positive, r=xFrednet
Fix needless_match false positive for if-let when the else block doesn't match to given expr <!-- Thank you for making Clippy better! We're collecting our changelog from pull request descriptions. If your PR only includes internal changes, you can just write `changelog: none`. Otherwise, please write a short comment explaining your change. Also, it's helpful for us that the lint name is put into brackets `[]` and backticks `` ` ` ``, e.g. ``[`lint_name`]``. If your PR fixes an issue, you can add "fixes #issue_number" into this PR description. This way the issue will be automatically closed when your PR is merged. If you added a new lint, here's a checklist for things that will be checked during review or continuous integration. - \[ ] Followed [lint naming conventions][lint_naming] - \[ ] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[ ] Added lint documentation - \[x] Run `cargo dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints Note that you can skip the above if you are just opening a WIP PR in order to get feedback. Delete this line and everything above before opening your PR. ---> fix #8695 *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Fixed ``[`needless_match`]`` false positive when else block expression differs.
2 parents e17b97c + b94e24e commit f99ad82

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

clippy_lints/src/matches/needless_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn check_if_let(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool {
9999
if let ExprKind::Path(ref qpath) = ret.kind {
100100
return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
101101
}
102-
return true;
102+
return false;
103103
}
104104
return eq_expr_value(cx, if_let.let_expr, ret);
105105
}

tests/ui/needless_match.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ fn if_let_option() {
8080
} else {
8181
None
8282
};
83+
84+
// Don't trigger
85+
let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
86+
}
87+
88+
fn if_let_option_result() -> Result<(), ()> {
89+
fn f(x: i32) -> Result<Option<i32>, ()> {
90+
Ok(Some(x))
91+
}
92+
// Don't trigger
93+
let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
94+
Ok(())
8395
}
8496

8597
fn if_let_result() {

tests/ui/needless_match.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ fn if_let_option() {
103103
} else {
104104
None
105105
};
106+
107+
// Don't trigger
108+
let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
109+
}
110+
111+
fn if_let_option_result() -> Result<(), ()> {
112+
fn f(x: i32) -> Result<Option<i32>, ()> {
113+
Ok(Some(x))
114+
}
115+
// Don't trigger
116+
let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
117+
Ok(())
106118
}
107119

108120
fn if_let_result() {

tests/ui/needless_match.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ LL | let _ = if let Some(a) = Some(1) { Some(a) } else { None };
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(1)`
7373

7474
error: this if-let expression is unnecessary
75-
--> $DIR/needless_match.rs:110:31
75+
--> $DIR/needless_match.rs:122:31
7676
|
7777
LL | let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
7878
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
7979

8080
error: this if-let expression is unnecessary
81-
--> $DIR/needless_match.rs:111:31
81+
--> $DIR/needless_match.rs:123:31
8282
|
8383
LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x`
8585

8686
error: this if-let expression is unnecessary
87-
--> $DIR/needless_match.rs:117:21
87+
--> $DIR/needless_match.rs:129:21
8888
|
8989
LL | let _: Simple = if let Simple::A = x {
9090
| _____________________^
@@ -97,7 +97,7 @@ LL | | };
9797
| |_____^ help: replace it with: `x`
9898

9999
error: this match expression is unnecessary
100-
--> $DIR/needless_match.rs:156:26
100+
--> $DIR/needless_match.rs:168:26
101101
|
102102
LL | let _: Complex = match ce {
103103
| __________________________^

0 commit comments

Comments
 (0)