dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.
https://josefpihrt.github.io/docs/roslynator
Other
3.07k stars 256 forks source link

Refactoring "invert if (recursively)" causes compiler error #1131

Open josefpihrt opened 1 year ago

josefpihrt commented 1 year ago

Product and Version Used: 4.3.0

Before:

class C
{
    void M(bool p, bool q, bool r)
    {
        if (r) // Invoke "Invert if (recursively)" here
        {
            if (p)
            {
                var x = 1;
                M2();
            }

            if (q)
            {
                var x = 2;
                M2();
            }
        }
    }

    void M2()
    {
    }
}

After:

class C
{
    void M(bool p, bool q, bool r)
    {
        if (!r)
        {
            return;
        }

        if (p)
        {
            var x = 1; // compiler error here
            M2();
        }

        if (!q)
        {
            return;
        }

        var x = 2;
        M2();
    }

    void M2()
    {
    }
}
BenjaminBrienen commented 1 month ago

I really wish C# allowed for shadowing, especially in different scopes.