Skip to content

Commit 624361d

Browse files
Do not offer 'remove unnecessary parens' when it would change a collection initializer (#78336)
2 parents b5ba5cc + aafb3c3 commit 624361d

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/Analyzers/CSharp/Tests/RemoveUnnecessaryParentheses/RemoveUnnecessaryExpressionParenthesesTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,4 +3552,32 @@ public void M()
35523552
}
35533553
""");
35543554
}
3555+
3556+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78331")]
3557+
public async Task TestCollectionExpressionInInitializer()
3558+
{
3559+
await TestMissingInRegularAndScriptAsync("""
3560+
class C
3561+
{
3562+
public void M()
3563+
{
3564+
var v = new List<int[]>() { $$([]) };
3565+
}
3566+
}
3567+
""");
3568+
}
3569+
3570+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78331")]
3571+
public async Task TestAttributedLambdaExpressionInInitializer()
3572+
{
3573+
await TestMissingInRegularAndScriptAsync("""
3574+
class C
3575+
{
3576+
public void M()
3577+
{
3578+
var v = new List<Action>() { $$([X] () => { }) };
3579+
}
3580+
}
3581+
""");
3582+
}
35553583
}

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,29 @@ public static bool CanRemoveParentheses(
110110
if (expression.IsKind(SyntaxKind.TupleExpression))
111111
return true;
112112

113+
// Cases:
114+
// {(x)} -> {x}
115+
if (nodeParent is InitializerExpressionSyntax)
116+
{
117+
// `{ ([]) }` can't become `{ [] }` as `[` in an initializer will be parsed as an index assignment.
118+
if (tokenAfterParen.Kind() == SyntaxKind.OpenBracketToken)
119+
return false;
120+
121+
// Assignment expressions and collection expressions are not allowed in initializers
122+
// as they are not parsed as expressions, but as more complex constructs
123+
if (expression is AssignmentExpressionSyntax)
124+
return false;
125+
126+
return true;
127+
}
128+
113129
// ([...]) -> [...]
114130
if (expression.IsKind(SyntaxKind.CollectionExpression))
115131
return true;
116132

117133
// int Prop => (x); -> int Prop => x;
118134
if (nodeParent is ArrowExpressionClauseSyntax arrowExpressionClause && arrowExpressionClause.Expression == node)
119-
{
120135
return true;
121-
}
122136

123137
// Easy statement-level cases:
124138
// var y = (x); -> var y = x;
@@ -181,15 +195,6 @@ public static bool CanRemoveParentheses(
181195
if (expression.IsKind(SyntaxKind.InterpolatedStringExpression))
182196
return true;
183197

184-
// Cases:
185-
// {(x)} -> {x}
186-
if (nodeParent is InitializerExpressionSyntax)
187-
{
188-
// Assignment expressions and collection expressions are not allowed in initializers
189-
// as they are not parsed as expressions, but as more complex constructs
190-
return expression is not AssignmentExpressionSyntax and not CollectionExpressionSyntax;
191-
}
192-
193198
// Cases:
194199
// new {(x)} -> {x}
195200
// new { a = (x)} -> { a = x }

0 commit comments

Comments
 (0)