Wire up remove question mark and remove non null assert fixes for patterns
Bug:49960
Change-Id: I6b0a94454e0f51c5fd15d4abef16d075dce54a90
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279334
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
index bb1ee6c..91b3083 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_non_null_assertion.dart
@@ -35,5 +35,13 @@
builder.addDeletion(range.startStart(bangToken, bangToken.next!));
});
}
+
+ if (expression is NullAssertPattern) {
+ var bangToken = expression.operator;
+
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.startStart(bangToken, bangToken.next!));
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
index e8ee546..c51a340 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart
@@ -42,5 +42,11 @@
builder.addDeletion(range.token(questionMark));
});
}
+ if (targetNode is NullCheckPattern) {
+ var questionMark = targetNode.operator;
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(range.token(questionMark));
+ });
+ }
}
}
diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
index 8fba8d1..d59e50c 100644
--- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
+++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
@@ -2716,9 +2716,9 @@
StaticWarningCode.UNNECESSARY_NON_NULL_ASSERTION:
status: hasFix
StaticWarningCode.UNNECESSARY_NULL_ASSERT_PATTERN:
- status: needsFix
+ status: hasFix
StaticWarningCode.UNNECESSARY_NULL_CHECK_PATTERN:
- status: needsFix
+ status: hasFix
WarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_TO_ERROR_HANDLER:
status: noFix
notes: |-
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index c109bae..cfd72ad 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -1539,6 +1539,12 @@
StaticWarningCode.UNNECESSARY_NON_NULL_ASSERTION: [
RemoveNonNullAssertion.new,
],
+ StaticWarningCode.UNNECESSARY_NULL_CHECK_PATTERN: [
+ RemoveQuestionMark.new,
+ ],
+ StaticWarningCode.UNNECESSARY_NULL_ASSERT_PATTERN: [
+ RemoveNonNullAssertion.new,
+ ],
WarningCode.BODY_MIGHT_COMPLETE_NORMALLY_NULLABLE: [
AddReturnNull.new,
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_non_null_assertion_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_non_null_assertion_test.dart
index 42ce051..8b1ec4f 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_non_null_assertion_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_non_null_assertion_test.dart
@@ -49,14 +49,13 @@
''');
}
- @FailingTest(
- reason: "TODO(keertip): Add support for patterns to FixProcessor")
Future<void> test_nonNullableCasePattern() async {
await resolveTestCode('''
void f() {
List<String> row = ['h', 'e', 'l'];
switch (row) {
- case ['user', var name!]: break;
+ case ['user', var name!]:
+ print(name);
}
}
''');
@@ -64,25 +63,28 @@
void f() {
List<String> row = ['h', 'e', 'l'];
switch (row) {
- case ['user', var name]: break;
+ case ['user', var name]:
+ print(name);
}
}
''');
}
- @FailingTest(
- reason: "TODO(keertip): Add support for patterns to FixProcessor")
Future<void> test_nonNullablePattern() async {
await resolveTestCode('''
void f() {
(int, int?) p = (1, 2);
var (x!, y!) = p;
+ print(x);
+ print(y);
}
''');
await assertHasFix('''
void f() {
(int, int?) p = (1, 2);
var (x, y!) = p;
+ print(x);
+ print(y);
}
''');
}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_question_mark_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_question_mark_test.dart
index f40ece4..f5cf00f 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_question_mark_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_question_mark_test.dart
@@ -47,15 +47,14 @@
@override
FixKind get kind => DartFixKind.REMOVE_QUESTION_MARK;
- @FailingTest(
- reason: "TODO(keertip): Add support for patterns to FixProcessor")
Future<void> test_casePattern() async {
await resolveTestCode('''
void f() {
String? maybeString = 'hello';
switch (maybeString) {
case
- var s?: break;
+ var s?:
+ print(s);
}
}
''');
@@ -64,7 +63,8 @@
String? maybeString = 'hello';
switch (maybeString) {
case
- var s: break;
+ var s:
+ print(s);
}
}
''');