roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS1208 False-positive on flat if statement with multiple conditions

Open wiciok opened this issue 1 year ago • 1 comments

Product and Version Used: Roslynator.Analyzers 4.12.2

Steps to Reproduce: Following code or similar one containing if statement with multiple conditions, but without any nesting:

private void Foo(string bar, int baz)
{
    if (bar == "bar" && baz == 123)  // RCS1208: Reduce 'if' nesting
    {
        var foo = "baz";
    }
}

Please note, that the issue doesn't occur, when there is some other code present after if statement in question:

private void Foo(string bar, int baz)
{
    if (bar == "bar" && baz == 123)
    {
        var foo = "baz";
    }
    var thisDoes = "something preventing RCS1208 in if statement above";
}

Actual Behavior:

RCS1208 is raised, suggesting rather counterproductive change to:

private void Foo(string bar, int baz)
{
    if (bar != "bar" || baz != 123)
    {
        return;
    }
    var foo = "baz";
}

Expected Behavior: Do not raise RCS1208 in case of flat if statements with multiple conditions.

wiciok avatar Apr 26 '24 09:04 wiciok

Also facing false positive, this triggers the analyzer:

if (last.End > time)
{
    return last;
}

return null;

but if the if is inverted:

if (last.End <= time)
{
    return null;
}

return last;

then it's fine

AdrianoAE avatar Aug 15 '24 13:08 AdrianoAE