rewrite the PR
This commit is contained in:
parent
79cf41297a
commit
26cc55133e
@ -1,10 +1,11 @@
|
|||||||
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)`
|
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)`
|
||||||
|
|
||||||
use super::UNWRAP_OR_ELSE_DEFAULT;
|
use super::UNWRAP_OR_ELSE_DEFAULT;
|
||||||
use clippy_utils::{
|
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,
|
is_default_equivalent_ctor, is_diag_trait_item
|
||||||
ty::is_type_diagnostic_item,
|
|
||||||
};
|
};
|
||||||
|
use rustc_hir::ExprKind;
|
||||||
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
@ -23,9 +24,25 @@ pub(super) fn check<'tcx>(
|
|||||||
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
|
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
|
||||||
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
|
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
|
||||||
|
|
||||||
|
let is_default_eq = match &u_arg.kind {
|
||||||
|
ExprKind::Path(qpath) => {
|
||||||
|
if let Some(repl_def_id) = cx.qpath_res(qpath, u_arg.hir_id).opt_def_id() {
|
||||||
|
if is_diag_trait_item(cx, repl_def_id, sym::Default)
|
||||||
|
|| is_default_equivalent_ctor(cx, repl_def_id, qpath) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {false}
|
||||||
|
};
|
||||||
|
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if is_option || is_result;
|
if is_option || is_result;
|
||||||
if is_trait_item(cx, u_arg, sym::Default) || is_default_equivalent(cx, u_arg);
|
if is_trait_item(cx, u_arg, sym::Default) || is_default_eq;
|
||||||
then {
|
then {
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
|
||||||
|
@ -637,7 +637,7 @@ pub fn can_mut_borrow_both(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>) -
|
|||||||
|
|
||||||
/// Returns true if the `def_id` associated with the `path` is recognized as a "default-equivalent"
|
/// Returns true if the `def_id` associated with the `path` is recognized as a "default-equivalent"
|
||||||
/// constructor from the std library
|
/// constructor from the std library
|
||||||
fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
|
pub fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<'_>) -> bool {
|
||||||
let std_types_symbols = &[
|
let std_types_symbols = &[
|
||||||
sym::String,
|
sym::String,
|
||||||
sym::Vec,
|
sym::Vec,
|
||||||
|
@ -45,7 +45,7 @@ fn unwrap_or_else_default() {
|
|||||||
with_enum.unwrap_or_else(Enum::A);
|
with_enum.unwrap_or_else(Enum::A);
|
||||||
|
|
||||||
let with_new = Some(vec![1]);
|
let with_new = Some(vec![1]);
|
||||||
with_new.unwrap_or_else(Vec::new);
|
with_new.unwrap_or_default();
|
||||||
|
|
||||||
let with_err: Result<_, ()> = Ok(vec![1]);
|
let with_err: Result<_, ()> = Ok(vec![1]);
|
||||||
with_err.unwrap_or_else(make);
|
with_err.unwrap_or_else(make);
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
|
error: use of `.unwrap_or_else(..)` to construct default value
|
||||||
|
--> $DIR/unwrap_or_else_default.rs:48:5
|
||||||
|
|
|
||||||
|
LL | with_new.unwrap_or_else(Vec::new);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_new.unwrap_or_default()`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
|
||||||
|
|
||||||
error: use of `.unwrap_or_else(..)` to construct default value
|
error: use of `.unwrap_or_else(..)` to construct default value
|
||||||
--> $DIR/unwrap_or_else_default.rs:62:5
|
--> $DIR/unwrap_or_else_default.rs:62:5
|
||||||
|
|
|
|
||||||
LL | with_real_default.unwrap_or_else(<HasDefaultAndDuplicate as Default>::default);
|
LL | with_real_default.unwrap_or_else(<HasDefaultAndDuplicate as Default>::default);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_real_default.unwrap_or_default()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_real_default.unwrap_or_default()`
|
||||||
|
|
|
||||||
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
|
|
||||||
|
|
||||||
error: use of `.unwrap_or_else(..)` to construct default value
|
error: use of `.unwrap_or_else(..)` to construct default value
|
||||||
--> $DIR/unwrap_or_else_default.rs:65:5
|
--> $DIR/unwrap_or_else_default.rs:65:5
|
||||||
|
Loading…
Reference in New Issue
Block a user