Skip to content

Report option to simplify code when return / break continue present in if and else blocks #2073

@dlsniper

Description

@dlsniper

When return / break keywords are present both in if and else blocks, there should be an inspection to tell that the else keyword / branch could be dropped.

Example:

package demo

func _(a int) {
    if a == 1 {
        return
    } else {
        return
    }
}

func _() {
    for i := 1; i < 10; i++ {
        if i == 3 {
            break
        } else {
            continue
        }
    }

    for i := 1; i < 10; i++ {
        if i == 3 {
            break
        } else {
            break
        }
    }
}

This is also part of the CodeReview guideline: https://github.com/golang/go/wiki/CodeReviewComments#indent-error-flow

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions