2022-03-24 13:50:04 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
|
|
use clippy_utils::{eager_or_lazy, usage};
|
2020-08-28 14:10:16 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2020-11-05 13:29:48 +00:00
|
|
|
use rustc_span::sym;
|
2020-08-28 14:10:16 +00: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 14:30:50 +00:00
|
|
|
pub(super) fn check<'tcx>(
|
2020-08-28 14:10:16 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'_>,
|
2021-04-08 15:50:13 +00:00
|
|
|
recv: &'tcx hir::Expr<'_>,
|
|
|
|
arg: &'tcx hir::Expr<'_>,
|
2020-08-28 14:10:16 +00:00
|
|
|
simplify_using: &str,
|
|
|
|
) {
|
2021-10-02 23:51:01 +00: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);
|
2022-07-02 22:00:24 +00:00
|
|
|
let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
|
2020-08-28 14:10:16 +00:00
|
|
|
|
2022-07-02 22:00:24 +00:00
|
|
|
if is_option || is_result || is_bool {
|
2022-06-30 10:18:51 +00:00
|
|
|
if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
|
2022-06-11 19:25:25 +00:00
|
|
|
let body = cx.tcx.hir().body(body);
|
2020-09-24 12:49:22 +00:00
|
|
|
let body_expr = &body.value;
|
|
|
|
|
|
|
|
if usage::BindingUsageFinder::are_params_used(cx, body) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-28 14:10:16 +00:00
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
|
2020-08-28 14:10:16 +00:00
|
|
|
let msg = if is_option {
|
|
|
|
"unnecessary closure used to substitute value for `Option::None`"
|
2022-07-02 22:00:24 +00:00
|
|
|
} else if is_result {
|
2020-08-28 14:10:16 +00:00
|
|
|
"unnecessary closure used to substitute value for `Result::Err`"
|
2022-07-02 22:00:24 +00:00
|
|
|
} else {
|
|
|
|
"unnecessary closure used with `bool::then`"
|
2020-08-28 14:10:16 +00:00
|
|
|
};
|
2020-11-23 12:51:04 +00: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 14:10:16 +00:00
|
|
|
|
2022-03-24 13:50:04 +00:00
|
|
|
// This is a duplicate of what's happening in clippy_lints::methods::method_call,
|
|
|
|
// which isn't ideal, We want to get the method call span,
|
|
|
|
// but prefer to avoid changing the signature of the function itself.
|
|
|
|
if let hir::ExprKind::MethodCall(_, _, span) = expr.kind {
|
|
|
|
span_lint_and_then(cx, UNNECESSARY_LAZY_EVALUATIONS, expr.span, msg, |diag| {
|
|
|
|
diag.span_suggestion(
|
|
|
|
span,
|
|
|
|
&format!("use `{}(..)` instead", simplify_using),
|
|
|
|
format!("{}({})", simplify_using, snippet(cx, body_expr.span, "..")),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|