Skip to content

Commit 5d5977f

Browse files
srawlinsCommit Queue
authored andcommitted
lint rule: unnecessary_parenthesis: fix switch expression case
Fixes https://github.com/dart-lang/linter/issues/5008 Change-Id: I423dbc4a431b5495b032c7803d6b691b65179022 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389783 Auto-Submit: Samuel Rawlins <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent aa6f85c commit 5d5977f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ class _Visitor extends SimpleAstVisitor<void> {
7070
parent.declaredElement2?.type is RecordType) {
7171
if (expression is! RecordLiteral) return;
7272
}
73+
7374
// `g((3)); => g((int,) i) { }` is OK.
7475
if (parent is ArgumentList) {
7576
var element = node.correspondingParameter;
7677
if (element?.type is RecordType && node.expression is! RecordLiteral) {
7778
return;
7879
}
7980
}
81+
8082
// `g(i: (3)); => g({required (int,) i}) { }` is OK.
8183
if (parent is NamedExpression &&
8284
parent.correspondingParameter?.type is RecordType) {
@@ -158,16 +160,6 @@ class _Visitor extends SimpleAstVisitor<void> {
158160
return;
159161
}
160162

161-
// https://github.com/dart-lang/linter/issues/2944
162-
if (expression is FunctionExpression) {
163-
if (parent is MethodInvocation ||
164-
parent is PropertyAccess ||
165-
parent is BinaryExpression ||
166-
parent is IndexExpression) {
167-
return;
168-
}
169-
}
170-
171163
if (expression is ConstructorReference) {
172164
if (parent is! FunctionExpressionInvocation ||
173165
parent.typeArguments == null) {
@@ -230,6 +222,7 @@ class _Visitor extends SimpleAstVisitor<void> {
230222
// An expression with internal whitespace can be made more readable when
231223
// wrapped in parentheses in many cases. But when the parentheses are
232224
// inside one of the following nodes, the readability is not affected.
225+
// See https://github.com/dart-lang/linter/issues/2944.
233226
if (parent is! AssignmentExpression &&
234227
parent is! ConstructorFieldInitializer &&
235228
parent is! ExpressionFunctionBody &&
@@ -327,7 +320,9 @@ extension on Expression? {
327320
self is AssignmentExpression ||
328321
self is AwaitExpression ||
329322
self is BinaryExpression ||
323+
self is FunctionExpression ||
330324
self is IsExpression ||
325+
self is SwitchExpression ||
331326
// As in, `!(new Foo())`.
332327
(self is InstanceCreationExpression && self.keyword != null) ||
333328
// No TypedLiteral (ListLiteral, MapLiteral, SetLiteral) accepts `-`

pkg/linter/test/rules/unnecessary_parenthesis_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,15 @@ void f(Object? x) {
566566
''');
567567
}
568568

569+
test_switchExpressionInside_methodInvocation() async {
570+
await assertNoDiagnostics(r'''
571+
void f(Object v) {
572+
const v = 0;
573+
(switch (v) { _ => Future.value() }).then((_) {});
574+
}
575+
''');
576+
}
577+
569578
test_switchExpressionInside_variableDeclaration() async {
570579
await assertDiagnostics(r'''
571580
void f(Object? x) {

0 commit comments

Comments
 (0)