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

Commit 4fdf618

Browse files
committed
fix: correctly handle dynamic type for avoid-passing-async-when-sync-expected
1 parent 4cecc46 commit 4fdf618

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* fix: correctly handle `dynamic` type for [`avoid-passing-async-when-sync-expected`](https://dcm.dev/docs/individuals/rules/common/avoid-passing-async-when-sync-expected).
56
* feat: add `allow-nullable` config option for [`avoid-returning-widgets`](https://dcm.dev/docs/individuals/rules/common/avoid-returning-widgets).
67
* fix: support `assert(mounted)` for [`use-setstate-synchronously`](https://dcm.dev/docs/individuals/rules/flutter/use-setstate-synchronously).
78
* fix: correctly support dartdoc tags for [`format-comment`](https://dcm.dev/docs/individuals/rules/common/format-comment).

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_passing_async_when_sync_expected/avoid_passing_async_when_sync_expected_rule.dart

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ class AvoidPassingAsyncWhenSyncExpectedRule extends CommonRule {
3636
return visitor.invalidArguments
3737
.map((invocation) => createIssue(
3838
rule: this,
39-
location: nodeLocation(
40-
node: invocation,
41-
source: source,
42-
),
39+
location: nodeLocation(node: invocation, source: source),
4340
message: _warningMessage,
4441
))
4542
.toList(growable: false);

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
2929
final parameterType = argument.staticParameterElement?.type;
3030
if (argumentType is FunctionType && parameterType is FunctionType) {
3131
if (argumentType.returnType.isDartAsyncFuture &&
32-
(!parameterType.returnType.isDartAsyncFuture &&
33-
!parameterType.returnType.isDartAsyncFutureOr)) {
32+
!_hasValidFutureType(parameterType.returnType)) {
3433
_invalidArguments.add(argument);
3534
}
3635
}
3736
}
3837
}
38+
39+
bool _hasValidFutureType(DartType type) =>
40+
type.isDartAsyncFuture ||
41+
type.isDartAsyncFutureOr ||
42+
type.isDynamic ||
43+
type.isDartCoreObject;
3944
}

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_passing_async_when_sync_expected/avoid_passing_async_when_sync_expected_rule_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void main() {
2626

2727
RuleTestHelper.verifyIssues(
2828
issues: issues,
29-
startLines: [49, 54, 57, 84],
29+
startLines: [49, 54, 57, 87],
3030
startColumns: [7, 7, 7, 9],
3131
locationTexts: [
3232
'synchronousWork: work1',

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

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class _MyHomePageState extends State<MyHomePage> {
5555
print('work 3');
5656
},
5757
synchronousWork4: work4, // LINT
58+
synchronousWork5: work1,
5859
);
5960
}
6061

@@ -63,11 +64,13 @@ class _MyHomePageState extends State<MyHomePage> {
6364
required VoidCallback synchronousWork2,
6465
required VoidCallback synchronousWork3,
6566
required VoidCallback synchronousWork4,
67+
required dynamic Function() synchronousWork5,
6668
}) {
6769
synchronousWork();
6870
synchronousWork2();
6971
synchronousWork3();
7072
synchronousWork4();
73+
synchronousWork5();
7174
}
7275

7376
void _incrementCounter() {

0 commit comments

Comments
 (0)