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

Commit 48229a1

Browse files
authored
Merge d49d7a4 into 7f9e642
2 parents 7f9e642 + d49d7a4 commit 48229a1

File tree

3 files changed

+36
-1
lines changed
  • lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async
  • test/src/analyzers/lint_analyzer/rules/rules_list/avoid_redundant_async/examples

3 files changed

+36
-1
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: partially handle normal completion function body for [`avoid-redundant-async`](https://dartcodemetrics.dev/docs/rules/common/avoid-redundant-async).
56
* fix: ignore enum constant arguments for [`no-magic-number`](https://dartcodemetrics.dev/docs/rules/common/no-magic-number).
67
* fix: correctly handle prefixed enums and static instance fields for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
78
* feat: add static code diagnostic [`prefer-provide-intl-description`](https://dartcodemetrics.dev/docs/rules/intl/prefer-provide-intl-description).

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,40 @@ class _Visitor extends RecursiveAstVisitor<void> {
3737
}
3838
}
3939

40-
final asyncVisitor = _AsyncVisitor();
40+
final asyncVisitor = _AsyncVisitor(body);
4141
body.parent?.visitChildren(asyncVisitor);
4242

43+
if (asyncVisitor._allReturns.isNotEmpty) {
44+
return !asyncVisitor.hasValidAsync &&
45+
asyncVisitor._allReturns.length !=
46+
asyncVisitor._returnsInsideIf.length;
47+
}
48+
4349
return !asyncVisitor.hasValidAsync;
4450
}
4551
}
4652

4753
class _AsyncVisitor extends RecursiveAstVisitor<void> {
54+
final FunctionBody body;
55+
4856
bool hasValidAsync = false;
4957

58+
final _allReturns = <ReturnStatement>{};
59+
final _returnsInsideIf = <ReturnStatement>{};
60+
61+
_AsyncVisitor(this.body);
62+
5063
@override
5164
void visitReturnStatement(ReturnStatement node) {
5265
super.visitReturnStatement(node);
5366

5467
final type = node.expression?.staticType;
5568

69+
_allReturns.add(node);
70+
if (_isInsideIfStatement(node, body)) {
71+
_returnsInsideIf.add(node);
72+
}
73+
5674
if (type == null ||
5775
!type.isDartAsyncFuture ||
5876
type.nullabilitySuffix == NullabilitySuffix.question) {
@@ -80,4 +98,12 @@ class _AsyncVisitor extends RecursiveAstVisitor<void> {
8098

8199
hasValidAsync = true;
82100
}
101+
102+
bool _isInsideIfStatement(ReturnStatement node, FunctionBody body) {
103+
final parent = node.thisOrAncestorMatching(
104+
(parent) => parent == body || parent is IfStatement,
105+
);
106+
107+
return parent is IfStatement;
108+
}
83109
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ void main() {
5959
() async => instance.someAsyncMethod(), // LINT
6060
onSelectedNamed: () async => instance.someAsyncMethod(), // LINT
6161
);
62+
63+
WithFunctionField(onSelectedNamed: () async {
64+
if (shouldRefresh) return apiCall();
65+
});
66+
}
67+
68+
Future<void> apiCall() {
69+
return Future.value();
6270
}

0 commit comments

Comments
 (0)