Skip to content

'Remove unnecessary cast' adds unnecessary parentheses #25554

@Neme12

Description

@Neme12

Inside a switch case:

switch (true)
{
    case (bool)default(bool):
}
switch (true)
{
    case (default(bool)):
}

inside a when clause:

switch (true)
{
    case true when (bool)default(bool):
}
switch (true)
{
    case true when (default(bool)):
}

inside an is pattern expression:

if (true is (bool)default(bool)) ;
if (true is (default(bool))) ;

Tagging @CyrusNajmabadi who's working on a code fix specifically to remove unnecessary parentheses. The problem is inside ParenthesizedExpressionSyntaxExtensions.CanRemoveParentheses. The first two cases should be trivial to fix but that's not the case inside is, where associativity plays a role. For example it's important to add tests so that parentheses are not removed here:

if (true is (true == true)) ;

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions