Skip to content

Commit 5b63ab1

Browse files
committed
Lint manual_unwrap_or_default for Result as well
1 parent 4e7f974 commit 5b63ab1

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

clippy_lints/src/manual_unwrap_or_default.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
5757
// Since it comes from a pattern binding, we need to get the parent to actually match
5858
// against it.
5959
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
60-
&& cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
60+
&& (cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
61+
|| cx.tcx.lang_items().get(LangItem::ResultOk) == Some(def_id))
6162
{
6263
let mut bindings = Vec::new();
6364
pat.each_binding(|_, id, _, _| bindings.push(id));
@@ -80,6 +81,14 @@ fn get_none<'tcx>(cx: &LateContext<'tcx>, arm: &Arm<'tcx>) -> Option<&'tcx Expr<
8081
&& cx.tcx.lang_items().get(LangItem::OptionNone) == Some(def_id)
8182
{
8283
Some(arm.body)
84+
} else if let PatKind::TupleStruct(QPath::Resolved(_, path), _, _)= arm.pat.kind
85+
&& let Some(def_id) = path.res.opt_def_id()
86+
// Since it comes from a pattern binding, we need to get the parent to actually match
87+
// against it.
88+
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
89+
&& cx.tcx.lang_items().get(LangItem::ResultErr) == Some(def_id)
90+
{
91+
Some(arm.body)
8392
} else if let PatKind::Wild = arm.pat.kind {
8493
// We consider that the `Some` check will filter it out if it's not right.
8594
Some(arm.body)

tests/ui/manual_unwrap_or_default.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ fn main() {
2626
Some(x) => x,
2727
None => &[],
2828
};
29+
30+
let x: Result<String, i64> = Ok(String::new());
31+
x.unwrap_or_default();
32+
33+
let x: Result<String, i64> = Ok(String::new());
34+
x.unwrap_or_default();
2935
}
3036

3137
// Issue #12531

tests/ui/manual_unwrap_or_default.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ fn main() {
4747
Some(x) => x,
4848
None => &[],
4949
};
50+
51+
let x: Result<String, i64> = Ok(String::new());
52+
match x {
53+
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
54+
Ok(v) => v,
55+
Err(_) => String::new(),
56+
};
57+
58+
let x: Result<String, i64> = Ok(String::new());
59+
if let Ok(v) = x {
60+
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
61+
v
62+
} else {
63+
String::new()
64+
};
5065
}
5166

5267
// Issue #12531

tests/ui/manual_unwrap_or_default.stderr

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

5555
error: match can be simplified with `.unwrap_or_default()`
56-
--> tests/ui/manual_unwrap_or_default.rs:56:20
56+
--> tests/ui/manual_unwrap_or_default.rs:52:5
57+
|
58+
LL | / match x {
59+
LL | |
60+
LL | | Ok(v) => v,
61+
LL | | Err(_) => String::new(),
62+
LL | | };
63+
| |_____^ help: replace it with: `x.unwrap_or_default()`
64+
65+
error: if let can be simplified with `.unwrap_or_default()`
66+
--> tests/ui/manual_unwrap_or_default.rs:59:5
67+
|
68+
LL | / if let Ok(v) = x {
69+
LL | |
70+
LL | | v
71+
LL | | } else {
72+
LL | | String::new()
73+
LL | | };
74+
| |_____^ help: replace it with: `x.unwrap_or_default()`
75+
76+
error: match can be simplified with `.unwrap_or_default()`
77+
--> tests/ui/manual_unwrap_or_default.rs:71:20
5778
|
5879
LL | Some(_) => match *b {
5980
| ____________________^
@@ -62,5 +83,5 @@ LL | | _ => 0,
6283
LL | | },
6384
| |_________^ help: replace it with: `(*b).unwrap_or_default()`
6485

65-
error: aborting due to 6 previous errors
86+
error: aborting due to 8 previous errors
6687

0 commit comments

Comments
 (0)