don't replace is{Not}Empty for nullable targets

Fixes: https://github.com/dart-lang/sdk/issues/55250

Change-Id: Iae692c0f6a36e7fc89028acd1934580d1648c937
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/359262
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2024-03-22 21:24:41 +00:00 committed by Commit Queue
parent ef3eb6b301
commit 4145eeba06
3 changed files with 27 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@ -36,6 +37,12 @@ class ReplaceWithIsEmpty extends ResolvedCorrectionProducer {
return;
}
// Skip nullable targets.
if (replacement.lengthTarget.staticType?.nullabilitySuffix ==
NullabilitySuffix.question) {
return;
}
var target = utils.getNodeText(replacement.lengthTarget);
var getter = replacement.getter;
await builder.addDartFileEdit(file, (builder) {

View file

@ -122,4 +122,14 @@ f(List c) {
}
''');
}
/// https://github.com/dart-lang/sdk/issues/55250
Future<void> test_nullableList() async {
await resolveTestCode('''
bool f(List<String>? l) {
return l?.length == 0;
}
''');
await assertNoFix();
}
}

View file

@ -74,4 +74,14 @@ f(List c) {
}
''');
}
/// https://github.com/dart-lang/sdk/issues/55250
Future<void> test_nullableList() async {
await resolveTestCode('''
f(List? c) {
if (c?.length != 0) {}
}
''');
await assertNoFix();
}
}