Skip to content

fix: manual_unwrap_or_default suggests falsely when condition type is uncertain #13889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
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
34 changes: 32 additions & 2 deletions clippy_lints/src/manual_unwrap_or_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use rustc_span::sym;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::higher::IfLetOrMatch;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::implements_trait;
use clippy_utils::{is_default_equivalent, is_in_const_context, peel_blocks, span_contains_comment};
use clippy_utils::ty::{expr_type_is_certain, implements_trait};
use clippy_utils::{is_default_equivalent, is_in_const_context, path_res, peel_blocks, span_contains_comment};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -158,6 +158,36 @@ fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, exp
} else {
Applicability::MachineApplicable
};

// We now check if the condition is a None variant, in which case we need to specify the type
if path_res(cx, condition)
.opt_def_id()
.is_some_and(|id| Some(cx.tcx.parent(id)) == cx.tcx.lang_items().option_none_variant())
{
return span_lint_and_sugg(
cx,
MANUAL_UNWRAP_OR_DEFAULT,
expr.span,
format!("{expr_name} can be simplified with `.unwrap_or_default()`"),
"replace it with",
format!("{receiver}::<{expr_type}>.unwrap_or_default()"),
applicability,
);
}

// We check if the expression type is still uncertain, in which case we ask the user to specify it
if !expr_type_is_certain(cx, condition) {
return span_lint_and_sugg(
cx,
MANUAL_UNWRAP_OR_DEFAULT,
expr.span,
format!("{expr_name} can be simplified with `.unwrap_or_default()`"),
format!("ascribe the type {expr_type} and replace your expression with"),
format!("{receiver}.unwrap_or_default()"),
Applicability::Unspecified,
);
}

span_lint_and_sugg(
cx,
MANUAL_UNWRAP_OR_DEFAULT,
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/manual_unwrap_or_default_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@no-rustfix
fn issue_12670() {
// no auto: type not found
#[allow(clippy::match_result_ok)]
let _ = if let Some(x) = "1".parse().ok() {
x
} else {
i32::default()
};
let _ = if let Some(x) = None { x } else { i32::default() };
// auto fix with unwrap_or_default
let a: Option<i32> = None;
let _ = if let Some(x) = a { x } else { i32::default() };
let _ = if let Some(x) = Some(99) { x } else { i32::default() };
}
34 changes: 34 additions & 0 deletions tests/ui/manual_unwrap_or_default_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default_unfixable.rs:5:13
|
LL | let _ = if let Some(x) = "1".parse().ok() {
| _____________^
LL | | x
LL | | } else {
LL | | i32::default()
LL | | };
| |_____^ help: ascribe the type i32 and replace your expression with: `"1".parse().ok().unwrap_or_default()`
|
= note: `-D clippy::manual-unwrap-or-default` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_unwrap_or_default)]`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default_unfixable.rs:10:13
|
LL | let _ = if let Some(x) = None { x } else { i32::default() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `None::<i32>.unwrap_or_default()`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default_unfixable.rs:13:13
|
LL | let _ = if let Some(x) = a { x } else { i32::default() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.unwrap_or_default()`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default_unfixable.rs:14:13
|
LL | let _ = if let Some(x) = Some(99) { x } else { i32::default() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `Some(99).unwrap_or_default()`

error: aborting due to 4 previous errors