Skip to content

Commit 0bb629e

Browse files
kallentuCommit Queue
authored andcommitted
[analysis_server] Dot shorthands: Update prefer_const_constructors lint.
Now that `DotShorthandsConstructorInvocation`'s `canBeConst` is updated, we can also update the `prefer_const_constructors` lint. We skip the lint when we have the `@literal` metadata on the constructor since it's already reporting a warning here: https://dart-review.googlesource.com/c/sdk/+/434881. Similar to instance creation expressions, we also skip reporting a lint if we're instantiating the `Object` class. Otherwise, if the constructor we're calling with the shorthand is const and we can const-ify it, we report this lint. Added unit tests. Fixes: #60911 Bug: #60893 Change-Id: Ib4bcba629f60e0dbff110080918adfacfb1ba5d2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434981 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent b3c579d commit 0bb629e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class PreferConstConstructors extends LintRule {
2424
@override
2525
void registerNodeProcessors(NodeLintRegistry registry, RuleContext context) {
2626
var visitor = _Visitor(this);
27+
registry.addDotShorthandConstructorInvocation(this, visitor);
2728
registry.addInstanceCreationExpression(this, visitor);
2829
}
2930
}
@@ -33,6 +34,30 @@ class _Visitor extends SimpleAstVisitor<void> {
3334

3435
_Visitor(this.rule);
3536

37+
@override
38+
void visitDotShorthandConstructorInvocation(
39+
DotShorthandConstructorInvocation node,
40+
) {
41+
if (node.isConst) return;
42+
43+
var element = node.constructorName.element;
44+
if (element is! ConstructorElement) return;
45+
if (!element.isConst) return;
46+
47+
// Handled by an analyzer warning.
48+
if (element.metadata.hasLiteral) return;
49+
50+
var enclosingElement = element.enclosingElement;
51+
if (enclosingElement is ClassElement && enclosingElement.isDartCoreObject) {
52+
// Skip lint for `new Object()`, because it can be used for ID creation.
53+
return;
54+
}
55+
56+
if (node.canBeConst) {
57+
rule.reportAtNode(node);
58+
}
59+
}
60+
3661
@override
3762
void visitInstanceCreationExpression(InstanceCreationExpression node) {
3863
if (node.isConst) return;

pkg/linter/test/rules/prefer_const_constructors_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ var a = A({});
7474
);
7575
}
7676

77+
test_canBeConst_dotShorthand() async {
78+
await assertDiagnostics(
79+
r'''
80+
class A {
81+
const A();
82+
}
83+
A a = .new();
84+
''',
85+
[lint(31, 6)],
86+
);
87+
}
88+
7789
test_canBeConst_explicitTypeArgument_dynamic() async {
7890
await assertDiagnostics(
7991
r'''
@@ -275,6 +287,15 @@ var a = A();
275287
''');
276288
}
277289

290+
test_cannotBeConst_notConstConstructor_dotShorthand() async {
291+
await assertNoDiagnostics(r'''
292+
class A {
293+
A();
294+
}
295+
A a = .new();
296+
''');
297+
}
298+
278299
test_cannotBeConst_stringLiteralArgument_withInterpolation() async {
279300
await assertNoDiagnostics(r'''
280301
class A {
@@ -363,6 +384,28 @@ K k() {
363384
);
364385
}
365386

387+
test_extraPositionalArgument_dotShorthands() async {
388+
await assertDiagnostics(
389+
r'''
390+
import 'package:meta/meta.dart';
391+
392+
class K {
393+
@literal
394+
const K();
395+
}
396+
397+
K k() {
398+
K kk = .new();
399+
return kk;
400+
}
401+
''',
402+
[
403+
// No lint
404+
error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 88, 6),
405+
],
406+
);
407+
}
408+
366409
test_isConst_intLiteralArgument() async {
367410
await assertNoDiagnostics(r'''
368411
class A {
@@ -386,6 +429,12 @@ var a = const A();
386429
test_objectConstructorCall() async {
387430
await assertNoDiagnostics(r'''
388431
var x = Object();
432+
''');
433+
}
434+
435+
test_objectConstructorCall_dotShorthand() async {
436+
await assertNoDiagnostics(r'''
437+
Object x = .new();
389438
''');
390439
}
391440
}

0 commit comments

Comments
 (0)