roslyn: Bug in ParenthesizedExpressionSyntaxExtensions.cs?

Version Used: Microsoft.CodeAnalysis.CSharp.Workspaces.1.2.1

context The code in Microsoft.CodeAnalysis.CSharp.Workspaces/Extensions/ParenthesizedExpressionSyntaxExtensions.cs is part of the Simplification feature. The function bool CanRemoveParentheses(this ParenthesizedExpressionSyntax node) determines if parentheses can be removed.

As part of this, the function RemovalChangesAssociation is called (same source file). This function will correctly determine that, for example, the parentheses in a + (b * c) can be removed, but not in a * (b + c).

problem The problem arises with expressions of the form a + (b + c) and a * (b * c). According to this function, the parentheses can be removed. However, that is not correct.

For example, consider the following code:

class C
{
    public static string operator+(C left, C right) => "";

    public static string Problem => "" + (new C() + new C());
}

expected behavior Removal of the parenthesis in the Problem expression alters the meaning of the expression, so they cannot be removed.

actual behavior The current implementation will remove them.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28 (28 by maintainers)

Most upvoted comments

I trust Dustin and yourself on this one 😉

Famous last words?

That works for me @DustinCampbell . Looks like we should either blacklist some cases, or whitelist others. I think i’m leaning toward a whitelist myself. That way just removing this optimization is a case where the whitelist is initially empty. As we find cases that are safe to do, we just add them to the whitelist.

I’ll have some time to take a look at this tomorrow. I’m itching to fix a bug anyways. 😃