1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-05 09:20:04 +00:00

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 <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2024-02-12 23:37:32 +00:00 committed by Commit Queue
parent c7793ec97b
commit fcca3f1b62
2 changed files with 99 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import '../analyzer.dart';
import '../extensions.dart';
@ -92,6 +93,26 @@ class _Visitor extends SimpleAstVisitor<void> {
var parent = node.parent;
// case const (a + b):
if (parent is ConstantPattern) return;
// Don't over-report on records missing trailing commas.
// (int,) r = (3);
if (parent is VariableDeclaration &&
parent.declaredElement?.type is RecordType) {
if (node.expression is! RecordLiteral) return;
}
// g((3)); => g((int,) i) { }
if (parent is ArgumentList) {
var element = node.staticParameterElement;
if (element?.type is RecordType && node.expression is! RecordLiteral) {
return;
}
}
// g(i: (3)); => g({required (int,) i}) { }
if (parent is NamedExpression &&
parent.staticParameterElement?.type is RecordType) {
if (node.expression is! RecordLiteral) return;
}
var expression = node.expression;
if (expression is SimpleIdentifier ||
expression.containsNullAwareInvocationInChain()) {

View File

@ -51,6 +51,84 @@ void g(List<int>? list) {
''');
}
test_record_assignment() async {
await assertDiagnostics(r'''
void f() {
(int,) r = ((3,));
}
''', [
lint(24, 6),
]);
}
test_record_namedParam() async {
await assertDiagnostics(r'''
void f() {
g(i: ((3,)));
}
g({required (int,) i}) { }
''', [
lint(18, 6),
]);
}
test_record_param() async {
await assertDiagnostics(r'''
void f() {
g(((3,)));
}
g((int,) i) { }
''', [
lint(15, 6),
]);
}
test_singleElementRecordWithNoTrailingComma_assignment() async {
await assertDiagnostics(r'''
void f() {
(int,) r = (3);
}
''', [
error(
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
24,
3),
]);
}
test_singleElementRecordWithNoTrailingComma_namedParam() async {
await assertDiagnostics(r'''
f() {
g(i: (3));
}
g({required (int,) i}) { }
''', [
error(
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
13,
3),
]);
}
/// https://github.com/dart-lang/linter/issues/4876
test_singleElementRecordWithNoTrailingComma_param() async {
await assertDiagnostics(r'''
f() {
g((3));
}
g((int,) i) { }
''', [
error(
CompileTimeErrorCode.RECORD_LITERAL_ONE_POSITIONAL_NO_TRAILING_COMMA,
14,
3),
]);
}
test_switchExpression_expressionStatement() async {
await assertNoDiagnostics(r'''
void f(Object? x) {