1 use crate::utils::{eager_or_lazy, usage};
2 use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_sugg};
3 use rustc_errors::Applicability;
5 use rustc_lint::LateContext;
8 use super::UNNECESSARY_LAZY_EVALUATIONS;
10 /// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
11 /// replaced with `<fn>(return value of simple closure)`
12 pub(super) fn lint<'tcx>(
13 cx: &LateContext<'tcx>,
14 expr: &'tcx hir::Expr<'_>,
15 args: &'tcx [hir::Expr<'_>],
18 let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym::option_type);
19 let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym::result_type);
21 if is_option || is_result {
22 if let hir::ExprKind::Closure(_, _, eid, _, _) = args[1].kind {
23 let body = cx.tcx.hir().body(eid);
24 let body_expr = &body.value;
26 if usage::BindingUsageFinder::are_params_used(cx, body) {
30 if eager_or_lazy::is_eagerness_candidate(cx, body_expr) {
31 let msg = if is_option {
32 "unnecessary closure used to substitute value for `Option::None`"
34 "unnecessary closure used to substitute value for `Result::Err`"
39 UNNECESSARY_LAZY_EVALUATIONS,
42 &format!("Use `{}` instead", simplify_using),
45 snippet(cx, args[0].span, ".."),
47 snippet(cx, body_expr.span, ".."),
49 Applicability::MachineApplicable,