File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
src/tools/rust-analyzer/crates Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -239,4 +239,33 @@ mod tests {
239
239
240
240
check_assist_not_applicable ( remove_parentheses, r#"fn f() { $0(return 2) + 2 }"# ) ;
241
241
}
242
+
243
+ #[ test]
244
+ fn remove_parens_indirect_calls ( ) {
245
+ check_assist (
246
+ remove_parentheses,
247
+ r#"fn f(call: fn(usize), arg: usize) { $0(call)(arg); }"# ,
248
+ r#"fn f(call: fn(usize), arg: usize) { call(arg); }"# ,
249
+ ) ;
250
+ check_assist (
251
+ remove_parentheses,
252
+ r#"fn f<F>(call: F, arg: usize) where F: Fn(usize) { $0(call)(arg); }"# ,
253
+ r#"fn f<F>(call: F, arg: usize) where F: Fn(usize) { call(arg); }"# ,
254
+ ) ;
255
+
256
+ // Parentheses are necessary when calling a function-like pointer that is a member of a struct or union.
257
+ check_assist_not_applicable (
258
+ remove_parentheses,
259
+ r#"
260
+ struct Foo<T> {
261
+ t: T,
262
+ }
263
+
264
+ impl Foo<fn(usize)> {
265
+ fn foo(&self, arg: usize) {
266
+ $0(self.t)(arg);
267
+ }
268
+ }"# ,
269
+ ) ;
270
+ }
242
271
}
Original file line number Diff line number Diff line change @@ -27,6 +27,14 @@ impl Expr {
27
27
}
28
28
29
29
fn needs_parens_in_expr ( & self , parent : & Expr ) -> bool {
30
+ // Parentheses are necessary when calling a function-like pointer that is a member of a struct or union
31
+ // (e.g. `(a.f)()`).
32
+ let is_parent_call_expr = matches ! ( parent, ast:: Expr :: CallExpr ( _) ) ;
33
+ let is_field_expr = matches ! ( self , ast:: Expr :: FieldExpr ( _) ) ;
34
+ if is_parent_call_expr && is_field_expr {
35
+ return true ;
36
+ }
37
+
30
38
// Special-case block weirdness
31
39
if parent. child_is_followed_by_a_block ( ) {
32
40
use Expr :: * ;
You can’t perform that action at this time.
0 commit comments