2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
|
|
use clippy_utils::{eager_or_lazy, usage};
|
2020-08-28 09:10:16 -05:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_span::sym;
|
2020-08-28 09:10:16 -05:00
|
|
|
|
|
|
|
use super::UNNECESSARY_LAZY_EVALUATIONS;
|
|
|
|
|
|
|
|
/// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
|
|
|
|
/// replaced with `<fn>(return value of simple closure)`
|
2021-03-12 08:30:50 -06:00
|
|
|
pub(super) fn check<'tcx>(
|
2020-08-28 09:10:16 -05:00
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'_>,
|
2021-04-08 10:50:13 -05:00
|
|
|
recv: &'tcx hir::Expr<'_>,
|
|
|
|
arg: &'tcx hir::Expr<'_>,
|
2020-08-28 09:10:16 -05:00
|
|
|
simplify_using: &str,
|
|
|
|
) {
|
2021-10-02 18:51:01 -05:00
|
|
|
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
|
|
|
|
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
|
2020-08-28 09:10:16 -05:00
|
|
|
|
|
|
|
if is_option || is_result {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let hir::ExprKind::Closure(_, _, eid, _, _) = arg.kind {
|
2020-08-28 09:10:16 -05:00
|
|
|
let body = cx.tcx.hir().body(eid);
|
2020-09-24 07:49:22 -05:00
|
|
|
let body_expr = &body.value;
|
|
|
|
|
|
|
|
if usage::BindingUsageFinder::are_params_used(cx, body) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-28 09:10:16 -05:00
|
|
|
|
2020-09-24 07:49:22 -05:00
|
|
|
if eager_or_lazy::is_eagerness_candidate(cx, body_expr) {
|
2020-08-28 09:10:16 -05:00
|
|
|
let msg = if is_option {
|
|
|
|
"unnecessary closure used to substitute value for `Option::None`"
|
|
|
|
} else {
|
|
|
|
"unnecessary closure used to substitute value for `Result::Err`"
|
|
|
|
};
|
2020-11-23 06:51:04 -06:00
|
|
|
let applicability = if body
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
// bindings are checked to be unused above
|
|
|
|
.all(|param| matches!(param.pat.kind, hir::PatKind::Binding(..) | hir::PatKind::Wild))
|
|
|
|
{
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
} else {
|
|
|
|
// replacing the lambda may break type inference
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
};
|
2020-08-28 09:10:16 -05:00
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNNECESSARY_LAZY_EVALUATIONS,
|
|
|
|
expr.span,
|
|
|
|
msg,
|
2021-03-12 08:30:50 -06:00
|
|
|
&format!("use `{}` instead", simplify_using),
|
2020-08-28 09:10:16 -05:00
|
|
|
format!(
|
|
|
|
"{0}.{1}({2})",
|
2021-04-08 10:50:13 -05:00
|
|
|
snippet(cx, recv.span, ".."),
|
2020-08-28 09:10:16 -05:00
|
|
|
simplify_using,
|
2020-09-24 07:49:22 -05:00
|
|
|
snippet(cx, body_expr.span, ".."),
|
2020-08-28 09:10:16 -05:00
|
|
|
),
|
2020-11-23 06:51:04 -06:00
|
|
|
applicability,
|
2020-08-28 09:10:16 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|