Skip to content

Commit 901b2eb

Browse files
tenhobiCommit Queue
authored andcommitted
Fix false positive fix remove ?
Closes #57068 GitOrigin-RevId: e875cd4 Change-Id: I00f3445a240dc1d92ddba6e7e4f42ecf5bb907d5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394403 Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 81c7964 commit 901b2eb

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ class TypeSystemOperations
459459
@override
460460
TypeClassification classifyType(SharedTypeView<DartType> type) {
461461
DartType unwrapped = type.unwrapTypeView();
462-
if (isSubtypeOfInternal(unwrapped, typeSystem.typeProvider.objectType)) {
462+
if (type is InvalidType) {
463+
return TypeClassification.potentiallyNullable;
464+
} else if (isSubtypeOfInternal(
465+
unwrapped, typeSystem.typeProvider.objectType)) {
463466
return TypeClassification.nonNullable;
464467
} else if (isSubtypeOfInternal(
465468
unwrapped, typeSystem.typeProvider.nullType)) {

pkg/analyzer/test/src/diagnostics/unnecessary_null_assert_pattern_test.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ main() {
1515

1616
@reflectiveTest
1717
class UnnecessaryNullAssertPatternTest extends PubPackageResolutionTest {
18-
test_interfaceType_nonNullable() async {
18+
Future<void> test_interfaceType_nonNullable() async {
1919
await assertErrorsInCode('''
2020
void f(int x) {
2121
if (x case var a!) {}
@@ -26,7 +26,7 @@ void f(int x) {
2626
]);
2727
}
2828

29-
test_interfaceType_nullable() async {
29+
Future<void> test_interfaceType_nullable() async {
3030
await assertErrorsInCode('''
3131
void f(int? x) {
3232
if (x case var a!) {}
@@ -36,7 +36,34 @@ void f(int? x) {
3636
]);
3737
}
3838

39-
test_typeParameter_nonNullable() async {
39+
Future<void> test_invalidType_nonNullable() async {
40+
await assertErrorsInCode('''
41+
UnknownType getValue() => UnknownType();
42+
void f() {
43+
if (getValue() case final valueX!) {
44+
print(valueX);
45+
}
46+
}
47+
''', [
48+
error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
49+
error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 26, 11),
50+
]);
51+
}
52+
53+
Future<void> test_invalidType_nullable() async {
54+
await assertErrorsInCode('''
55+
UnknownType? getValue() => null;
56+
void f() {
57+
if (getValue() case final valueX!) {
58+
print(valueX);
59+
}
60+
}
61+
''', [
62+
error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
63+
]);
64+
}
65+
66+
Future<void> test_typeParameter_nonNullable() async {
4067
await assertErrorsInCode('''
4168
class A<T extends num> {
4269
void f(T x) {
@@ -49,7 +76,7 @@ class A<T extends num> {
4976
]);
5077
}
5178

52-
test_typeParameter_nullable() async {
79+
Future<void> test_typeParameter_nullable() async {
5380
await assertErrorsInCode('''
5481
class A<T> {
5582
void f(T x) {

pkg/analyzer/test/src/diagnostics/unnecessary_null_check_pattern_test.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ main() {
1515

1616
@reflectiveTest
1717
class UnnecessaryNullCheckPatternTest extends PubPackageResolutionTest {
18-
test_interfaceType_nonNullable() async {
18+
Future<void> test_interfaceType_nonNullable() async {
1919
await assertErrorsInCode('''
2020
void f(int x) {
2121
if (x case var a?) {}
@@ -26,7 +26,7 @@ void f(int x) {
2626
]);
2727
}
2828

29-
test_interfaceType_nullable() async {
29+
Future<void> test_interfaceType_nullable() async {
3030
await assertErrorsInCode('''
3131
void f(int? x) {
3232
if (x case var a?) {}
@@ -36,7 +36,34 @@ void f(int? x) {
3636
]);
3737
}
3838

39-
test_typeParameter_nonNullable() async {
39+
Future<void> test_invalidType_nonNullable() async {
40+
await assertErrorsInCode('''
41+
UnknownType getValue() => UnknownType();
42+
void f() {
43+
if (getValue() case final valueX?) {
44+
print(valueX);
45+
}
46+
}
47+
''', [
48+
error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
49+
error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 26, 11),
50+
]);
51+
}
52+
53+
Future<void> test_invalidType_nullable() async {
54+
await assertErrorsInCode('''
55+
UnknownType? getValue() => null;
56+
void f() {
57+
if (getValue() case final valueX?) {
58+
print(valueX);
59+
}
60+
}
61+
''', [
62+
error(CompileTimeErrorCode.UNDEFINED_CLASS, 0, 11),
63+
]);
64+
}
65+
66+
Future<void> test_typeParameter_nonNullable() async {
4067
await assertErrorsInCode('''
4168
class A<T extends num> {
4269
void f(T x) {
@@ -49,7 +76,7 @@ class A<T extends num> {
4976
]);
5077
}
5178

52-
test_typeParameter_nullable() async {
79+
Future<void> test_typeParameter_nullable() async {
5380
await assertErrorsInCode('''
5481
class A<T> {
5582
void f(T x) {

0 commit comments

Comments
 (0)