From 645587bc70473eac6b455fcd86bbdf123bdf32ca Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 23 May 2023 19:19:38 +0000 Subject: [PATCH] Issue 52486. Fix LinterContext.resolveNameInScope() in SwitchExpression cases. Bug: https://github.com/dart-lang/sdk/issues/52486 Change-Id: I7d00ed03f8da37e7adb2959a62acefbd1904770a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/305060 Commit-Queue: Konstantin Shcheglov Reviewed-by: Brian Wilkerson --- pkg/analyzer/CHANGELOG.md | 4 ++ pkg/analyzer/lib/src/generated/resolver.dart | 1 + pkg/analyzer/pubspec.yaml | 2 +- .../linter/resolve_name_in_scope_test.dart | 53 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/pkg/analyzer/CHANGELOG.md b/pkg/analyzer/CHANGELOG.md index b97d45827d2..6c38466060f 100644 --- a/pkg/analyzer/CHANGELOG.md +++ b/pkg/analyzer/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.14.0-dev +* Updated SDK constraint to `>=3.0.0 <4.0.0`. +* Fixed #52486. + ## 5.13.0 * `InvalidType` is now used when types or property cannot be resolved. Previously `DynamicType` was used. diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 85940fa0390..bfd842f7cf5 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -4961,6 +4961,7 @@ class ScopeResolverVisitor extends UnifyingAstVisitor { for (var case_ in node.cases) { _withNameScope(() { + _setNodeNameScope(case_, nameScope); var guardedPattern = case_.guardedPattern; var variables = guardedPattern.variables; for (var variable in variables.values) { diff --git a/pkg/analyzer/pubspec.yaml b/pkg/analyzer/pubspec.yaml index e4bb774c374..76cad9776b1 100644 --- a/pkg/analyzer/pubspec.yaml +++ b/pkg/analyzer/pubspec.yaml @@ -1,5 +1,5 @@ name: analyzer -version: 5.13.0 +version: 5.14.0-dev description: >- This package provides a library that performs static analysis of Dart code. repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer diff --git a/pkg/analyzer/test/src/lint/linter/resolve_name_in_scope_test.dart b/pkg/analyzer/test/src/lint/linter/resolve_name_in_scope_test.dart index ecab45450ba..0b270003fee 100644 --- a/pkg/analyzer/test/src/lint/linter/resolve_name_in_scope_test.dart +++ b/pkg/analyzer/test/src/lint/linter/resolve_name_in_scope_test.dart @@ -414,6 +414,59 @@ class A { _checkMethodRequestedLocalVariable(); } + test_class_method_requested_patternVariable_ifCase() async { + await resolve(''' +class A { + void foo() {} + + void bar(Object? x) { + if (x case A(:var foo)) { + this.foo(); + } + } +} +''', [ + error(WarningCode.UNUSED_LOCAL_VARIABLE, 73, 3), + ]); + _checkMethodRequestedLocalVariable(); + } + + test_class_method_requested_patternVariable_switchExpression() async { + await resolve(''' +class A { + void foo() {} + + void bar(Object? x) { + (switch (x) { + A(:var foo) => this.foo(), + _ => 0, + }); + } +} +''', [ + error(WarningCode.UNUSED_LOCAL_VARIABLE, 82, 3), + ]); + _checkMethodRequestedLocalVariable(); + } + + test_class_method_requested_patternVariable_switchStatement() async { + await resolve(''' +class A { + void foo() {} + + void bar(Object? x) { + switch (x) { + case A(:var foo): + this.foo(); + } + } +} +''', [ + error(WarningCode.UNUSED_LOCAL_VARIABLE, 86, 3), + ]); + _checkMethodRequestedLocalVariable(); + } + test_class_method_requested_thisClass() async { await resolve(''' class A {