Skip to content

Commit e36813b

Browse files
authored
fix non_constant_identifier_names pattern field over-reporting (#4300)
* fix `non_constant_identifier_names` pattern field over-reporting * ++ * sort
1 parent 7050946 commit e36813b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/src/rules/non_constant_identifier_names.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011
import '../util/ascii_utils.dart';
1112
import '../utils.dart';
1213

@@ -100,6 +101,7 @@ class _Visitor extends SimpleAstVisitor<void> {
100101

101102
@override
102103
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
104+
if (node.parent.isFieldNameShortcut) return;
103105
checkIdentifier(node.name);
104106
}
105107

@@ -131,6 +133,7 @@ class _Visitor extends SimpleAstVisitor<void> {
131133

132134
@override
133135
void visitPatternField(PatternField node) {
136+
if (node.isFieldNameShortcut) return;
134137
var pattern = node.pattern;
135138
if (pattern is DeclaredVariablePattern) {
136139
checkIdentifier(pattern.name);

test/rules/non_constant_identifier_names_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ void f() {
3939
]);
4040
}
4141

42+
test_patternIfStatement_recordField() async {
43+
await assertDiagnostics(r'''
44+
void f(Object o) {
45+
if (o case (a: int AB, BC: int CD)) { }
46+
}
47+
''', [
48+
lint(40, 2),
49+
lint(52, 2),
50+
]);
51+
}
52+
53+
test_patternIfStatement_recordField_ok() async {
54+
await assertNoDiagnostics(r'''
55+
void f(Object o) {
56+
if (o case (:int AB, var b)) { }
57+
}
58+
''');
59+
}
60+
4261
test_patternIfStatement_underscores() async {
4362
await assertNoDiagnostics(r'''
4463
void f() {
@@ -85,6 +104,20 @@ void f() {
85104
]);
86105
}
87106

107+
test_patternRecordField_shortcut_ok() async {
108+
await assertNoDiagnostics(r'''
109+
f(Object o) {
110+
switch(o) {
111+
case (:int AB, var b):
112+
}
113+
switch(o) {
114+
case (:int AB?, var b):
115+
case (:int AB!, var b):
116+
}
117+
}
118+
''');
119+
}
120+
88121
test_patternRecordField_underscores() async {
89122
await assertDiagnostics(r'''
90123
void f() {

0 commit comments

Comments
 (0)