Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

fix non_constant_identifier_names pattern field over-reporting #4300

Merged
merged 3 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/rules/non_constant_identifier_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';
import '../extensions.dart';
import '../util/ascii_utils.dart';
import '../utils.dart';

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

@override
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
if (node.parent.isFieldNameShortcut) return;
checkIdentifier(node.name);
}

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

@override
void visitPatternField(PatternField node) {
if (node.isFieldNameShortcut) return;
var pattern = node.pattern;
if (pattern is DeclaredVariablePattern) {
checkIdentifier(pattern.name);
Expand Down
33 changes: 33 additions & 0 deletions test/rules/non_constant_identifier_names_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ void f() {
]);
}

test_patternIfStatement_recordField() async {
await assertDiagnostics(r'''
void f(Object o) {
if (o case (a: int AB, BC: int CD)) { }
}
''', [
lint(40, 2),
lint(52, 2),
]);
}

test_patternIfStatement_recordField_ok() async {
await assertNoDiagnostics(r'''
void f(Object o) {
if (o case (:int AB, var b)) { }
}
''');
}

test_patternIfStatement_underscores() async {
await assertNoDiagnostics(r'''
void f() {
Expand Down Expand Up @@ -85,6 +104,20 @@ void f() {
]);
}

test_patternRecordField_shortcut_ok() async {
await assertNoDiagnostics(r'''
f(Object o) {
switch(o) {
case (:int AB, var b):
}
switch(o) {
case (:int AB?, var b):
case (:int AB!, var b):
}
}
''');
}

test_patternRecordField_underscores() async {
await assertDiagnostics(r'''
void f() {
Expand Down