Skip to content

Commit 59ceb55

Browse files
FMorschelCommit Queue
authored andcommitted
[linter] Fixes unnecessary_this false negative inside if case
Fixes: #60514 Change-Id: I934f22461b395177eca0046b0bf8797067ccbe46 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421961 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent 2f3b0bd commit 59ceb55

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,9 @@ abstract final class CaseClause implements AstNode {
22812281
GuardedPattern get guardedPattern;
22822282
}
22832283

2284-
final class CaseClauseImpl extends AstNodeImpl implements CaseClause {
2284+
final class CaseClauseImpl extends AstNodeImpl
2285+
with AstNodeWithNameScopeMixin
2286+
implements CaseClause {
22852287
@override
22862288
final Token caseKeyword;
22872289

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5236,6 +5236,11 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
52365236
if (caseClause != null) {
52375237
var guardedPattern = caseClause.guardedPattern;
52385238
_withNameScope(() {
5239+
caseClause.nameScope = nameScope;
5240+
var variables = guardedPattern.variables;
5241+
for (var variable in variables.values) {
5242+
_define(variable);
5243+
}
52395244
guardedPattern.accept(this);
52405245
node.ifTrue.accept(this);
52415246
});

pkg/linter/test/rules/unnecessary_this_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ class A {
166166
);
167167
}
168168

169+
test_shadowInIfCaseClause() async {
170+
await assertNoDiagnostics(r'''
171+
class A {
172+
int? value;
173+
174+
void m(A a) {
175+
if (a case A(:var value) when value != this.value) {}
176+
}
177+
}
178+
''');
179+
}
180+
169181
test_shadowInMethodBody() async {
170182
await assertNoDiagnostics(r'''
171183
class C {

0 commit comments

Comments
 (0)