Skip to content

Commit

Permalink
Auto merge of #11106 - syvb:literal_unwrap_ice, r=dswij
Browse files Browse the repository at this point in the history
[`unnecessary_literal_unwrap`]: Fix ICE on None.unwrap_or_default()

Fixes #11099
Fixes #11064

I'm running into #11099 (cc `@y21)` on my Rust codebase. Clippy ICEs on this code when evaluating the `unnecessary_literal_unwrap` lint:
```rust
fn main() {
    let val1: u8 = None.unwrap_or_default();
}
```

This fixes that ICE and adds an message specifically for that case:

```
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()`
```

This PR also fixes the same ICE with `None.unwrap_or_else` (by giving the generic error message for the lint in that case).

changelog: Fix ICE in `unnecessary_literal_unwrap` on `None.unwrap_or_default()`
  • Loading branch information
bors committed Jul 20, 2023
2 parents fbe292e + 8d258c1 commit fca1f9a
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 125 deletions.
31 changes: 31 additions & 0 deletions clippy_lints/src/methods/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use clippy_utils::{is_res_lang_ctor, last_path_segment, path_res, MaybePath};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::print::with_forced_trimmed_paths;

use super::UNNECESSARY_LITERAL_UNWRAP;

Expand All @@ -22,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<'_>,
Expand Down Expand Up @@ -84,6 +87,34 @@ pub(super) fn check(
}
Some(suggs)
},
("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()"))])
},
("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![
(
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +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() {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +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() {
Expand Down
Loading

0 comments on commit fca1f9a

Please sign in to comment.