Skip to content

Commit ae3501b

Browse files
authored
fix no_leading_underscores_for_local_identifiers pattern field over-reporting (#4296)
* fix `no_leading_underscores_for_local_identifiers` pattern field over-reporting * ++
1 parent 17dcb46 commit ae3501b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/src/rules/no_leading_underscores_for_local_identifiers.dart

+10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class _Visitor extends SimpleAstVisitor<void> {
105105

106106
@override
107107
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
108+
if (node.parent.isFieldNameShortcut) return;
108109
checkIdentifier(node.name);
109110
}
110111

@@ -145,3 +146,12 @@ class _Visitor extends SimpleAstVisitor<void> {
145146
}
146147
}
147148
}
149+
150+
extension on AstNode? {
151+
bool get isFieldNameShortcut {
152+
var node = this;
153+
if (node is NullCheckPattern) node = node.parent;
154+
if (node is NullAssertPattern) node = node.parent;
155+
return node is PatternField && node.name != null && node.name?.name == null;
156+
}
157+
}

test/rules/no_leading_underscores_for_local_identifiers_test.dart

+50
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ f() {
9797
]);
9898
}
9999

100+
test_objectPattern_destructured_field() async {
101+
await assertNoDiagnostics(r'''
102+
class A {
103+
int _a;
104+
A(this._a);
105+
}
106+
f() {
107+
var A(:_a) = A(1);
108+
}
109+
''');
110+
}
111+
100112
test_objectPattern_ifCase() async {
101113
await assertDiagnostics(r'''
102114
class C {
@@ -129,6 +141,24 @@ f() {
129141
]);
130142
}
131143

144+
test_objectPattern_switch_field() async {
145+
await assertNoDiagnostics(r'''
146+
class A {
147+
var _a;
148+
}
149+
150+
f(A a) {
151+
switch (a) {
152+
case A(:var _a):
153+
}
154+
switch (a) {
155+
case A(:var _a?):
156+
case A(:var _a!):
157+
}
158+
}
159+
''');
160+
}
161+
132162
test_recordPattern_destructured() async {
133163
await assertDiagnostics(r'''
134164
f() {
@@ -139,6 +169,16 @@ f() {
139169
]);
140170
}
141171

172+
test_recordPattern_destructured_field() async {
173+
await assertDiagnostics(r'''
174+
f() {
175+
var (a: _a, :b) = (a: 1, b: 1);
176+
}
177+
''', [
178+
lint(16, 2),
179+
]);
180+
}
181+
142182
test_recordPattern_ifCase() async {
143183
await assertDiagnostics(r'''
144184
f(Object o) {
@@ -149,6 +189,16 @@ f(Object o) {
149189
]);
150190
}
151191

192+
test_recordPattern_ifCase_field() async {
193+
await assertDiagnostics(r'''
194+
f(Object o) {
195+
if (o case (x: int _x, :int y)) {}
196+
}
197+
''', [
198+
lint(35, 2),
199+
]);
200+
}
201+
152202
test_recordPattern_switch() async {
153203
await assertDiagnostics(r'''
154204
f() {

0 commit comments

Comments
 (0)