Skip to content

Commit

Permalink
Imrpove unwrap_or_else_default
Browse files Browse the repository at this point in the history
  • Loading branch information
pmnoxx committed Dec 24, 2021
1 parent 9ae4043 commit 79cf412
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/unwrap_or_else_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use super::UNWRAP_OR_ELSE_DEFAULT;
use clippy_utils::{
diagnostics::span_lint_and_sugg, is_trait_item, source::snippet_with_applicability, ty::is_type_diagnostic_item,
diagnostics::span_lint_and_sugg, is_default_equivalent, is_trait_item, source::snippet_with_applicability,
ty::is_type_diagnostic_item,
};
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand All @@ -24,7 +25,7 @@ pub(super) fn check<'tcx>(

if_chain! {
if is_option || is_result;
if is_trait_item(cx, u_arg, sym::Default);
if is_trait_item(cx, u_arg, sym::Default) || is_default_equivalent(cx, u_arg);
then {
let mut applicability = Applicability::MachineApplicable;

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/unwrap_or_else_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ fn unwrap_or_else_default() {

let with_default_type = Some(1);
with_default_type.unwrap_or_default();

let with_default_type: Option<Vec<u64>> = None;
with_default_type.unwrap_or_default();
}

fn main() {}
3 changes: 3 additions & 0 deletions tests/ui/unwrap_or_else_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ fn unwrap_or_else_default() {

let with_default_type = Some(1);
with_default_type.unwrap_or_else(u64::default);

let with_default_type: Option<Vec<u64>> = None;
with_default_type.unwrap_or_else(Vec::new);
}

fn main() {}

0 comments on commit 79cf412

Please sign in to comment.