Do not apply the fix for avoid_function_literals_in_foreach_calls when body is not sync

Change-Id: I0482c0d35a01afffd26073383b137697b829249c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274943
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2022-12-12 18:12:57 +00:00 committed by Commit Queue
parent c09f790d37
commit 2fbb93fc11
2 changed files with 69 additions and 0 deletions

View file

@ -52,6 +52,9 @@ class ConvertForEachToForLoop extends CorrectionProducer {
}
var target = utils.getNodeText(invocation.target!);
var body = argument.body;
if (body.isAsynchronous || body.isGenerator) {
return;
}
if (body is BlockFunctionBody) {
await builder.addDartFileEdit(file, (builder) {
builder.addReplacement(range.startStart(invocation, body), (builder) {

View file

@ -129,6 +129,39 @@ void f(List<String> list) {
''');
}
Future<void> test_blockBody_async() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) async {
e.length / 2;
});
}
''');
await assertNoFix();
}
Future<void> test_blockBody_asyncStar() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) async* {
e.length / 2;
});
}
''');
await assertNoFix();
}
Future<void> test_blockBody_syncStar() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) sync* {
e.length / 2;
});
}
''');
await assertNoFix();
}
Future<void> test_expressionBody() async {
await resolveTestCode('''
void f(List<String> list) {
@ -144,6 +177,39 @@ void f(List<String> list) {
''');
}
Future<void> test_expressionBody_async() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) async => e.substring(3, 7));
}
''');
await assertNoFix();
}
Future<void> test_expressionBody_asyncStar() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) async* => e.substring(3, 7));
}
''');
await assertNoFix(
errorFilter: (error) =>
error.errorCode.name ==
LintNames.avoid_function_literals_in_foreach_calls);
}
Future<void> test_expressionBody_syncStar() async {
await resolveTestCode('''
void f(List<String> list) {
list.forEach((e) sync* => e.substring(3, 7));
}
''');
await assertNoFix(
errorFilter: (error) =>
error.errorCode.name ==
LintNames.avoid_function_literals_in_foreach_calls);
}
Future<void> test_return() async {
await resolveTestCode('''
void f(List<String> list) {