Add handling for None.unwrap_or(_else)

This commit is contained in:
syvb 2023-07-05 23:24:20 -04:00
parent c2aaa622eb
commit 8d258c1508
4 changed files with 175 additions and 42 deletions

View File

@ -24,6 +24,7 @@ fn get_ty_from_args<'a>(args: Option<&'a [hir::GenericArg<'a>]>, index: usize) -
}
}
#[expect(clippy::too_many_lines)]
pub(super) fn check(
cx: &LateContext<'_>,
expr: &hir::Expr<'_>,
@ -95,6 +96,24 @@ pub(super) fn check(
};
Some(vec![(expr.span, format!("{default_ty_string}::default()"))])
},
("None", "unwrap_or", _) => Some(vec![
(expr.span.with_hi(args[0].span.lo()), String::new()),
(expr.span.with_lo(args[0].span.hi()), String::new()),
]),
("None", "unwrap_or_else", _) => match args[0].kind {
hir::ExprKind::Closure(hir::Closure {
fn_decl:
hir::FnDecl {
output: hir::FnRetTy::DefaultReturn(span) | hir::FnRetTy::Return(hir::Ty { span, .. }),
..
},
..
}) => Some(vec![
(expr.span.with_hi(span.hi()), String::new()),
(expr.span.with_lo(args[0].span.hi()), String::new()),
]),
_ => None,
},
_ if call_args.is_empty() => None,
(_, _, Some(_)) => None,
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![

View File

@ -16,14 +16,23 @@ fn unwrap_option_some() {
1;
}
#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
fn unwrap_option_none() {
let _val = panic!();
let _val = panic!("this always happens");
let _val: String = String::default();
let _val: u16 = 234;
let _val: u16 = 234;
let _val: u16 = { 234 };
let _val: u16 = { 234 };
panic!();
panic!("this always happens");
String::default();
234;
234;
{ 234 };
{ 234 };
}
fn unwrap_result_ok() {

View File

@ -16,14 +16,23 @@ fn unwrap_option_some() {
Some(1).expect("this never happens");
}
#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
fn unwrap_option_none() {
let _val = None::<()>.unwrap();
let _val = None::<()>.expect("this always happens");
let _val: String = None.unwrap_or_default();
let _val: u16 = None.unwrap_or(234);
let _val: u16 = None.unwrap_or_else(|| 234);
let _val: u16 = None.unwrap_or_else(|| { 234 });
let _val: u16 = None.unwrap_or_else(|| -> u16 { 234 });
None::<()>.unwrap();
None::<()>.expect("this always happens");
None::<String>.unwrap_or_default();
None::<u16>.unwrap_or(234);
None::<u16>.unwrap_or_else(|| 234);
None::<u16>.unwrap_or_else(|| { 234 });
None::<u16>.unwrap_or_else(|| -> u16 { 234 });
}
fn unwrap_result_ok() {

View File

@ -48,13 +48,13 @@ LL + 1;
|
error: used `unwrap()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:20:16
--> $DIR/unnecessary_literal_unwrap.rs:21:16
|
LL | let _val = None::<()>.unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap()`: `panic!()`
error: used `expect()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:21:16
--> $DIR/unnecessary_literal_unwrap.rs:22:16
|
LL | let _val = None::<()>.expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -65,19 +65,67 @@ LL | let _val = panic!("this always happens");
| ~~~~~~~ ~
error: used `unwrap_or_default()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:22:24
--> $DIR/unnecessary_literal_unwrap.rs:23:24
|
LL | let _val: String = None.unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap_or_default()`: `String::default()`
error: used `unwrap_or()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:24:21
|
LL | let _val: u16 = None.unwrap_or(234);
| ^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or()`
|
LL - let _val: u16 = None.unwrap_or(234);
LL + let _val: u16 = 234;
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:25:21
|
LL | let _val: u16 = None.unwrap_or_else(|| 234);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - let _val: u16 = None.unwrap_or_else(|| 234);
LL + let _val: u16 = 234;
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:26:21
|
LL | let _val: u16 = None.unwrap_or_else(|| { 234 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - let _val: u16 = None.unwrap_or_else(|| { 234 });
LL + let _val: u16 = { 234 };
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:27:21
|
LL | let _val: u16 = None.unwrap_or_else(|| -> u16 { 234 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - let _val: u16 = None.unwrap_or_else(|| -> u16 { 234 });
LL + let _val: u16 = { 234 };
|
error: used `unwrap()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:24:5
--> $DIR/unnecessary_literal_unwrap.rs:29:5
|
LL | None::<()>.unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap()`: `panic!()`
error: used `expect()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:25:5
--> $DIR/unnecessary_literal_unwrap.rs:30:5
|
LL | None::<()>.expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -88,13 +136,61 @@ LL | panic!("this always happens");
| ~~~~~~~ ~
error: used `unwrap_or_default()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:26:5
--> $DIR/unnecessary_literal_unwrap.rs:31:5
|
LL | None::<String>.unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap_or_default()`: `String::default()`
error: used `unwrap_or()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:32:5
|
LL | None::<u16>.unwrap_or(234);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or()`
|
LL - None::<u16>.unwrap_or(234);
LL + 234;
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:33:5
|
LL | None::<u16>.unwrap_or_else(|| 234);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - None::<u16>.unwrap_or_else(|| 234);
LL + 234;
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:34:5
|
LL | None::<u16>.unwrap_or_else(|| { 234 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - None::<u16>.unwrap_or_else(|| { 234 });
LL + { 234 };
|
error: used `unwrap_or_else()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:35:5
|
LL | None::<u16>.unwrap_or_else(|| -> u16 { 234 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `None` and `unwrap_or_else()`
|
LL - None::<u16>.unwrap_or_else(|| -> u16 { 234 });
LL + { 234 };
|
error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:30:16
--> $DIR/unnecessary_literal_unwrap.rs:39:16
|
LL | let _val = Ok::<_, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^
@ -106,7 +202,7 @@ LL + let _val = 1;
|
error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:31:16
--> $DIR/unnecessary_literal_unwrap.rs:40:16
|
LL | let _val = Ok::<_, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -118,7 +214,7 @@ LL + let _val = 1;
|
error: used `unwrap_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:32:16
--> $DIR/unnecessary_literal_unwrap.rs:41:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -129,7 +225,7 @@ LL | let _val = panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~
error: used `expect_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:33:16
--> $DIR/unnecessary_literal_unwrap.rs:42:16
|
LL | let _val = Ok::<_, ()>(1).expect_err("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -140,7 +236,7 @@ LL | let _val = panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~
error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:35:5
--> $DIR/unnecessary_literal_unwrap.rs:44:5
|
LL | Ok::<_, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^
@ -152,7 +248,7 @@ LL + 1;
|
error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:36:5
--> $DIR/unnecessary_literal_unwrap.rs:45:5
|
LL | Ok::<_, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -164,7 +260,7 @@ LL + 1;
|
error: used `unwrap_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:37:5
--> $DIR/unnecessary_literal_unwrap.rs:46:5
|
LL | Ok::<_, ()>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -175,7 +271,7 @@ LL | panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~
error: used `expect_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:38:5
--> $DIR/unnecessary_literal_unwrap.rs:47:5
|
LL | Ok::<_, ()>(1).expect_err("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -186,7 +282,7 @@ LL | panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~
error: used `unwrap_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:42:16
--> $DIR/unnecessary_literal_unwrap.rs:51:16
|
LL | let _val = Err::<(), _>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -198,7 +294,7 @@ LL + let _val = 1;
|
error: used `expect_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:43:16
--> $DIR/unnecessary_literal_unwrap.rs:52:16
|
LL | let _val = Err::<(), _>(1).expect_err("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -210,7 +306,7 @@ LL + let _val = 1;
|
error: used `unwrap()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:44:16
--> $DIR/unnecessary_literal_unwrap.rs:53:16
|
LL | let _val = Err::<(), _>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -221,7 +317,7 @@ LL | let _val = panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~
error: used `expect()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:45:16
--> $DIR/unnecessary_literal_unwrap.rs:54:16
|
LL | let _val = Err::<(), _>(1).expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -232,7 +328,7 @@ LL | let _val = panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~
error: used `unwrap_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:47:5
--> $DIR/unnecessary_literal_unwrap.rs:56:5
|
LL | Err::<(), _>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -244,7 +340,7 @@ LL + 1;
|
error: used `expect_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:48:5
--> $DIR/unnecessary_literal_unwrap.rs:57:5
|
LL | Err::<(), _>(1).expect_err("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -256,7 +352,7 @@ LL + 1;
|
error: used `unwrap()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:49:5
--> $DIR/unnecessary_literal_unwrap.rs:58:5
|
LL | Err::<(), _>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -267,7 +363,7 @@ LL | panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~
error: used `expect()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:50:5
--> $DIR/unnecessary_literal_unwrap.rs:59:5
|
LL | Err::<(), _>(1).expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -278,7 +374,7 @@ LL | panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~
error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:54:16
--> $DIR/unnecessary_literal_unwrap.rs:63:16
|
LL | let _val = Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
@ -290,7 +386,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:55:16
--> $DIR/unnecessary_literal_unwrap.rs:64:16
|
LL | let _val = Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -302,7 +398,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_else()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:56:16
--> $DIR/unnecessary_literal_unwrap.rs:65:16
|
LL | let _val = Some(1).unwrap_or_else(|| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -314,7 +410,7 @@ LL + let _val = 1;
|
error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:58:5
--> $DIR/unnecessary_literal_unwrap.rs:67:5
|
LL | Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
@ -326,7 +422,7 @@ LL + 1;
|
error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:59:5
--> $DIR/unnecessary_literal_unwrap.rs:68:5
|
LL | Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -338,7 +434,7 @@ LL + 1;
|
error: used `unwrap_or_else()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:60:5
--> $DIR/unnecessary_literal_unwrap.rs:69:5
|
LL | Some(1).unwrap_or_else(|| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -350,7 +446,7 @@ LL + 1;
|
error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:64:16
--> $DIR/unnecessary_literal_unwrap.rs:73:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -362,7 +458,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:65:16
--> $DIR/unnecessary_literal_unwrap.rs:74:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -374,7 +470,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_else()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:66:16
--> $DIR/unnecessary_literal_unwrap.rs:75:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or_else(|_| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -386,7 +482,7 @@ LL + let _val = 1;
|
error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:68:5
--> $DIR/unnecessary_literal_unwrap.rs:77:5
|
LL | Ok::<_, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -398,7 +494,7 @@ LL + 1;
|
error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:69:5
--> $DIR/unnecessary_literal_unwrap.rs:78:5
|
LL | Ok::<_, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -410,7 +506,7 @@ LL + 1;
|
error: used `unwrap_or_else()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:70:5
--> $DIR/unnecessary_literal_unwrap.rs:79:5
|
LL | Ok::<_, ()>(1).unwrap_or_else(|_| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -422,7 +518,7 @@ LL + 1;
|
error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:84:22
--> $DIR/unnecessary_literal_unwrap.rs:93:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -434,7 +530,7 @@ LL + let _ = 1;
|
error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:85:22
--> $DIR/unnecessary_literal_unwrap.rs:94:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -446,7 +542,7 @@ LL + let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe
|
error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:86:22
--> $DIR/unnecessary_literal_unwrap.rs:95:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -458,7 +554,7 @@ LL + let _ = 1 + 1;
|
error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:87:22
--> $DIR/unnecessary_literal_unwrap.rs:96:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -470,7 +566,7 @@ LL + let _ = 1;
|
error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:88:22
--> $DIR/unnecessary_literal_unwrap.rs:97:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -482,7 +578,7 @@ LL + let _ = unsafe { 1 + *(&1 as *const i32) };
|
error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:89:22
--> $DIR/unnecessary_literal_unwrap.rs:98:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -494,7 +590,7 @@ LL + let _ = 1 + 1;
|
error: used `unwrap_err_unchecked()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:90:22
--> $DIR/unnecessary_literal_unwrap.rs:99:22
|
LL | let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -505,5 +601,5 @@ LL - let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
LL + let _ = 123;
|
error: aborting due to 45 previous errors
error: aborting due to 53 previous errors