Skip to content

Commit 26cc551

Browse files
committed
rewrite the PR
1 parent 79cf412 commit 26cc551

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

clippy_lints/src/methods/unwrap_or_else_default.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)`
22
33
use super::UNWRAP_OR_ELSE_DEFAULT;
4-
use clippy_utils::{
5-
diagnostics::span_lint_and_sugg, is_default_equivalent, is_trait_item, source::snippet_with_applicability,
6-
ty::is_type_diagnostic_item,
4+
use clippy_utils::{diagnostics::span_lint_and_sugg, is_trait_item, source::snippet_with_applicability, ty::is_type_diagnostic_item,
5+
is_default_equivalent_ctor, is_diag_trait_item
76
};
7+
use rustc_hir::ExprKind;
8+
89
use rustc_errors::Applicability;
910
use rustc_hir as hir;
1011
use rustc_lint::LateContext;
@@ -23,9 +24,25 @@ pub(super) fn check<'tcx>(
2324
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
2425
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
2526

27+
let is_default_eq = match &u_arg.kind {
28+
ExprKind::Path(qpath) => {
29+
if let Some(repl_def_id) = cx.qpath_res(qpath, u_arg.hir_id).opt_def_id() {
30+
if is_diag_trait_item(cx, repl_def_id, sym::Default)
31+
|| is_default_equivalent_ctor(cx, repl_def_id, qpath) {
32+
true
33+
} else {
34+
false
35+
}
36+
} else {
37+
false
38+
}
39+
},
40+
_ => {false}
41+
};
42+
2643
if_chain! {
2744
if is_option || is_result;
28-
if is_trait_item(cx, u_arg, sym::Default) || is_default_equivalent(cx, u_arg);
45+
if is_trait_item(cx, u_arg, sym::Default) || is_default_eq;
2946
then {
3047
let mut applicability = Applicability::MachineApplicable;
3148

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
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-
fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
640+
pub fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
641641
let std_types_symbols = &[
642642
sym::String,
643643
sym::Vec,

tests/ui/unwrap_or_else_default.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn unwrap_or_else_default() {
4545
with_enum.unwrap_or_else(Enum::A);
4646

4747
let with_new = Some(vec![1]);
48-
with_new.unwrap_or_else(Vec::new);
48+
with_new.unwrap_or_default();
4949

5050
let with_err: Result<_, ()> = Ok(vec![1]);
5151
with_err.unwrap_or_else(make);

tests/ui/unwrap_or_else_default.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
error: use of `.unwrap_or_else(..)` to construct default value
2+
--> $DIR/unwrap_or_else_default.rs:48:5
3+
|
4+
LL | with_new.unwrap_or_else(Vec::new);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_new.unwrap_or_default()`
6+
|
7+
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
8+
19
error: use of `.unwrap_or_else(..)` to construct default value
210
--> $DIR/unwrap_or_else_default.rs:62:5
311
|
412
LL | with_real_default.unwrap_or_else(<HasDefaultAndDuplicate as Default>::default);
513
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_real_default.unwrap_or_default()`
6-
|
7-
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
814

915
error: use of `.unwrap_or_else(..)` to construct default value
1016
--> $DIR/unwrap_or_else_default.rs:65:5

0 commit comments

Comments
 (0)