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 <srawlins@google.com> Reviewed-by: Phil Quitslund <pquitslund@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
committed by
Commit Queue
parent
b72f968334
commit
29f78ef98c
@@ -1708,6 +1708,47 @@ Future<void> 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<void> 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<void> c() async {}
|
||||
''', [
|
||||
lint(125, 21),
|
||||
]);
|
||||
}
|
||||
|
||||
test_await_afterReferenceToContext() async {
|
||||
// Use of BuildContext, then await, in statement block is OK.
|
||||
await assertNoDiagnostics(r'''
|
||||
|
||||
@@ -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<void>.delayed(Duration());
|
||||
switch (1) {
|
||||
case 1:
|
||||
await Navigator.of(context).pushNamed('routeName'); // LINT
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void awaitInSwitchCase_mountedCheckBeforeSwitch(BuildContext context) async {
|
||||
await Future<void>.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<void>.delayed(Duration());
|
||||
f2(contextOrNull); // OK
|
||||
}
|
||||
|
||||
void nullableContext2(ContextHolder holder) async {
|
||||
f2(holder.contextOrNull);
|
||||
await Future<void>.delayed(Duration());
|
||||
f2(holder.contextOrNull); // OK
|
||||
}
|
||||
|
||||
void nullableContext3() async {
|
||||
f2(contextOrNull);
|
||||
await Future<void>.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<void>.delayed(Duration());
|
||||
|
||||
var renderObject = context.findRenderObject(); // LINT
|
||||
}
|
||||
|
||||
class _MyState extends State<MyWidget> {
|
||||
// Same as above, but using a conditional path.
|
||||
void methodWithBuildContextParameter2(BuildContext context) async {
|
||||
if (defaultTargetPlatform == TargetPlatform.iOS) {
|
||||
await Future<void>.delayed(Duration());
|
||||
}
|
||||
Navigator.of(context).pushNamed('routeName'); // LINT
|
||||
}
|
||||
|
||||
void methodWithBuildContextParameter2g(BuildContext context) async {
|
||||
await Future<void>.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<void>.delayed(Duration());
|
||||
// todo (pq): consider other conditionals (for, while, do, ...)
|
||||
if (true) {
|
||||
Navigator.of(context).pushNamed('routeName'); // LINT
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user