Skip to content

Commit 8d258c1

Browse files
committed
Add handling for None.unwrap_or(_else)
1 parent c2aaa62 commit 8d258c1

File tree

4 files changed

+175
-42
lines changed

4 files changed

+175
-42
lines changed

clippy_lints/src/methods/unnecessary_literal_unwrap.rs

+19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn get_ty_from_args<'a>(args: Option<&'a [hir::GenericArg<'a>]>, index: usize) -
2424
}
2525
}
2626

27+
#[expect(clippy::too_many_lines)]
2728
pub(super) fn check(
2829
cx: &LateContext<'_>,
2930
expr: &hir::Expr<'_>,
@@ -95,6 +96,24 @@ pub(super) fn check(
9596
};
9697
Some(vec![(expr.span, format!("{default_ty_string}::default()"))])
9798
},
99+
("None", "unwrap_or", _) => Some(vec![
100+
(expr.span.with_hi(args[0].span.lo()), String::new()),
101+
(expr.span.with_lo(args[0].span.hi()), String::new()),
102+
]),
103+
("None", "unwrap_or_else", _) => match args[0].kind {
104+
hir::ExprKind::Closure(hir::Closure {
105+
fn_decl:
106+
hir::FnDecl {
107+
output: hir::FnRetTy::DefaultReturn(span) | hir::FnRetTy::Return(hir::Ty { span, .. }),
108+
..
109+
},
110+
..
111+
}) => Some(vec![
112+
(expr.span.with_hi(span.hi()), String::new()),
113+
(expr.span.with_lo(args[0].span.hi()), String::new()),
114+
]),
115+
_ => None,
116+
},
98117
_ if call_args.is_empty() => None,
99118
(_, _, Some(_)) => None,
100119
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![

tests/ui/unnecessary_literal_unwrap.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ fn unwrap_option_some() {
1616
1;
1717
}
1818

19+
#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
1920
fn unwrap_option_none() {
2021
let _val = panic!();
2122
let _val = panic!("this always happens");
2223
let _val: String = String::default();
24+
let _val: u16 = 234;
25+
let _val: u16 = 234;
26+
let _val: u16 = { 234 };
27+
let _val: u16 = { 234 };
2328

2429
panic!();
2530
panic!("this always happens");
2631
String::default();
32+
234;
33+
234;
34+
{ 234 };
35+
{ 234 };
2736
}
2837

2938
fn unwrap_result_ok() {

tests/ui/unnecessary_literal_unwrap.rs

+9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ fn unwrap_option_some() {
1616
Some(1).expect("this never happens");
1717
}
1818

19+
#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
1920
fn unwrap_option_none() {
2021
let _val = None::<()>.unwrap();
2122
let _val = None::<()>.expect("this always happens");
2223
let _val: String = None.unwrap_or_default();
24+
let _val: u16 = None.unwrap_or(234);
25+
let _val: u16 = None.unwrap_or_else(|| 234);
26+
let _val: u16 = None.unwrap_or_else(|| { 234 });
27+
let _val: u16 = None.unwrap_or_else(|| -> u16 { 234 });
2328

2429
None::<()>.unwrap();
2530
None::<()>.expect("this always happens");
2631
None::<String>.unwrap_or_default();
32+
None::<u16>.unwrap_or(234);
33+
None::<u16>.unwrap_or_else(|| 234);
34+
None::<u16>.unwrap_or_else(|| { 234 });
35+
None::<u16>.unwrap_or_else(|| -> u16 { 234 });
2736
}
2837

2938
fn unwrap_result_ok() {

0 commit comments

Comments
 (0)