Skip to content

Commit f39897a

Browse files
authored
Don't report avoid_function_literals_in_foreach_calls with null-aware call (dart-archive/linter#4039)
* Don't report `avoid_function_literals_in_foreach_calls` with null-aware call The current behavior is fully correct in null-safe mode, but in legacy mode the check for nullable type is inverted. Not adding a test because there is no legacy-mode test file. Closes dart-lang/linter#3354 * Add test * Move test to noSoundNullSafety
1 parent 2e85332 commit f39897a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/src/rules/avoid_function_literals_in_foreach_calls.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool _hasMethodChaining(MethodInvocation node) {
5454

5555
bool _isNonNullableIterable(DartType? type) =>
5656
type != null &&
57-
type.nullabilitySuffix != NullabilitySuffix.question &&
57+
type.nullabilitySuffix == NullabilitySuffix.none &&
5858
type.implementsInterface('Iterable', 'dart.core');
5959

6060
class AvoidFunctionLiteralInForeachMethod extends LintRule {

test/rules/avoid_function_literals_in_foreach_calls_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/utilities/legacy.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../rule_test_support.dart';
@@ -98,3 +99,28 @@ void f(List<String> people) {
9899
''');
99100
}
100101
}
102+
103+
@reflectiveTest
104+
class AvoidFunctionLiteralsInForeachCallsPreNNBDTest extends LintRuleTest {
105+
@override
106+
String get lintRule => 'avoid_function_literals_in_foreach_calls';
107+
108+
@override
109+
setUp() {
110+
super.setUp();
111+
noSoundNullSafety = false;
112+
}
113+
114+
tearDown() {
115+
noSoundNullSafety = true;
116+
}
117+
118+
test_functionExpression_nullableTarget() async {
119+
await assertNoDiagnostics(r'''
120+
// @dart=2.9
121+
void f(List<String> people) {
122+
people?.forEach((person) => print('$person'));
123+
}
124+
''');
125+
}
126+
}

0 commit comments

Comments
 (0)