Skip to content

Convert switch cases where every case returns into a returned switch expression #800

@timtebeek

Description

@timtebeek

Similar to

Describe the situation before applying the recipe

class B {
    String doFormat(String str) {
        switch (str) {
            case "foo": return "Foo";
            case "bar": return "Bar";
            case null, default: return "Other";
        }
    }
}

Describe the situation after applying the recipe

class B {
    String doFormat(String str) {
        return switch (str) {
            case "foo" -> "Foo";
            case "bar" -> "Bar";
            case null, default -> "Other";
        };
    }
}

Any additional context

Ideally again starting only with switches where every case is a direct return, and only later expanding to cover additional usages.

Metadata

Metadata

Assignees

Labels

recipeRecipe requested

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions