Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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