Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 8af25bd

Browse files
authored
fix: correctly handle FunctionExpressions for avoid-redundant-async (#1124)
* fix: correctly handle FunctionExpressions for avoid-redundant-async * chore: update example
1 parent 8feb0e9 commit 8af25bd

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* fix: correctly handle FunctionExpressions for [`avoid-redundant-async`](https://dartcodemetrics.dev/docs/rules/common/avoid-redundant-async).
56
* feat: support ignoring nesting for [`prefer-conditional-expressions`](https://dartcodemetrics.dev/docs/rules/common/prefer-conditional-expressions).
67
* fix: ignore Providers for ['avoid-returning-widgets'](https://dartcodemetrics.dev/docs/rules/common/avoid-returning-widgets).
78
* feat: add [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously).

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async/avoid_redundant_async_rule.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ class AvoidRedundantAsyncRule extends CommonRule {
3535

3636
source.unit.visitChildren(visitor);
3737

38-
return visitor.declarations.map((declaration) => createIssue(
39-
rule: this,
40-
location: nodeLocation(
41-
node: declaration,
42-
source: source,
43-
),
44-
message: _warningMessage,
45-
));
38+
return visitor.nodes.map(
39+
(node) => createIssue(
40+
rule: this,
41+
location: nodeLocation(
42+
node: node,
43+
source: source,
44+
),
45+
message: _warningMessage,
46+
),
47+
);
4648
}
4749
}

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async/visitor.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
part of 'avoid_redundant_async_rule.dart';
22

33
class _Visitor extends RecursiveAstVisitor<void> {
4-
final _declarations = <Declaration>[];
4+
final _nodes = <AstNode>[];
55

6-
Iterable<Declaration> get declarations => _declarations;
6+
Iterable<AstNode> get nodes => _nodes;
77

88
@override
99
void visitMethodDeclaration(MethodDeclaration node) {
@@ -15,16 +15,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
1515
);
1616

1717
if (!isOverride && _hasRedundantAsync(node.body)) {
18-
_declarations.add(node);
18+
_nodes.add(node);
1919
}
2020
}
2121

2222
@override
23-
void visitFunctionDeclaration(FunctionDeclaration node) {
24-
super.visitFunctionDeclaration(node);
23+
void visitFunctionExpression(FunctionExpression node) {
24+
super.visitFunctionExpression(node);
2525

26-
if (_hasRedundantAsync(node.functionExpression.body)) {
27-
_declarations.add(node);
26+
if (_hasRedundantAsync(node.body)) {
27+
_nodes.add(node);
2828
}
2929
}
3030

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async/avoid_redundant_async_rule_test.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ void main() {
2525

2626
RuleTestHelper.verifyIssues(
2727
issues: issues,
28-
startLines: [12, 22],
29-
startColumns: [1, 3],
28+
startLines: [12, 22, 59, 60],
29+
startColumns: [26, 3, 5, 22],
3030
locationTexts: [
31-
'Future<int> fastestBranch(Future<int> left, Future<int> right) async {\n'
31+
'(Future<int> left, Future<int> right) async {\n'
3232
' return Future.any([left, right]);\n'
3333
'}',
3434
"Future<String> anotherAsyncMethod() async => Future.value('value');",
35+
'() async => instance.someAsyncMethod()',
36+
'() async => instance.someAsyncMethod()',
3537
],
3638
messages: [
3739
"'async' keyword is redundant, consider removing it.",
3840
"'async' keyword is redundant, consider removing it.",
41+
"'async' keyword is redundant, consider removing it.",
42+
"'async' keyword is redundant, consider removing it.",
3943
],
4044
);
4145
});

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async/examples/example.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ class SomeClass {
4444
}
4545
}
4646
}
47+
48+
class WithFunctionField {
49+
final Function(void) onSelected;
50+
final Function(void) onSelectedNamed;
51+
52+
const WithFunctionField(this.onSelected, {required this.onSelectedNamed});
53+
}
54+
55+
void main() {
56+
final instance = SomeClass();
57+
58+
WithFunctionField(
59+
() async => instance.someAsyncMethod(), // LINT
60+
onSelectedNamed: () async => instance.someAsyncMethod(), // LINT
61+
);
62+
}

0 commit comments

Comments
 (0)