Skip to content

fix: redundant_closure_call lint for closures with block #15144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions clippy_lints/src/redundant_closure_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use hir::Param;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
use rustc_hir::{
ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, ExprKind, Node, intravisit as hir_visit,
};
use rustc_hir::{ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, ExprKind, intravisit as hir_visit};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty;
Expand Down Expand Up @@ -198,15 +196,15 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
hint = hint.asyncify();
}

let is_in_fn_call_arg = if let Node::Expr(expr) = cx.tcx.parent_hir_node(expr.hir_id) {
matches!(expr.kind, ExprKind::Call(_, _))
} else {
false
};

// avoid clippy::double_parens
if !is_in_fn_call_arg {
hint = hint.maybe_paren();
// If the closure body is a block with a single expression, suggest just the inner expression,
// not the block. Example: `(|| { Some(true) })()` should suggest
// `Some(true)`
if let ExprKind::Block(block, _) = body.kind
&& block.stmts.is_empty()
&& let Some(expr) = block.expr
{
hint = Sugg::hir_with_context(cx, expr, full_expr.span.ctxt(), "..", &mut applicability)
.maybe_paren();
}

diag.span_suggestion(full_expr.span, "try doing something like", hint, applicability);
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/redundant_closure_call_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn issue9956() {
//~^ redundant_closure_call

// immediately calling only one closure, so we can't remove the other ones
let a = (|| || 123);
let a = || || 123;
//~^ redundant_closure_call
dbg!(a()());

Expand Down Expand Up @@ -144,3 +144,15 @@ fn issue_12358() {
// different.
make_closure!(x)();
}

#[rustfmt::skip]
fn issue_9583() {
Some(true) == Some(true);
//~^ redundant_closure_call
Some(true) == Some(true);
//~^ redundant_closure_call
Some(if 1 > 2 {1} else {2}) == Some(2);
//~^ redundant_closure_call
Some( 1 > 2 ) == Some(true);
//~^ redundant_closure_call
}
12 changes: 12 additions & 0 deletions tests/ui/redundant_closure_call_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ fn issue_12358() {
// different.
make_closure!(x)();
}

#[rustfmt::skip]
fn issue_9583() {
(|| { Some(true) })() == Some(true);
//~^ redundant_closure_call
(|| Some(true))() == Some(true);
//~^ redundant_closure_call
(|| { Some(if 1 > 2 {1} else {2}) })() == Some(2);
//~^ redundant_closure_call
(|| { Some( 1 > 2 ) })() == Some(true);
//~^ redundant_closure_call
}
28 changes: 26 additions & 2 deletions tests/ui/redundant_closure_call_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:63:13
|
LL | let a = (|| || || 123)();
| ^^^^^^^^^^^^^^^^ help: try doing something like: `(|| || 123)`
| ^^^^^^^^^^^^^^^^ help: try doing something like: `|| || 123`

error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:68:13
Expand Down Expand Up @@ -145,5 +145,29 @@ error: try not to call a closure in the expression where it is declared
LL | std::convert::identity((|| 13_i32 + 36_i32)()).leading_zeros();
| ^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `13_i32 + 36_i32`

error: aborting due to 17 previous errors
error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:150:5
|
LL | (|| { Some(true) })() == Some(true);
| ^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `Some(true)`

error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:152:5
|
LL | (|| Some(true))() == Some(true);
| ^^^^^^^^^^^^^^^^^ help: try doing something like: `Some(true)`

error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:154:5
|
LL | (|| { Some(if 1 > 2 {1} else {2}) })() == Some(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `Some(if 1 > 2 {1} else {2})`

error: try not to call a closure in the expression where it is declared
--> tests/ui/redundant_closure_call_fixable.rs:156:5
|
LL | (|| { Some( 1 > 2 ) })() == Some(true);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `Some( 1 > 2 )`

error: aborting due to 21 previous errors

Loading