From fa63ad41b3bc3142f8827444ef0a0eeb5bc4ff08 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 21 Nov 2024 18:43:31 -0800 Subject: [PATCH] Remove unnecessary parentheses around collection expressions --- ...veUnnecessaryExpressionParenthesesTests.cs | 38 +++++++++++++++---- .../UseConditionalExpressionForReturnTests.cs | 31 +++++++++++++++ ...ParenthesizedExpressionSyntaxExtensions.cs | 4 ++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryParentheses/RemoveUnnecessaryExpressionParenthesesTests.cs b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryParentheses/RemoveUnnecessaryExpressionParenthesesTests.cs index a8d90e34f4f95..f7b5eca75f4ad 100644 --- a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryParentheses/RemoveUnnecessaryExpressionParenthesesTests.cs +++ b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryParentheses/RemoveUnnecessaryExpressionParenthesesTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; @@ -20,17 +21,16 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.RemoveUnnecessaryParentheses; [Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryParentheses)] -public partial class RemoveUnnecessaryExpressionParenthesesTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor +public sealed class RemoveUnnecessaryExpressionParenthesesTests(ITestOutputHelper logger) + : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor(logger) { - public RemoveUnnecessaryExpressionParenthesesTests(ITestOutputHelper logger) - : base(logger) - { - } - internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace) => (new CSharpRemoveUnnecessaryExpressionParenthesesDiagnosticAnalyzer(), new CSharpRemoveUnnecessaryParenthesesCodeFixProvider()); - private async Task TestAsync(string initial, string expected, bool offeredWhenRequireForClarityIsEnabled, int index = 0) + private async Task TestAsync( + [StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string initial, + [StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string expected, + bool offeredWhenRequireForClarityIsEnabled, int index = 0) { await TestInRegularAndScriptAsync(initial, expected, options: RemoveAllUnnecessaryParentheses, index: index); @@ -3458,4 +3458,28 @@ public void M() } """); } + + [Fact] + public async Task TestRemoveAroundCollectionExpression() + { + await TestInRegularAndScriptAsync( + """ + class C + { + void M(bool b) + { + int[] a = b ? $$([1]) : []; + } + } + """, + """ + class C + { + void M(bool b) + { + int[] a = b ? [1] : []; + } + } + """); + } } diff --git a/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForReturnTests.cs b/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForReturnTests.cs index e942959c3fbab..667934aeeba06 100644 --- a/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForReturnTests.cs +++ b/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForReturnTests.cs @@ -2308,4 +2308,35 @@ public static string Method(bool empty) } """); } + + [Fact] + public async Task TestWithCollectionExpressions() + { + await TestInRegularAndScript1Async( + """ + class C + { + int[] M() + { + [||]if (true) + { + return [0]; + } + else + { + return [1]; + } + } + } + """, + """ + class C + { + int[] M() + { + return true ? [0] : [1]; + } + } + """); + } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs index 02042426e6d6c..4c3dbcd9d9ef9 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs @@ -111,6 +111,10 @@ public static bool CanRemoveParentheses( if (expression.IsKind(SyntaxKind.TupleExpression)) return true; + // ([...]) -> [...] + if (expression.IsKind(SyntaxKind.CollectionExpression)) + return true; + // int Prop => (x); -> int Prop => x; if (nodeParent is ArrowExpressionClauseSyntax arrowExpressionClause && arrowExpressionClause.Expression == node) {