Skip to content

Invert if #88

@ElemarJR

Description

@ElemarJR

This is essentially a refactoring, so we are classifying it as a Hidden diagnostic.

Condition to raise diagnostic: more than one statement inside if block.

Scenario 1: return void

Additional condition to raise diagnostic: no code after if statement.

This:

void Foo()
{
    if (condition)
    {
        //some code
    }
}

Becomes:

void Foo()
{
    if (!condition)
        return;
    //some code
}

Scenario 2: return something

Additional condition to raise diagnostic: only a return statement after if statement and a return at the end of the if block (or in every branch).

This:

int Foo()
{
    if (condition)
    {
        //some code
        return 2;
    }
    return 1;
}

Becomes:

int Foo()
{
    if (!condition)
        return 1;
    //some code
    return 2;
}

This code should also raise a diagnostic:

int Foo()
{
    if (condition)
    {
        //some code
        if (anotherCondition)
            return 2;
        else
            return 3;
    }
    return 1;
}

Category: Refactoring
Severity: Hidden
Diagnostic id: CC0087

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions