linter: catch fields referenced in a pattern field

Fixes https://github.com/dart-lang/sdk/issues/53674

Change-Id: I87effd3cc4b3d3227cd26c3fdeb68b7f7e2d0ca9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330880
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Auto-Submit: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins
2023-10-17 19:54:48 +00:00
committed by Commit Queue
parent becd3e396c
commit 20dae1a3f6
2 changed files with 25 additions and 0 deletions
@@ -315,6 +315,15 @@ class _ReferenceVisitor extends RecursiveAstVisitor {
}
}
@override
void visitPatternField(PatternField node) {
var e = node.element;
if (e != null) {
_addDeclaration(e);
}
super.visitPatternField(node);
}
@override
void visitPostfixExpression(PostfixExpression node) {
_visitCompoundAssignmentExpression(node);
@@ -20,6 +20,22 @@ class UnreachableFromMainTest extends LintRuleTest {
@override
String get lintRule => 'unreachable_from_main';
test_class_instanceField_reachable_matchedInPattern() async {
await assertNoDiagnostics(r'''
void main() {
var x = switch (C(1)) {
C(:final f) => f.round(),
};
print(x);
}
class C {
final int f;
C(this.f);
}
''');
}
test_class_instanceField_reachable_overrides_local() async {
await assertDiagnostics(r'''
void main() {