1 //! Lint for `some_result_or_option.unwrap_or_else(Default::default)`
3 use super::UNWRAP_OR_ELSE_DEFAULT
;
5 diagnostics
::span_lint_and_sugg
, is_trait_item
, source
::snippet_with_applicability
, ty
::is_type_diagnostic_item
,
7 use rustc_errors
::Applicability
;
9 use rustc_lint
::LateContext
;
12 pub(super) fn check
<'tcx
>(
13 cx
: &LateContext
<'tcx
>,
14 expr
: &'tcx hir
::Expr
<'_
>,
15 recv
: &'tcx hir
::Expr
<'_
>,
16 u_arg
: &'tcx hir
::Expr
<'_
>,
18 // something.unwrap_or_else(Default::default)
19 // ^^^^^^^^^- recv ^^^^^^^^^^^^^^^^- u_arg
20 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- expr
21 let recv_ty
= cx
.typeck_results().expr_ty(recv
);
22 let is_option
= is_type_diagnostic_item(cx
, recv_ty
, sym
::option_type
);
23 let is_result
= is_type_diagnostic_item(cx
, recv_ty
, sym
::result_type
);
26 if is_option
|| is_result
;
27 if is_trait_item(cx
, u_arg
, sym
::Default
);
29 let mut applicability
= Applicability
::MachineApplicable
;
33 UNWRAP_OR_ELSE_DEFAULT
,
35 "use of `.unwrap_or_else(..)` to construct default value",
38 "{}.unwrap_or_default()",
39 snippet_with_applicability(cx
, recv
.span
, "..", &mut applicability
)