Skip to content

Commit 62c323b

Browse files
committed
manual_unwrap_or: fix FP edge case
1 parent aa1fa10 commit 62c323b

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

clippy_lints/src/matches/manual_unwrap_or.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn get_some(cx: &LateContext<'_>, pat: &Pat<'_>) -> Option<HirId> {
3131
}
3232
}
3333

34-
fn get_none<'tcx>(cx: &LateContext<'_>, arm: &Arm<'tcx>) -> Option<&'tcx Expr<'tcx>> {
34+
fn get_none<'tcx>(cx: &LateContext<'_>, arm: &Arm<'tcx>, allow_wildcard: bool) -> Option<&'tcx Expr<'tcx>> {
3535
if let PatKind::Expr(PatExpr { kind: PatExprKind::Path(QPath::Resolved(_, path)), .. }) = arm.pat.kind
3636
&& let Some(def_id) = path.res.opt_def_id()
3737
// Since it comes from a pattern binding, we need to get the parent to actually match
@@ -48,7 +48,7 @@ fn get_none<'tcx>(cx: &LateContext<'_>, arm: &Arm<'tcx>) -> Option<&'tcx Expr<'t
4848
&& cx.tcx.lang_items().get(LangItem::ResultErr) == Some(def_id)
4949
{
5050
Some(arm.body)
51-
} else if let PatKind::Wild = arm.pat.kind {
51+
} else if let (PatKind::Wild, true) = (arm.pat.kind, allow_wildcard) {
5252
// We consider that the `Some` check will filter it out if it's not right.
5353
Some(arm.body)
5454
} else {
@@ -62,11 +62,11 @@ fn get_some_and_none_bodies<'tcx>(
6262
arm2: &'tcx Arm<'tcx>,
6363
) -> Option<((&'tcx Expr<'tcx>, HirId), &'tcx Expr<'tcx>)> {
6464
if let Some(binding_id) = get_some(cx, arm1.pat)
65-
&& let Some(body_none) = get_none(cx, arm2)
65+
&& let Some(body_none) = get_none(cx, arm2, true)
6666
{
6767
Some(((arm1.body, binding_id), body_none))
6868
} else if let Some(binding_id) = get_some(cx, arm2.pat)
69-
&& let Some(body_none) = get_none(cx, arm1)
69+
&& let Some(body_none) = get_none(cx, arm1, false)
7070
{
7171
Some(((arm2.body, binding_id), body_none))
7272
} else {

tests/ui/manual_unwrap_or_default.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ fn main() {
3232

3333
let x: Result<String, i64> = Ok(String::new());
3434
x.unwrap_or_default();
35+
36+
let bizarro = Some(String::new());
37+
match bizarro {
38+
_ => String::new(),
39+
Some(bizarro) => bizarro,
40+
};
3541
}
3642

3743
// Issue #12531

tests/ui/manual_unwrap_or_default.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ fn main() {
6464
} else {
6565
String::new()
6666
};
67+
68+
// edge case
69+
let bizarro = Some(String::new());
70+
match bizarro {
71+
_ => String::new(),
72+
Some(bizarro) => bizarro,
73+
};
6774
}
6875

6976
// Issue #12531

tests/ui/manual_unwrap_or_default.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ LL | | };
7676
| |_____^ help: replace it with: `x.unwrap_or_default()`
7777

7878
error: match can be simplified with `.unwrap_or_default()`
79-
--> tests/ui/manual_unwrap_or_default.rs:74:24
79+
--> tests/ui/manual_unwrap_or_default.rs:80:24
8080
|
8181
LL | Some(_) => match *b {
8282
| ________________________^
@@ -87,7 +87,7 @@ LL | | },
8787
| |_____________^ help: replace it with: `(*b).unwrap_or_default()`
8888

8989
error: if let can be simplified with `.unwrap_or_default()`
90-
--> tests/ui/manual_unwrap_or_default.rs:143:5
90+
--> tests/ui/manual_unwrap_or_default.rs:149:5
9191
|
9292
LL | / if let Some(x) = Some(42) {
9393
LL | |

0 commit comments

Comments
 (0)