roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS1111 should trigger when case returns directly or throws an exception

Open daze99 opened this issue 1 year ago • 2 comments

Product and Version Used: Roslynator 4.12

Steps to Reproduce:

private string Test(int i)
{
    var result = string.Empty;

    switch (i)
    {
        case 0:
            result = i.ToString();
            break;

        case 1:
            var s = i.ToString();
            return s;

        case 2:
            return i.ToString();

        default:
            throw new Exception();
    }

    return result;
}

Actual Behavior:

private string Test(int i)
{
    var result = string.Empty;

    switch (i)
    {
        case 0:
        {
            result = i.ToString();
            break;
        }

        case 1:
        {
            var s = i.ToString();
            return s;
        }

        case 2:
            return i.ToString();

        default:
            throw new Exception();
    }

    return result;
}

Expected Behavior:

private string Test(int i)
{
    var result = string.Empty;

    switch (i)
    {
        case 0:
        {
            result = i.ToString();
            break;
        }

        case 1:
        {
            var s = i.ToString();
            return s;
        }

        case 2:
        {
            return i.ToString();
        }

        default:
        {
            throw new Exception();
        }
    }

    return result;
}

daze99 avatar Apr 12 '24 15:04 daze99

RCS1111 is called "Add braces to switch section with multiple statements". Why do you think is should trigger when there is a single statement?

josefpihrt avatar Apr 24 '24 07:04 josefpihrt

Yes, you are right. The title says is all.

Anyway it would be nice to have another analyzer "Add braces to switch section" like it exists for if statements (IDE0011).

daze99 avatar Apr 24 '24 09:04 daze99