Skip to content

Commit

Permalink
Fix ICE on None.unwrap_or_default()
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb committed Jul 6, 2023
1 parent 3f4e599 commit 1cb1646
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 116 deletions.
11 changes: 11 additions & 0 deletions clippy_lints/src/methods/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clippy_utils::{diagnostics::span_lint_and_then, is_res_lang_ctor, last_path_
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, print::with_forced_trimmed_paths};

use super::UNNECESSARY_LITERAL_UNWRAP;

Expand Down Expand Up @@ -62,6 +63,16 @@ pub(super) fn check(
(expr.span.with_hi(args[0].span.lo()), "panic!(".to_string()),
(expr.span.with_lo(args[0].span.hi()), ")".to_string()),
]),
("None", "unwrap_or_default", _) => {
let ty = cx.typeck_results().expr_ty(expr);
let default_ty_string = if let ty::Adt(def, ..) = ty.kind() {
with_forced_trimmed_paths!(format!("{}", cx.tcx.def_path_str(def.did())))
} else {
"Default".to_string()
};
Some(vec![(expr.span, format!("{default_ty_string}::default()"))])
},
_ if call_args.is_empty() => None,
(_, _, Some(_)) => None,
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![
(
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ fn unwrap_option_some() {
fn unwrap_option_none() {
let _val = panic!();
let _val = panic!("this always happens");
let _val: String = String::default();

panic!();
panic!("this always happens");
String::default();
}

fn unwrap_result_ok() {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ fn unwrap_option_some() {
fn unwrap_option_none() {
let _val = None::<()>.unwrap();
let _val = None::<()>.expect("this always happens");
let _val: String = None.unwrap_or_default();

None::<()>.unwrap();
None::<()>.expect("this always happens");
None::<String>.unwrap_or_default();
}

fn unwrap_result_ok() {
Expand Down
74 changes: 43 additions & 31 deletions tests/ui/unnecessary_literal_unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ help: remove the `None` and `expect()`
LL | let _val = panic!("this always happens");
| ~~~~~~~ ~

error: used `unwrap_or_default()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:22:24
|
LL | let _val: String = None.unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap_or_default()`: `String::default()`

error: used `unwrap()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:23:5
--> $DIR/unnecessary_literal_unwrap.rs:24:5
|
LL | None::<()>.unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap()`: `panic!()`

error: used `expect()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:24:5
--> $DIR/unnecessary_literal_unwrap.rs:25:5
|
LL | None::<()>.expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -81,8 +87,14 @@ help: remove the `None` and `expect()`
LL | panic!("this always happens");
| ~~~~~~~ ~

error: used `unwrap_or_default()` on `None` value
--> $DIR/unnecessary_literal_unwrap.rs:26:5
|
LL | None::<String>.unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the `None` and `unwrap_or_default()`: `String::default()`

error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:28:16
--> $DIR/unnecessary_literal_unwrap.rs:30:16
|
LL | let _val = Ok::<_, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -94,7 +106,7 @@ LL + let _val = 1;
|

error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:29:16
--> $DIR/unnecessary_literal_unwrap.rs:31:16
|
LL | let _val = Ok::<_, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -106,7 +118,7 @@ LL + let _val = 1;
|

error: used `unwrap_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:30:16
--> $DIR/unnecessary_literal_unwrap.rs:32:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -117,7 +129,7 @@ LL | let _val = panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~

error: used `expect_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:31:16
--> $DIR/unnecessary_literal_unwrap.rs:33:16
|
LL | let _val = Ok::<_, ()>(1).expect_err("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -128,7 +140,7 @@ LL | let _val = panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~

error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:33:5
--> $DIR/unnecessary_literal_unwrap.rs:35:5
|
LL | Ok::<_, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -140,7 +152,7 @@ LL + 1;
|

error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:34:5
--> $DIR/unnecessary_literal_unwrap.rs:36:5
|
LL | Ok::<_, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -152,7 +164,7 @@ LL + 1;
|

error: used `unwrap_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:35:5
--> $DIR/unnecessary_literal_unwrap.rs:37:5
|
LL | Ok::<_, ()>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -163,7 +175,7 @@ LL | panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~

error: used `expect_err()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:36:5
--> $DIR/unnecessary_literal_unwrap.rs:38:5
|
LL | Ok::<_, ()>(1).expect_err("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -174,7 +186,7 @@ LL | panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~

error: used `unwrap_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:40:16
--> $DIR/unnecessary_literal_unwrap.rs:42:16
|
LL | let _val = Err::<(), _>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -186,7 +198,7 @@ LL + let _val = 1;
|

error: used `expect_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:41:16
--> $DIR/unnecessary_literal_unwrap.rs:43:16
|
LL | let _val = Err::<(), _>(1).expect_err("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -198,7 +210,7 @@ LL + let _val = 1;
|

error: used `unwrap()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:42:16
--> $DIR/unnecessary_literal_unwrap.rs:44:16
|
LL | let _val = Err::<(), _>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -209,7 +221,7 @@ LL | let _val = panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~

error: used `expect()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:43:16
--> $DIR/unnecessary_literal_unwrap.rs:45:16
|
LL | let _val = Err::<(), _>(1).expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -220,7 +232,7 @@ LL | let _val = panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~

error: used `unwrap_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:45:5
--> $DIR/unnecessary_literal_unwrap.rs:47:5
|
LL | Err::<(), _>(1).unwrap_err();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -232,7 +244,7 @@ LL + 1;
|

error: used `expect_err()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:46:5
--> $DIR/unnecessary_literal_unwrap.rs:48:5
|
LL | Err::<(), _>(1).expect_err("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -244,7 +256,7 @@ LL + 1;
|

error: used `unwrap()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:47:5
--> $DIR/unnecessary_literal_unwrap.rs:49:5
|
LL | Err::<(), _>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -255,7 +267,7 @@ LL | panic!("{:?}", 1);
| ~~~~~~~~~~~~~~ ~

error: used `expect()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:48:5
--> $DIR/unnecessary_literal_unwrap.rs:50:5
|
LL | Err::<(), _>(1).expect("this always happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -266,7 +278,7 @@ LL | panic!("{1}: {:?}", 1, "this always happens");
| ~~~~~~~~~~~~~~~~~~~ ~

error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:52:16
--> $DIR/unnecessary_literal_unwrap.rs:54:16
|
LL | let _val = Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -278,7 +290,7 @@ LL + let _val = 1;
|

error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:53:16
--> $DIR/unnecessary_literal_unwrap.rs:55:16
|
LL | let _val = Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -290,7 +302,7 @@ LL + let _val = 1;
|

error: used `unwrap_or_else()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:54:16
--> $DIR/unnecessary_literal_unwrap.rs:56:16
|
LL | let _val = Some(1).unwrap_or_else(|| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -302,7 +314,7 @@ LL + let _val = 1;
|

error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:56:5
--> $DIR/unnecessary_literal_unwrap.rs:58:5
|
LL | Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -314,7 +326,7 @@ LL + 1;
|

error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:57:5
--> $DIR/unnecessary_literal_unwrap.rs:59:5
|
LL | Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -326,7 +338,7 @@ LL + 1;
|

error: used `unwrap_or_else()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:58:5
--> $DIR/unnecessary_literal_unwrap.rs:60:5
|
LL | Some(1).unwrap_or_else(|| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -338,7 +350,7 @@ LL + 1;
|

error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:62:16
--> $DIR/unnecessary_literal_unwrap.rs:64:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -350,7 +362,7 @@ LL + let _val = 1;
|

error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:63:16
--> $DIR/unnecessary_literal_unwrap.rs:65:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -362,7 +374,7 @@ LL + let _val = 1;
|

error: used `unwrap_or_else()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:64:16
--> $DIR/unnecessary_literal_unwrap.rs:66:16
|
LL | let _val = Ok::<_, ()>(1).unwrap_or_else(|_| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -374,7 +386,7 @@ LL + let _val = 1;
|

error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:66:5
--> $DIR/unnecessary_literal_unwrap.rs:68:5
|
LL | Ok::<_, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -386,7 +398,7 @@ LL + 1;
|

error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:67:5
--> $DIR/unnecessary_literal_unwrap.rs:69:5
|
LL | Ok::<_, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -398,7 +410,7 @@ LL + 1;
|

error: used `unwrap_or_else()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:68:5
--> $DIR/unnecessary_literal_unwrap.rs:70:5
|
LL | Ok::<_, ()>(1).unwrap_or_else(|_| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -409,5 +421,5 @@ LL - Ok::<_, ()>(1).unwrap_or_else(|_| 2);
LL + 1;
|

error: aborting due to 36 previous errors
error: aborting due to 38 previous errors

2 changes: 2 additions & 0 deletions tests/ui/unnecessary_literal_unwrap_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn unwrap_option_none() {
let val = None::<()>;
let _val2 = val.unwrap();
let _val2 = val.expect("this always happens");
let _val3: u8 = None.unwrap_or_default();
None::<()>.unwrap_or_default();
}

fn unwrap_result_ok() {
Expand Down
Loading

0 comments on commit 1cb1646

Please sign in to comment.