Skip to content

Commit fcca3f1

Browse files
pqCommit Queue
authored andcommitted
fix unnecessary_parens overreporting on records missing commas
Fixes: https://github.com/dart-lang/linter/issues/4876 Change-Id: I171709f15b5341e91726024f387d9f842a524b4e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352104 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent c7793ec commit fcca3f1

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/type.dart';
910

1011
import '../analyzer.dart';
1112
import '../extensions.dart';
@@ -92,6 +93,26 @@ class _Visitor extends SimpleAstVisitor<void> {
9293
var parent = node.parent;
9394
// case const (a + b):
9495
if (parent is ConstantPattern) return;
96+
97+
// Don't over-report on records missing trailing commas.
98+
// (int,) r = (3);
99+
if (parent is VariableDeclaration &&
100+
parent.declaredElement?.type is RecordType) {
101+
if (node.expression is! RecordLiteral) return;
102+
}
103+
// g((3)); => g((int,) i) { }
104+
if (parent is ArgumentList) {
105+
var element = node.staticParameterElement;
106+
if (element?.type is RecordType && node.expression is! RecordLiteral) {
107+
return;
108+
}
109+
}
110+
// g(i: (3)); => g({required (int,) i}) { }
111+
if (parent is NamedExpression &&
112+
parent.staticParameterElement?.type is RecordType) {
113+
if (node.expression is! RecordLiteral) return;
114+
}
115+
95116
var expression = node.expression;
96117
if (expression is SimpleIdentifier ||
97118
expression.containsNullAwareInvocationInChain()) {

pkg/linter/test/rules/unnecessary_parenthesis_test.dart

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,84 @@ void g(List<int>? list) {
5151
''');
5252
}
5353

54+
test_record_assignment() async {
55+
await assertDiagnostics(r'''
56+
void f() {
57+
(int,) r = ((3,));
58+
}
59+
''', [
60+
lint(24, 6),
61+
]);
62+
}
63+
64+
test_record_namedParam() async {
65+
await assertDiagnostics(r'''
66+
void f() {
67+
g(i: ((3,)));
68+
}
69+
g({required (int,) i}) { }
70+
71+
''', [
72+
lint(18, 6),
73+
]);
74+
}
75+
76+
test_record_param() async {
77+
await assertDiagnostics(r'''
78+
void f() {
79+
g(((3,)));
80+
}
81+
g((int,) i) { }
82+
83+
''', [
84+
lint(15, 6),
85+
]);
86+
}
87+
88+
test_singleElementRecordWithNoTrailingComma_assignment() async {
89+
await assertDiagnostics(r'''
90+
void f() {
91+
(int,) r = (3);
92+
}
93+
''', [
94+
error(
95+
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
96+
24,
97+
3),
98+
]);
99+
}
100+
101+
test_singleElementRecordWithNoTrailingComma_namedParam() async {
102+
await assertDiagnostics(r'''
103+
f() {
104+
g(i: (3));
105+
}
106+
107+
g({required (int,) i}) { }
108+
''', [
109+
error(
110+
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
111+
13,
112+
3),
113+
]);
114+
}
115+
116+
/// https://github.com/dart-lang/linter/issues/4876
117+
test_singleElementRecordWithNoTrailingComma_param() async {
118+
await assertDiagnostics(r'''
119+
f() {
120+
g((3));
121+
}
122+
123+
g((int,) i) { }
124+
''', [
125+
error(
126+
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
127+
14,
128+
3),
129+
]);
130+
}
131+
54132
test_switchExpression_expressionStatement() async {
55133
await assertNoDiagnostics(r'''
56134
void f(Object? x) {

0 commit comments

Comments
 (0)