Skip to content

Commit

Permalink
linter: fix use_build_context_synchronously bug with OR
Browse files Browse the repository at this point in the history
Fixes dart-lang/linter#5074

Change-Id: Ic4a012cbb753f1995e8e0b4fe8e444af996f46aa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381901
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Auto-Submit: Samuel Rawlins <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Aug 23, 2024
1 parent 13285dc commit 14cbdb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/linter/lib/src/rules/use_build_context_synchronously.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,14 @@ class AsyncStateVisitor extends SimpleAstVisitor<AsyncState> {
return switch ((leftGuardState, rightGuardState)) {
// Anything on the left followed by async on the right is async.
(_, AsyncState.asynchronous) => AsyncState.asynchronous,
// Async on the left followed by anything on the right is async.
// Anything on the left followed by not-mounted on the right is a
// not-mounted check.
(_, AsyncState.notMountedCheck) => AsyncState.notMountedCheck,
// Async on the left followed by anything else on the right is async.
(AsyncState.asynchronous, _) => AsyncState.asynchronous,
// A mounted guard only applies if both sides are guarded.
(AsyncState.mountedCheck, AsyncState.mountedCheck) =>
AsyncState.mountedCheck,
(_, AsyncState.notMountedCheck) => AsyncState.notMountedCheck,
(AsyncState.notMountedCheck, _) => AsyncState.notMountedCheck,
// Otherwise it's just uninteresting.
(_, _) => null,
Expand Down
16 changes: 16 additions & 0 deletions pkg/linter/test/rules/use_build_context_synchronously_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,22 @@ void foo(BuildContext context) async {
AsyncState.asynchronous);
}

test_ifStatement_referenceAfter_asyncOrNotMountedInCondition() async {
await resolveCode(r'''
import 'package:flutter/widgets.dart';
void foo(BuildContext context) async {
if (await Future.value(true) || !context.mounted) return;
//} else {
context /* ref */;
//}
}
''');
var body = findNode.ifStatement('if ').parent!;
var reference = findNode.statement('context /* ref */');
expect(body.asyncStateFor(reference, contextElement),
AsyncState.notMountedCheck);
}

test_ifStatement_referenceAfter_awaitThenExitInElse() async {
await resolveCode(r'''
import 'package:flutter/widgets.dart';
Expand Down

0 comments on commit 14cbdb3

Please sign in to comment.