From 901b2eb9c76bb9db218f5951b55d61d47f6c7b46 Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Wed, 13 Nov 2024 23:33:30 +0000 Subject: [PATCH] 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 Reviewed-by: Phil Quitslund --- .../dart/resolver/flow_analysis_visitor.dart | 5 ++- .../unnecessary_null_assert_pattern_test.dart | 35 ++++++++++++++++--- .../unnecessary_null_check_pattern_test.dart | 35 ++++++++++++++++--- 3 files changed, 66 insertions(+), 9 deletions(-) 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 8b4ea6abb206..075c051b44a8 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 @@ class TypeSystemOperations @override TypeClassification classifyType(SharedTypeView 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 b8c875f16efe..916a0a9887ef 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 @@ main() { @reflectiveTest class UnnecessaryNullAssertPatternTest extends PubPackageResolutionTest { - test_interfaceType_nonNullable() async { + Future test_interfaceType_nonNullable() async { await assertErrorsInCode(''' void f(int x) { if (x case var a!) {} @@ -26,7 +26,7 @@ void f(int x) { ]); } - test_interfaceType_nullable() async { + Future test_interfaceType_nullable() async { await assertErrorsInCode(''' void f(int? x) { if (x case var a!) {} @@ -36,7 +36,34 @@ void f(int? x) { ]); } - test_typeParameter_nonNullable() async { + Future 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 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 test_typeParameter_nonNullable() async { await assertErrorsInCode(''' class A { void f(T x) { @@ -49,7 +76,7 @@ class A { ]); } - test_typeParameter_nullable() async { + Future test_typeParameter_nullable() async { await assertErrorsInCode(''' class A { 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 1f6e748d74a3..005190a41bce 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 @@ main() { @reflectiveTest class UnnecessaryNullCheckPatternTest extends PubPackageResolutionTest { - test_interfaceType_nonNullable() async { + Future test_interfaceType_nonNullable() async { await assertErrorsInCode(''' void f(int x) { if (x case var a?) {} @@ -26,7 +26,7 @@ void f(int x) { ]); } - test_interfaceType_nullable() async { + Future test_interfaceType_nullable() async { await assertErrorsInCode(''' void f(int? x) { if (x case var a?) {} @@ -36,7 +36,34 @@ void f(int? x) { ]); } - test_typeParameter_nonNullable() async { + Future 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 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 test_typeParameter_nonNullable() async { await assertErrorsInCode(''' class A { void f(T x) { @@ -49,7 +76,7 @@ class A { ]); } - test_typeParameter_nullable() async { + Future test_typeParameter_nullable() async { await assertErrorsInCode(''' class A { void f(T x) {