linter: allow unnecessary parens in spreads

Fixes https://github.com/dart-lang/linter/issues/3816

Change-Id: I3e5928e939de18b95cb27d2214a16b045c9aabc0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/360400
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/linter/lib/src/rules/unnecessary_parenthesis.dart b/pkg/linter/lib/src/rules/unnecessary_parenthesis.dart
index dc3f260..6332d29 100644
--- a/pkg/linter/lib/src/rules/unnecessary_parenthesis.dart
+++ b/pkg/linter/lib/src/rules/unnecessary_parenthesis.dart
@@ -94,6 +94,9 @@
     // `case const (a + b):` is OK.
     if (parent is ConstantPattern) return;
 
+    // `[...(p as List)]` is OK.
+    if (parent is SpreadElement) return;
+
     // Don't over-report on records missing trailing commas.
     // `(int,) r = (3);` is OK.
     if (parent is VariableDeclaration &&
diff --git a/pkg/linter/test/rules/unnecessary_parenthesis_test.dart b/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
index 27ff412..ce79949 100644
--- a/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
+++ b/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
@@ -205,6 +205,22 @@
     ]);
   }
 
+  test_spread() async {
+    await assertNoDiagnostics(r'''
+void f(Object p) {
+  [...(p as List)];
+}
+''');
+  }
+
+  test_spread_nullAware() async {
+    await assertNoDiagnostics(r'''
+void f(Object? p) {
+  [...?(p as List?)];
+}
+''');
+  }
+
   test_switchExpression_expressionStatement() async {
     await assertNoDiagnostics(r'''
 void f(Object? x) {