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

Commit 27cf88f

Browse files
committed
Don't check if iterable is nullable, what matters is null-aware calls
Fixes #4328. Reverts #4039 that caused false negatives in non-null-safe mode. And reverts #2752 along with it because it was not the exact right fix in the first place. Null-aware calls are since handled by #4305 and all the existing tests still pass.
1 parent 997671c commit 27cf88f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/src/rules/avoid_function_literals_in_foreach_calls.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/nullability_suffix.dart';
87
import 'package:analyzer/dart/element/type.dart';
98

109
import '../analyzer.dart';
@@ -56,10 +55,8 @@ bool _isInsideCascade(AstNode node) =>
5655
node.thisOrAncestorMatching((n) => n is Statement || n is CascadeExpression)
5756
is CascadeExpression;
5857

59-
bool _isNonNullableIterable(DartType? type) =>
60-
type != null &&
61-
type.nullabilitySuffix == NullabilitySuffix.none &&
62-
type.implementsInterface('Iterable', 'dart.core');
58+
bool _isIterable(DartType? type) =>
59+
type != null && type.implementsInterface('Iterable', 'dart.core');
6360

6461
class AvoidFunctionLiteralInForeachMethod extends LintRule {
6562
static const LintCode code = LintCode(
@@ -97,7 +94,7 @@ class _Visitor extends SimpleAstVisitor<void> {
9794
node.methodName.token.value() == 'forEach' &&
9895
node.argumentList.arguments.isNotEmpty &&
9996
node.argumentList.arguments.first is FunctionExpression &&
100-
_isNonNullableIterable(target.staticType) &&
97+
_isIterable(target.staticType) &&
10198
!node.containsNullAwareInvocationInChain() &&
10299
!_hasMethodChaining(node) &&
103100
!_isInsideCascade(node)) {

test/rules/avoid_function_literals_in_foreach_calls_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../rule_test_support.dart';
1010
main() {
1111
defineReflectiveSuite(() {
1212
defineReflectiveTests(AvoidFunctionLiteralsInForeachCalls);
13+
defineReflectiveTests(AvoidFunctionLiteralsInForeachCallsPreNNBDTest);
1314
});
1415
}
1516

@@ -159,7 +160,16 @@ class AvoidFunctionLiteralsInForeachCallsPreNNBDTest extends LintRuleTest {
159160
noSoundNullSafety = true;
160161
}
161162

162-
test_functionExpression_nullableTarget() async {
163+
test_functionExpression_basic() async {
164+
await assertDiagnostics(r'''
165+
// @dart=2.9
166+
void f(List<String> people) {
167+
people.forEach((person) => print('$person'));
168+
}
169+
''', [lint(52, 7)]);
170+
}
171+
172+
test_functionExpression_nullAware() async {
163173
await assertNoDiagnostics(r'''
164174
// @dart=2.9
165175
void f(List<String> people) {

0 commit comments

Comments
 (0)