2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2020-07-26 19:07:07 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_ast::visit as ast_visit;
|
|
|
|
use rustc_ast::visit::Visitor as AstVisitor;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::intravisit as hir_visit;
|
|
|
|
use rustc_hir::intravisit::Visitor as HirVisitor;
|
2021-12-04 15:09:15 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
2022-01-15 22:07:52 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-07-26 19:07:07 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Detects closures called in the same expression where they
|
2020-07-26 19:07:07 +00:00
|
|
|
/// are defined.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is unnecessarily adding to the expression's
|
2020-07-26 19:07:07 +00:00
|
|
|
/// complexity.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2022-06-16 15:39:06 +00:00
|
|
|
/// ```rust
|
|
|
|
/// let a = (|| 42)();
|
|
|
|
/// ```
|
2020-07-26 19:07:07 +00:00
|
|
|
///
|
2022-06-16 15:39:06 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let a = 42;
|
2020-07-26 19:07:07 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2020-07-26 19:07:07 +00:00
|
|
|
pub REDUNDANT_CLOSURE_CALL,
|
|
|
|
complexity,
|
|
|
|
"throwaway closures called in the expression they are defined"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]);
|
|
|
|
|
|
|
|
// Used to find `return` statements or equivalents e.g., `?`
|
|
|
|
struct ReturnVisitor {
|
|
|
|
found_return: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReturnVisitor {
|
|
|
|
#[must_use]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self { found_return: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
|
|
|
|
fn visit_expr(&mut self, ex: &'ast ast::Expr) {
|
2021-09-08 14:31:47 +00:00
|
|
|
if let ast::ExprKind::Ret(_) | ast::ExprKind::Try(_) = ex.kind {
|
2020-07-26 19:07:07 +00:00
|
|
|
self.found_return = true;
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
ast_visit::walk_expr(self, ex);
|
2020-07-26 19:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for RedundantClosureCall {
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
2021-12-04 15:09:15 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
2020-07-26 19:07:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if_chain! {
|
|
|
|
if let ast::ExprKind::Call(ref paren, _) = expr.kind;
|
|
|
|
if let ast::ExprKind::Paren(ref closure) = paren.kind;
|
|
|
|
if let ast::ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind;
|
|
|
|
then {
|
|
|
|
let mut visitor = ReturnVisitor::new();
|
|
|
|
visitor.visit_expr(block);
|
|
|
|
if !visitor.found_return {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_CLOSURE_CALL,
|
|
|
|
expr.span,
|
2020-08-28 14:10:16 +00:00
|
|
|
"try not to call a closure in the expression where it is declared",
|
2020-07-26 19:07:07 +00:00
|
|
|
|diag| {
|
|
|
|
if decl.inputs.is_empty() {
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let hint =
|
|
|
|
snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
|
|
|
|
diag.span_suggestion(expr.span, "try doing something like", hint, app);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
|
|
|
|
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
|
2020-08-28 14:10:16 +00:00
|
|
|
fn count_closure_usage<'a, 'tcx>(
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
block: &'tcx hir::Block<'_>,
|
|
|
|
path: &'tcx hir::Path<'tcx>,
|
|
|
|
) -> usize {
|
|
|
|
struct ClosureUsageCount<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>,
|
2020-07-26 19:07:07 +00:00
|
|
|
path: &'tcx hir::Path<'tcx>,
|
|
|
|
count: usize,
|
2020-12-06 14:01:03 +00:00
|
|
|
}
|
2020-08-28 14:10:16 +00:00
|
|
|
impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
|
2022-01-15 22:07:52 +00:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-07-26 19:07:07 +00:00
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::Call(closure, _) = expr.kind;
|
|
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
|
|
|
|
if self.path.segments[0].ident == path.segments[0].ident;
|
|
|
|
if self.path.res == path.res;
|
2020-07-26 19:07:07 +00:00
|
|
|
then {
|
|
|
|
self.count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir_visit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2022-01-15 22:07:52 +00:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.cx.tcx.hir()
|
2020-07-26 19:07:07 +00:00
|
|
|
}
|
2020-12-06 14:01:03 +00:00
|
|
|
}
|
2020-08-28 14:10:16 +00:00
|
|
|
let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
|
2020-07-26 19:07:07 +00:00
|
|
|
closure_usage_count.visit_block(block);
|
|
|
|
closure_usage_count.count
|
|
|
|
}
|
|
|
|
|
|
|
|
for w in block.stmts.windows(2) {
|
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::StmtKind::Local(local) = w[0].kind;
|
|
|
|
if let Option::Some(t) = local.init;
|
2022-06-11 19:25:25 +00:00
|
|
|
if let hir::ExprKind::Closure { .. } = t.kind;
|
2020-07-26 19:07:07 +00:00
|
|
|
if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind;
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::StmtKind::Semi(second) = w[1].kind;
|
|
|
|
if let hir::ExprKind::Assign(_, call, _) = second.kind;
|
|
|
|
if let hir::ExprKind::Call(closure, _) = call.kind;
|
|
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
|
2020-07-26 19:07:07 +00:00
|
|
|
if ident == path.segments[0].ident;
|
2020-08-28 14:10:16 +00:00
|
|
|
if count_closure_usage(cx, block, path) == 1;
|
2020-07-26 19:07:07 +00:00
|
|
|
then {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_CLOSURE_CALL,
|
|
|
|
second.span,
|
|
|
|
"closure called just once immediately after it was declared",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|