From 14cbdb34d6655d2ebdc2e93f64767e923c3a6281 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 23 Aug 2024 16:45:19 +0000 Subject: [PATCH] linter: fix use_build_context_synchronously bug with OR Fixes https://github.com/dart-lang/linter/issues/5074 Change-Id: Ic4a012cbb753f1995e8e0b4fe8e444af996f46aa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381901 Commit-Queue: Phil Quitslund Reviewed-by: Phil Quitslund Auto-Submit: Samuel Rawlins --- .../rules/use_build_context_synchronously.dart | 6 ++++-- .../use_build_context_synchronously_test.dart | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/linter/lib/src/rules/use_build_context_synchronously.dart b/pkg/linter/lib/src/rules/use_build_context_synchronously.dart index 663b6b5c25fd..1126929dcee1 100644 --- a/pkg/linter/lib/src/rules/use_build_context_synchronously.dart +++ b/pkg/linter/lib/src/rules/use_build_context_synchronously.dart @@ -270,12 +270,14 @@ class AsyncStateVisitor extends SimpleAstVisitor { 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, diff --git a/pkg/linter/test/rules/use_build_context_synchronously_test.dart b/pkg/linter/test/rules/use_build_context_synchronously_test.dart index accd7a1ca96d..8c3e1c72d633 100644 --- a/pkg/linter/test/rules/use_build_context_synchronously_test.dart +++ b/pkg/linter/test/rules/use_build_context_synchronously_test.dart @@ -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';