From 29f78ef98ce9e6d2b8a3a14b85a174fb844f42b2 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 19 Sep 2023 16:34:59 +0000 Subject: [PATCH] linter: Move final use_build_context_synchronously tests Most of the tests removed here already are covered in the new tests. And also two test cases are added. Change-Id: I3d6e15d2b9fde14a2b2249a3422ca1508a0d7572 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326726 Auto-Submit: Samuel Rawlins Reviewed-by: Phil Quitslund Commit-Queue: Phil Quitslund --- .../use_build_context_synchronously_test.dart | 41 +++++++ .../use_build_context_synchronously.dart | 101 ------------------ 2 files changed, 41 insertions(+), 101 deletions(-) delete mode 100644 pkg/linter/test_data/rules/use_build_context_synchronously.dart 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 fc3abb1fc86..fa4d9a11398 100644 --- a/pkg/linter/test/rules/use_build_context_synchronously_test.dart +++ b/pkg/linter/test/rules/use_build_context_synchronously_test.dart @@ -1708,6 +1708,47 @@ Future c() async {} ]); } + test_async_thenMountedCheck_thenSwitchWithReferenceToContext() async { + // Assignment statement-expression with mounted check, then use of + // BuildContext in if-then statement, is REPORTED. + await assertNoDiagnostics(r''' +import 'package:flutter/widgets.dart'; + +void foo(BuildContext context) async { + await c(); + if (!context.mounted) return; + switch (1) { + case 1: + Navigator.of(context); + break; + } +} + +Future c() async {} +'''); + } + + test_async_thenSwitchWithReferenceToContext() async { + // Assignment statement-expression with mounted check, then use of + // BuildContext in if-then statement, is REPORTED. + await assertDiagnostics(r''' +import 'package:flutter/widgets.dart'; + +void foo(BuildContext context) async { + await c(); + switch (1) { + case 1: + Navigator.of(context); + break; + } +} + +Future c() async {} +''', [ + lint(125, 21), + ]); + } + test_await_afterReferenceToContext() async { // Use of BuildContext, then await, in statement block is OK. await assertNoDiagnostics(r''' diff --git a/pkg/linter/test_data/rules/use_build_context_synchronously.dart b/pkg/linter/test_data/rules/use_build_context_synchronously.dart deleted file mode 100644 index 6469af3bfae..00000000000 --- a/pkg/linter/test_data/rules/use_build_context_synchronously.dart +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -// test w/ `dart test -N use_build_context_synchronously` - -import 'package:flutter/foundation.dart'; -import 'package:flutter/widgets.dart'; - -void awaitInSwitchCase(BuildContext context) async { - await Future.delayed(Duration()); - switch (1) { - case 1: - await Navigator.of(context).pushNamed('routeName'); // LINT - break; - } -} - -void awaitInSwitchCase_mountedCheckBeforeSwitch(BuildContext context) async { - await Future.delayed(Duration()); - if (!mounted) return; - switch (1) { - case 1: - await Navigator.of(context).pushNamed('routeName'); // OK - break; - } -} - -bool get mounted => true; - -BuildContext? get contextOrNull => null; - -class ContextHolder { - BuildContext? get contextOrNull => null; -} - -void f2(BuildContext? contextOrNull) {} - -void nullableContext() async { - f2(contextOrNull); - await Future.delayed(Duration()); - f2(contextOrNull); // OK -} - -void nullableContext2(ContextHolder holder) async { - f2(holder.contextOrNull); - await Future.delayed(Duration()); - f2(holder.contextOrNull); // OK -} - -void nullableContext3() async { - f2(contextOrNull); - await Future.delayed(Duration()); - var renderObject = contextOrNull?.findRenderObject(); // OK -} - -void f(BuildContext context) {} - -class MyWidget extends StatefulWidget { - @override - State createState() => _MyState(); -} - -void directAccess(BuildContext context) async { - await Future.delayed(Duration()); - - var renderObject = context.findRenderObject(); // LINT -} - -class _MyState extends State { - // Same as above, but using a conditional path. - void methodWithBuildContextParameter2(BuildContext context) async { - if (defaultTargetPlatform == TargetPlatform.iOS) { - await Future.delayed(Duration()); - } - Navigator.of(context).pushNamed('routeName'); // LINT - } - - void methodWithBuildContextParameter2g(BuildContext context) async { - await Future.delayed(Duration()); - switch (1) { - case 1: - if (!mounted) return; - await Navigator.of(context).pushNamed('routeName'); // OK - break; - } - } - - @override - Widget build(BuildContext context) => Placeholder(); -} - -void topLevel2(BuildContext context) async { - Navigator.of(context).pushNamed('routeName'); // OK - - await Future.delayed(Duration()); - // todo (pq): consider other conditionals (for, while, do, ...) - if (true) { - Navigator.of(context).pushNamed('routeName'); // LINT - } -}