Skip to content

Commit 95c6b1f

Browse files
FMorschelCommit Queue
authored andcommitted
[linter] Fixes use_named_constants for dot-shorthands
Fixes: #62206 Change-Id: I73633eb56f2ac9cd290dc884445d923485d09397 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/466901 Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent bfeebf0 commit 95c6b1f

File tree

2 files changed

+69
-30
lines changed

2 files changed

+69
-30
lines changed

pkg/linter/lib/src/rules/use_named_constants.dart

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class UseNamedConstants extends AnalysisRule {
3030
) {
3131
var visitor = _Visitor(this);
3232
registry.addInstanceCreationExpression(this, visitor);
33+
registry.addDotShorthandConstructorInvocation(this, visitor);
3334
}
3435
}
3536

@@ -38,41 +39,49 @@ class _Visitor extends SimpleAstVisitor<void> {
3839

3940
_Visitor(this.rule);
4041

42+
@override
43+
void visitDotShorthandConstructorInvocation(
44+
DotShorthandConstructorInvocation node,
45+
) {
46+
if (node.isConst) {
47+
_reportWhenMatchingConstant(node);
48+
}
49+
}
50+
4151
@override
4252
void visitInstanceCreationExpression(InstanceCreationExpression node) {
4353
if (node.isConst) {
44-
var type = node.staticType;
45-
if (type is! InterfaceType) return;
46-
var element = type.element;
47-
if (element is ClassElement) {
48-
var nodeField = node
49-
.thisOrAncestorOfType<VariableDeclaration>()
50-
?.declaredFragment
51-
?.element;
54+
_reportWhenMatchingConstant(node);
55+
}
56+
}
57+
58+
void _reportWhenMatchingConstant(Expression node) {
59+
var type = node.staticType;
60+
if (type is! InterfaceType) return;
61+
var element = type.element;
62+
if (element is ClassElement) {
63+
var nodeField = node
64+
.thisOrAncestorOfType<VariableDeclaration>()
65+
?.declaredFragment
66+
?.element;
5267

53-
// avoid diagnostic for fields in the same class having the same value
54-
// class A {
55-
// const A();
56-
// static const a = A();
57-
// static const b = A();
58-
// }
59-
if (nodeField?.enclosingElement == element) return;
68+
// avoid diagnostic for fields in the same class having the same value
69+
// class A {
70+
// const A();
71+
// static const a = A();
72+
// static const b = A();
73+
// }
74+
if (nodeField?.enclosingElement == element) return;
6075

61-
var library =
62-
(node.root as CompilationUnit).declaredFragment?.element.library;
63-
if (library == null) return;
64-
var value = node.computeConstantValue()?.value;
65-
for (var field in element.fields.where(
66-
(e) => e.isStatic && e.isConst,
67-
)) {
68-
if (field.isAccessibleIn(library) &&
69-
field.computeConstantValue() == value) {
70-
rule.reportAtNode(
71-
node,
72-
arguments: ['${element.name}.${field.name}'],
73-
);
74-
return;
75-
}
76+
var library =
77+
(node.root as CompilationUnit).declaredFragment?.element.library;
78+
if (library == null) return;
79+
var value = node.computeConstantValue()?.value;
80+
for (var field in element.fields.where((e) => e.isStatic && e.isConst)) {
81+
if (field.isAccessibleIn(library) &&
82+
field.computeConstantValue() == value) {
83+
rule.reportAtNode(node, arguments: ['${element.name}.${field.name}']);
84+
return;
7685
}
7786
}
7887
}

pkg/linter/test/rules/use_named_constants_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ void f(A a) {
5858
);
5959
}
6060

61+
test_dotShorthand() async {
62+
await assertDiagnostics(
63+
r'''
64+
void f() {
65+
const A a = .new(0);
66+
}
67+
class A {
68+
const A(int value);
69+
static const zero = A(0);
70+
}
71+
''',
72+
[lint(25, 7)],
73+
);
74+
}
75+
76+
test_dotShorthand_const() async {
77+
await assertDiagnostics(
78+
r'''
79+
void f() {
80+
A a = const .new(0);
81+
}
82+
class A {
83+
const A(int value);
84+
static const zero = A(0);
85+
}
86+
''',
87+
[lint(19, 13)],
88+
);
89+
}
90+
6191
test_duplicate_inDefinition() async {
6292
await assertNoDiagnostics(r'''
6393
class A {

0 commit comments

Comments
 (0)