diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 3292789e4f1..17069dad16c 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -5395,12 +5395,10 @@ class ErrorVerifier extends RecursiveAstVisitor { return false; } - // For `C?.foo()` the type of `C` is not set, it is not an expression. + // For `foo?.bar`, `foo` must be an identifier with a value. if (node is Identifier) { var element = node.staticElement; - if (element is ClassElement || element is ExtensionElement) { - return false; - } + return element is PropertyAccessorElement || element is VariableElement; } return true; diff --git a/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart index 1d72a87c580..d36cb8fc789 100644 --- a/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart +++ b/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart @@ -97,6 +97,24 @@ f(int? x) { '''); } + /// Here we test that analysis does not crash while checking whether to + /// report [StaticWarningCode.INVALID_NULL_AWARE_OPERATOR]. But we also + /// report another error. + test_getter_prefix() async { + newFile('/test/lib/a.dart', content: r''' +int x = 0; +'''); + await assertErrorsInCode(''' +import 'a.dart' as p; + +f() { + p?.x; +} +''', [ + error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1), + ]); + } + test_index_legacy() async { newFile('/test/lib/a.dart', content: r''' // @dart = 2.5 @@ -287,4 +305,22 @@ f() { } '''); } + + /// Here we test that analysis does not crash while checking whether to + /// report [StaticWarningCode.INVALID_NULL_AWARE_OPERATOR]. But we also + /// report another error. + test_setter_prefix() async { + newFile('/test/lib/a.dart', content: r''' +int x = 0; +'''); + await assertErrorsInCode(''' +import 'a.dart' as p; + +f() { + p?.x = 0; +} +''', [ + error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1), + ]); + } }