Skip to content

Commit 1d9e0cd

Browse files
committed
Merge branches 'master' and 'master' of https://github.com/t-webber/rust-clippy into fix_manual_unwrap_or_default
2 parents 1fd8241 + f97d38e commit 1d9e0cd

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

clippy_lints/src/manual_unwrap_or_default.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, exp
131131
},
132132
};
133133

134-
// We check if the condition is not a None variant
135-
let none_def_id = cx.tcx.lang_items().option_none_variant().unwrap();
136-
if let ExprKind::Path(QPath::Resolved(_, path)) = &condition.kind {
137-
if let Some(def_id) = path.res.opt_def_id() {
138-
if cx.tcx.parent(def_id) == none_def_id {
139-
dbg!("found none");
140-
<<<<<<< HEAD
141-
return;
142-
>>>>>>> b9dcd2785 (Fix manual_unwrap_or_default)
143-
=======
144-
return;
145-
>>>>>>> dcaf974c8 (fix merge)
134+
// We check if the expression is not a None variant
135+
if let Some(none_def_id) = cx.tcx.lang_items().option_none_variant() {
136+
if let ExprKind::Path(QPath::Resolved(_, path)) = &condition.kind {
137+
if let Some(def_id) = path.res.opt_def_id() {
138+
if cx.tcx.parent(def_id) == none_def_id {
139+
return;
140+
}
146141
}
147142
}
148143
}

tests/ui/manual_unwrap_or_default.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,12 @@ fn issue_12569() {
7272
0
7373
};
7474
}
75+
76+
fn issue_16937() {
77+
// pass
78+
let _ = if let Some(x) = None { x } else { i32::default() };
79+
// fail
80+
let a: Option<i32> = None;
81+
let _ = a.unwrap_or_default();
82+
let _ = Some(99).unwrap_or_default();
83+
}

tests/ui/manual_unwrap_or_default.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ fn issue_12569() {
9696
0
9797
};
9898
}
99+
100+
fn issue_16937() {
101+
// pass
102+
let _ = if let Some(x) = None { x } else { i32::default() };
103+
// fail
104+
let a: Option<i32> = None;
105+
let _ = if let Some(x) = a { x } else { i32::default() };
106+
let _ = if let Some(x) = Some(99) { x } else { i32::default() };
107+
}

tests/ui/manual_unwrap_or_default.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,17 @@ LL | | _ => 0,
6262
LL | | },
6363
| |_________^ help: replace it with: `(*b).unwrap_or_default()`
6464

65-
error: aborting due to 6 previous errors
65+
error: if let can be simplified with `.unwrap_or_default()`
66+
--> tests/ui/manual_unwrap_or_default.rs:105:13
67+
|
68+
LL | let _ = if let Some(x) = a { x } else { i32::default() };
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.unwrap_or_default()`
70+
71+
error: if let can be simplified with `.unwrap_or_default()`
72+
--> tests/ui/manual_unwrap_or_default.rs:106:13
73+
|
74+
LL | let _ = if let Some(x) = Some(99) { x } else { i32::default() };
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(99).unwrap_or_default()`
76+
77+
error: aborting due to 8 previous errors
6678

0 commit comments

Comments
 (0)