Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove unnecessary parentheses around collection expressions #76023

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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] : [];
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading