Fix false positive fix remove ?

Closes https://github.com/dart-lang/sdk/pull/57068

GitOrigin-RevId: e875cd42fca07b1a1da3ece549b3faeb0c8c221c
Change-Id: I00f3445a240dc1d92ddba6e7e4f42ecf5bb907d5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394403
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart
index 8b4ea6a..075c051 100644
--- a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart
@@ -459,7 +459,10 @@
   @override
   TypeClassification classifyType(SharedTypeView<DartType> type) {
     DartType unwrapped = type.unwrapTypeView();
-    if (isSubtypeOfInternal(unwrapped, typeSystem.typeProvider.objectType)) {
+    if (type is InvalidType) {
+      return TypeClassification.potentiallyNullable;
+    } else if (isSubtypeOfInternal(
+        unwrapped, typeSystem.typeProvider.objectType)) {
       return TypeClassification.nonNullable;
     } else if (isSubtypeOfInternal(
         unwrapped, typeSystem.typeProvider.nullType)) {
diff --git a/pkg/analyzer/test/src/diagnostics/unnecessary_null_assert_pattern_test.dart b/pkg/analyzer/test/src/diagnostics/unnecessary_null_assert_pattern_test.dart
index b8c875f..916a0a9 100644
--- a/pkg/analyzer/test/src/diagnostics/unnecessary_null_assert_pattern_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/unnecessary_null_assert_pattern_test.dart
@@ -15,7 +15,7 @@
 
 @reflectiveTest
 class UnnecessaryNullAssertPatternTest extends PubPackageResolutionTest {
-  test_interfaceType_nonNullable() async {
+  Future<void> test_interfaceType_nonNullable() async {
     await assertErrorsInCode('''
 void f(int x) {
   if (x case var a!) {}
@@ -26,7 +26,7 @@
     ]);
   }
 
-  test_interfaceType_nullable() async {
+  Future<void> test_interfaceType_nullable() async {
     await assertErrorsInCode('''
 void f(int? x) {
   if (x case var a!) {}
@@ -36,7 +36,34 @@
     ]);
   }
 
-  test_typeParameter_nonNullable() async {
+  Future<void> test_invalidType_nonNullable() async {
+    await assertErrorsInCode('''
+UnknownType getValue() => UnknownType();
+void f() {
+  if (getValue() case final valueX!) {
+    print(valueX);
+  }
+}
+''', [
+      error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
+      error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 26, 11),
+    ]);
+  }
+
+  Future<void> test_invalidType_nullable() async {
+    await assertErrorsInCode('''
+UnknownType? getValue() => null;
+void f() {
+  if (getValue() case final valueX!) {
+    print(valueX);
+  }
+}
+''', [
+      error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
+    ]);
+  }
+
+  Future<void> test_typeParameter_nonNullable() async {
     await assertErrorsInCode('''
 class A<T extends num> {
   void f(T x) {
@@ -49,7 +76,7 @@
     ]);
   }
 
-  test_typeParameter_nullable() async {
+  Future<void> test_typeParameter_nullable() async {
     await assertErrorsInCode('''
 class A<T> {
   void f(T x) {
diff --git a/pkg/analyzer/test/src/diagnostics/unnecessary_null_check_pattern_test.dart b/pkg/analyzer/test/src/diagnostics/unnecessary_null_check_pattern_test.dart
index 1f6e748..005190a 100644
--- a/pkg/analyzer/test/src/diagnostics/unnecessary_null_check_pattern_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/unnecessary_null_check_pattern_test.dart
@@ -15,7 +15,7 @@
 
 @reflectiveTest
 class UnnecessaryNullCheckPatternTest extends PubPackageResolutionTest {
-  test_interfaceType_nonNullable() async {
+  Future<void> test_interfaceType_nonNullable() async {
     await assertErrorsInCode('''
 void f(int x) {
   if (x case var a?) {}
@@ -26,7 +26,7 @@
     ]);
   }
 
-  test_interfaceType_nullable() async {
+  Future<void> test_interfaceType_nullable() async {
     await assertErrorsInCode('''
 void f(int? x) {
   if (x case var a?) {}
@@ -36,7 +36,34 @@
     ]);
   }
 
-  test_typeParameter_nonNullable() async {
+  Future<void> test_invalidType_nonNullable() async {
+    await assertErrorsInCode('''
+UnknownType getValue() => UnknownType();
+void f() {
+  if (getValue() case final valueX?) {
+    print(valueX);
+  }
+}
+''', [
+      error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
+      error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 26, 11),
+    ]);
+  }
+
+  Future<void> test_invalidType_nullable() async {
+    await assertErrorsInCode('''
+UnknownType? getValue() => null;
+void f() {
+  if (getValue() case final valueX?) {
+    print(valueX);
+  }
+}
+''', [
+      error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
+    ]);
+  }
+
+  Future<void> test_typeParameter_nonNullable() async {
     await assertErrorsInCode('''
 class A<T extends num> {
   void f(T x) {
@@ -49,7 +76,7 @@
     ]);
   }
 
-  test_typeParameter_nullable() async {
+  Future<void> test_typeParameter_nullable() async {
     await assertErrorsInCode('''
 class A<T> {
   void f(T x) {