File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff 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;
Original file line number Diff line number Diff 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'''
2349class 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'''
You can’t perform that action at this time.
0 commit comments