Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

[use_build_context_synchronously] Fix for unprotected 'last' invocation. #4279

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/src/rules/use_build_context_synchronously.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';

import '../analyzer.dart';
import '../util/flutter_utils.dart';
Expand Down Expand Up @@ -346,7 +347,9 @@ extension on Statement {
bool get terminatesControl {
var self = this;
if (self is Block) {
return self.statements.last.terminatesControl;
// TODO(scheglov) Stop using package:collection when SDK 3.0.0
var last = self.statements.lastOrNull;
return last != null && last.terminatesControl;
}
// TODO(srawlins): Make ExitDetector 100% functional for our needs. The
// basic (only?) difference is that it doesn't consider a `break` statement
Expand Down
13 changes: 12 additions & 1 deletion test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,30 @@ typedef DiagnosticMatcher = bool Function(AnalysisError error);
class AnalysisOptionsFileConfig {
final List<String> experiments;
final List<String> lints;
final bool propagateLinterExceptions;

AnalysisOptionsFileConfig({
this.experiments = const [],
this.lints = const [],
this.propagateLinterExceptions = false,
});

String toContent() {
var buffer = StringBuffer();

if (experiments.isNotEmpty) {
if (experiments.isNotEmpty || propagateLinterExceptions) {
buffer.writeln('analyzer:');
buffer.writeln(' enable-experiment:');
for (var experiment in experiments) {
buffer.writeln(' - $experiment');
}

if (propagateLinterExceptions) {
buffer.writeln(' strong-mode:');
buffer.writeln(
' propagate-linter-exceptions: $propagateLinterExceptions',
);
}
}

buffer.writeln('linter:');
Expand Down Expand Up @@ -378,6 +387,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
AnalysisOptionsFileConfig(
experiments: experiments,
lints: _lintRules,
propagateLinterExceptions: true,
),
);
writeTestPackageConfig(
Expand Down Expand Up @@ -456,6 +466,7 @@ export 'src/widgets/framework.dart';
.writeAsStringSync(r'''
abstract class BuildContext {
Widget get widget;
bool get mounted;
}

class Navigator {
Expand Down
15 changes: 15 additions & 0 deletions test/rules/use_build_context_synchronously_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,21 @@ Future<void> bar({required BuildContext context}) async {}
]);
}

test_noAwaitBefore_ifEmptyThen_methodInvocation() async {
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void f(BuildContext context) async {
if (true) {}
context.foo();
}

extension on BuildContext {
void foo() {}
}
''');
}

/// https://github.com/dart-lang/linter/issues/3700
test_propertyAccess_getter() async {
await assertDiagnostics(r'''
Expand Down