Skip to content

Commit e7d44ae

Browse files
kallentuCommit Queue
authored andcommitted
[linter] Dot shorthands: Update unnecessary_const lint.
Update `unnecessary_const` so it fires when we explicitly write a const dot shorthand in a constant context. Unit tests added and passing. Fixes: #60912 Bug: #60893 Change-Id: I5d2768dae4c55a3437fe2ec063af021d47721d98 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434580 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 43b5c14 commit e7d44ae

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class UnnecessaryConst extends LintRule {
2525
@override
2626
void registerNodeProcessors(NodeLintRegistry registry, RuleContext context) {
2727
var visitor = _Visitor(this);
28+
registry.addDotShorthandConstructorInvocation(this, visitor);
2829
registry.addInstanceCreationExpression(this, visitor);
2930
registry.addListLiteral(this, visitor);
3031
registry.addRecordLiteral(this, visitor);
@@ -36,6 +37,17 @@ class _Visitor extends SimpleAstVisitor<void> {
3637
final LintRule rule;
3738
_Visitor(this.rule);
3839

40+
@override
41+
void visitDotShorthandConstructorInvocation(
42+
DotShorthandConstructorInvocation node,
43+
) {
44+
var constKeyword = node.constKeyword;
45+
if (constKeyword == null) return;
46+
if (node.inConstantContext) {
47+
rule.reportAtToken(constKeyword);
48+
}
49+
}
50+
3951
@override
4052
void visitInstanceCreationExpression(InstanceCreationExpression node) {
4153
var keyword = node.keyword;

pkg/linter/test/rules/unnecessary_const_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ class UnnecessaryConstPatternsTest extends LintRuleTest {
1818
@override
1919
String get lintRule => LintNames.unnecessary_const;
2020

21+
test_case_constConstructor_dotShorthand_named_ok() async {
22+
await assertNoDiagnostics(r'''
23+
class C {
24+
const C.named();
25+
}
26+
f(C c) {
27+
switch (c) {
28+
case const .named():
29+
}
30+
}
31+
''');
32+
}
33+
34+
test_case_constConstructor_dotShorthand_ok() async {
35+
await assertNoDiagnostics(r'''
36+
class C {
37+
const C();
38+
}
39+
f(C c) {
40+
switch (c) {
41+
case const .new():
42+
}
43+
}
44+
''');
45+
}
46+
2147
test_case_constConstructor_ok() async {
2248
await assertNoDiagnostics(r'''
2349
class C {
@@ -73,6 +99,30 @@ const c = const C();
7399
);
74100
}
75101

102+
test_constConstructor_dotShorthand() async {
103+
await assertDiagnostics(
104+
r'''
105+
class C {
106+
const C();
107+
}
108+
const C c = const .new();
109+
''',
110+
[lint(37, 5)],
111+
);
112+
}
113+
114+
test_constConstructor_dotShorthand_named() async {
115+
await assertDiagnostics(
116+
r'''
117+
class C {
118+
const C.named();
119+
}
120+
const C c = const .named();
121+
''',
122+
[lint(43, 5)],
123+
);
124+
}
125+
76126
test_listLiteral() async {
77127
await assertDiagnostics(
78128
r'''

0 commit comments

Comments
 (0)