Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3318249

Browse files
don't add paren on occurrences that is in call args
1 parent 40b6aa0 commit 3318249

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::sugg::Sugg;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
8-
use rustc_hir::{intravisit as hir_visit, CoroutineKind, CoroutineSource};
8+
use rustc_hir::{intravisit as hir_visit, CoroutineKind, CoroutineSource, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::hir::nested_filter;
1111
use rustc_middle::lint::in_external_macro;
@@ -176,12 +176,19 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
176176
hint = hint.asyncify();
177177
}
178178

179-
diag.span_suggestion(
180-
full_expr.span,
181-
"try doing something like",
182-
hint.maybe_par(),
183-
applicability,
184-
);
179+
let is_in_fn_call_arg = clippy_utils::get_parent_node(cx.tcx, expr.hir_id)
180+
.map(|x| match x {
181+
Node::Expr(expr) => matches!(expr.kind, hir::ExprKind::Call(_, _)),
182+
_ => false,
183+
})
184+
.unwrap_or(false);
185+
186+
// avoid clippy::double_parens
187+
if !is_in_fn_call_arg {
188+
hint = hint.maybe_par()
189+
};
190+
191+
diag.span_suggestion(full_expr.span, "try doing something like", hint, applicability);
185192
}
186193
},
187194
);

tests/ui/redundant_closure_call_fixable.fixed

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![allow(clippy::redundant_async_block)]
44
#![allow(clippy::type_complexity)]
55
#![allow(unused)]
6-
#![allow(clippy::double_parens)]
76

87
async fn something() -> u32 {
98
21
@@ -87,7 +86,7 @@ fn issue9956() {
8786
}
8887

8988
async fn issue11357() {
90-
(async {}).await;
89+
async {}.await;
9190
}
9291

9392
mod issue11707 {
@@ -96,6 +95,10 @@ mod issue11707 {
9695
fn spawn_on(fut: impl Future<Output = ()>) {}
9796

9897
fn demo() {
99-
spawn_on((async move {}));
98+
spawn_on(async move {});
10099
}
101100
}
101+
102+
fn avoid_double_parens() {
103+
std::convert::identity(13_i32 + 36_i32).leading_zeros();
104+
}

tests/ui/redundant_closure_call_fixable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![allow(clippy::redundant_async_block)]
44
#![allow(clippy::type_complexity)]
55
#![allow(unused)]
6-
#![allow(clippy::double_parens)]
76

87
async fn something() -> u32 {
98
21
@@ -99,3 +98,7 @@ mod issue11707 {
9998
spawn_on((|| async move {})());
10099
}
101100
}
101+
102+
fn avoid_double_parens() {
103+
std::convert::identity((|| 13_i32 + 36_i32)()).leading_zeros();
104+
}

tests/ui/redundant_closure_call_fixable.stderr

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: try not to call a closure in the expression where it is declared
2-
--> $DIR/redundant_closure_call_fixable.rs:17:13
2+
--> $DIR/redundant_closure_call_fixable.rs:16:13
33
|
44
LL | let a = (|| 42)();
55
| ^^^^^^^^^ help: try doing something like: `42`
@@ -8,7 +8,7 @@ LL | let a = (|| 42)();
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure_call)]`
99

1010
error: try not to call a closure in the expression where it is declared
11-
--> $DIR/redundant_closure_call_fixable.rs:18:13
11+
--> $DIR/redundant_closure_call_fixable.rs:17:13
1212
|
1313
LL | let b = (async || {
1414
| _____________^
@@ -28,7 +28,7 @@ LL ~ };
2828
|
2929

3030
error: try not to call a closure in the expression where it is declared
31-
--> $DIR/redundant_closure_call_fixable.rs:23:13
31+
--> $DIR/redundant_closure_call_fixable.rs:22:13
3232
|
3333
LL | let c = (|| {
3434
| _____________^
@@ -48,13 +48,13 @@ LL ~ };
4848
|
4949

5050
error: try not to call a closure in the expression where it is declared
51-
--> $DIR/redundant_closure_call_fixable.rs:28:13
51+
--> $DIR/redundant_closure_call_fixable.rs:27:13
5252
|
5353
LL | let d = (async || something().await)();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { something().await }`
5555

5656
error: try not to call a closure in the expression where it is declared
57-
--> $DIR/redundant_closure_call_fixable.rs:37:13
57+
--> $DIR/redundant_closure_call_fixable.rs:36:13
5858
|
5959
LL | (|| m!())()
6060
| ^^^^^^^^^^^ help: try doing something like: `m!()`
@@ -65,7 +65,7 @@ LL | m2!();
6565
= note: this error originates in the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info)
6666

6767
error: try not to call a closure in the expression where it is declared
68-
--> $DIR/redundant_closure_call_fixable.rs:32:13
68+
--> $DIR/redundant_closure_call_fixable.rs:31:13
6969
|
7070
LL | (|| 0)()
7171
| ^^^^^^^^ help: try doing something like: `0`
@@ -76,64 +76,70 @@ LL | m2!();
7676
= note: this error originates in the macro `m` which comes from the expansion of the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info)
7777

7878
error: try not to call a closure in the expression where it is declared
79-
--> $DIR/redundant_closure_call_fixable.rs:45:16
79+
--> $DIR/redundant_closure_call_fixable.rs:44:16
8080
|
8181
LL | assert_eq!((|| || 43)()(), 42);
8282
| ^^^^^^^^^^^^^^ help: try doing something like: `43`
8383

8484
error: try not to call a closure in the expression where it is declared
85-
--> $DIR/redundant_closure_call_fixable.rs:54:10
85+
--> $DIR/redundant_closure_call_fixable.rs:53:10
8686
|
8787
LL | dbg!((|| 42)());
8888
| ^^^^^^^^^ help: try doing something like: `42`
8989

9090
error: try not to call a closure in the expression where it is declared
91-
--> $DIR/redundant_closure_call_fixable.rs:57:13
91+
--> $DIR/redundant_closure_call_fixable.rs:56:13
9292
|
9393
LL | let a = (|| || || 123)();
9494
| ^^^^^^^^^^^^^^^^ help: try doing something like: `(|| || 123)`
9595

9696
error: try not to call a closure in the expression where it is declared
97-
--> $DIR/redundant_closure_call_fixable.rs:61:13
97+
--> $DIR/redundant_closure_call_fixable.rs:60:13
9898
|
9999
LL | let a = (|| || || || async || 1)()()()()();
100100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { 1 }`
101101

102102
error: try not to call a closure in the expression where it is declared
103-
--> $DIR/redundant_closure_call_fixable.rs:70:13
103+
--> $DIR/redundant_closure_call_fixable.rs:69:13
104104
|
105105
LL | let a = (|| echo!(|| echo!(|| 1)))()()();
106106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `1`
107107

108108
error: try not to call a closure in the expression where it is declared
109-
--> $DIR/redundant_closure_call_fixable.rs:72:13
109+
--> $DIR/redundant_closure_call_fixable.rs:71:13
110110
|
111111
LL | let a = (|| echo!((|| 123)))()();
112112
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `123`
113113

114114
error: try not to call a closure in the expression where it is declared
115-
--> $DIR/redundant_closure_call_fixable.rs:85:11
115+
--> $DIR/redundant_closure_call_fixable.rs:84:11
116116
|
117117
LL | bar()((|| || 42)()(), 5);
118118
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
119119

120120
error: try not to call a closure in the expression where it is declared
121-
--> $DIR/redundant_closure_call_fixable.rs:86:9
121+
--> $DIR/redundant_closure_call_fixable.rs:85:9
122122
|
123123
LL | foo((|| || 42)()(), 5);
124124
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
125125

126126
error: try not to call a closure in the expression where it is declared
127-
--> $DIR/redundant_closure_call_fixable.rs:90:5
127+
--> $DIR/redundant_closure_call_fixable.rs:89:5
128128
|
129129
LL | (|| async {})().await;
130-
| ^^^^^^^^^^^^^^^ help: try doing something like: `(async {})`
130+
| ^^^^^^^^^^^^^^^ help: try doing something like: `async {}`
131131

132132
error: try not to call a closure in the expression where it is declared
133-
--> $DIR/redundant_closure_call_fixable.rs:99:18
133+
--> $DIR/redundant_closure_call_fixable.rs:98:18
134134
|
135135
LL | spawn_on((|| async move {})());
136-
| ^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `(async move {})`
136+
| ^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async move {}`
137+
138+
error: try not to call a closure in the expression where it is declared
139+
--> $DIR/redundant_closure_call_fixable.rs:103:28
140+
|
141+
LL | std::convert::identity((|| 13_i32 + 36_i32)()).leading_zeros();
142+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `13_i32 + 36_i32`
137143

138-
error: aborting due to 16 previous errors
144+
error: aborting due to 17 previous errors
139145

0 commit comments

Comments
 (0)