Skip to content

Commit 01b7411

Browse files
committed
Refactor
1 parent 4b3a87f commit 01b7411

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

clippy_lints/src/methods/unwrap_or_else_default.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use super::UNWRAP_OR_ELSE_DEFAULT;
44
use clippy_utils::{
5-
diagnostics::span_lint_and_sugg, is_default_equivalent_ctor, is_diag_trait_item, is_trait_item,
6-
source::snippet_with_applicability, ty::is_type_diagnostic_item,
5+
diagnostics::span_lint_and_sugg, is_default_equivalent_call, is_trait_item, source::snippet_with_applicability,
6+
ty::is_type_diagnostic_item,
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir as hir;
@@ -23,21 +23,9 @@ pub(super) fn check<'tcx>(
2323
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
2424
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
2525

26-
let is_default_eq = match &u_arg.kind {
27-
hir::ExprKind::Path(qpath) => {
28-
if let Some(repl_def_id) = cx.qpath_res(qpath, u_arg.hir_id).opt_def_id() {
29-
is_diag_trait_item(cx, repl_def_id, sym::Default)
30-
|| is_default_equivalent_ctor(cx, repl_def_id, qpath)
31-
} else {
32-
false
33-
}
34-
},
35-
_ => false,
36-
};
37-
3826
if_chain! {
3927
if is_option || is_result;
40-
if is_trait_item(cx, u_arg, sym::Default) || is_default_eq;
28+
if is_trait_item(cx, u_arg, sym::Default) || is_default_equivalent_call(cx, u_arg);
4129
then {
4230
let mut applicability = Applicability::MachineApplicable;
4331

clippy_utils/src/lib.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ pub fn can_mut_borrow_both(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>) -
637637

638638
/// Returns true if the `def_id` associated with the `path` is recognized as a "default-equivalent"
639639
/// constructor from the std library
640-
pub fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
640+
fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
641641
let std_types_symbols = &[
642642
sym::String,
643643
sym::Vec,
@@ -664,6 +664,22 @@ pub fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QP
664664
false
665665
}
666666

667+
/// Return true if the expr is equal to `Default::default` when evaluated.
668+
pub fn is_default_equivalent_call(cx: &LateContext<'_>, repl_func: &Expr<'_>) -> bool {
669+
if_chain! {
670+
if let hir::ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
671+
if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
672+
if is_diag_trait_item(cx, repl_def_id, sym::Default)
673+
|| is_default_equivalent_ctor(cx, repl_def_id, repl_func_qpath);
674+
then {
675+
true
676+
}
677+
else {
678+
false
679+
}
680+
}
681+
}
682+
667683
/// Returns true if the expr is equal to `Default::default()` of it's type when evaluated.
668684
/// It doesn't cover all cases, for example indirect function calls (some of std
669685
/// functions are supported) but it is the best we have.
@@ -686,18 +702,7 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
686702
false
687703
}
688704
},
689-
ExprKind::Call(repl_func, _) => if_chain! {
690-
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
691-
if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
692-
if is_diag_trait_item(cx, repl_def_id, sym::Default)
693-
|| is_default_equivalent_ctor(cx, repl_def_id, repl_func_qpath);
694-
then {
695-
true
696-
}
697-
else {
698-
false
699-
}
700-
},
705+
ExprKind::Call(repl_func, _) => is_default_equivalent_call(cx, repl_func),
701706
ExprKind::Path(qpath) => is_lang_ctor(cx, qpath, OptionNone),
702707
ExprKind::AddrOf(rustc_hir::BorrowKind::Ref, _, expr) => matches!(expr.kind, ExprKind::Array([])),
703708
_ => false,

0 commit comments

Comments
 (0)