Resharper Structural Pattern Replacement adds extra empty braces

Answered

I need help with a resharper structural pattern that removes a redundant variable. The output has an extra pair of curly braces:

This is my target:

public bool functionName(object argument)
{
    var redundantVariable = false;
    var actualVariable = false;

    if (!actualVariable)
    {
        actualVariable = true;
    }
    else
    {
        throw new Exception("nasty exception");
    }

    redundantVariable = actualVariable;
    return redundantVariable;
}

Pattern:

{
    $retT$ $reduntant$ = $default$;
    $retT$ $retVal$ = $default$;
            
    $smth$
            
    $reduntant$ = $retVal$;
            
    return $reduntant$;
}

Replacement

{
    $retT$ $retVal$ = $default$;            
    $smth$            
    return $retVal$;
}

Resharper output (note the extra braces):

public bool functionName(object argument)
{
}
{
    bool actualVariable = false;
    if (!actualVariable)
    {
        actualVariable = true;
    }
    else
    {
        throw new Exception("nasty exception");
    }
    return actualVariable;
}
1 comment
Comment actions Permalink
Official comment

Hello Jzuloaga,

Thank you for contacting us.
It's a known issue. Here's a corresponding bug report - https://youtrack.jetbrains.com/issue/RSRP-473275
Feel free to comment or vote for it.

Also, you can fix it by deleting opening and closing curly braces in your search and replacement pattern. They are not necessary in this case.
For this, go to ReSharper | Options | Code Inspection | Custom Patterns | Add Pattern/Edit and delete opening and closing curly braces.

Here you can read more about Add/Edit/Search with Highlighting Pattern dialog.

Please sign in to leave a comment.