A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5311 below:

[java] TooFewBranchesForSwitch false positive for exhaustive switches over enums without default case · Issue #5311 · pmd/pmd · GitHub

Affects PMD Version: 7.7.0

Rule: TooFewBranchesForSwitch

Description:
When Switch with enums, this rule reports a violations when there are too few branches.

Using switch is preferable, as the compiler will generate a compiler error if a new enum value is added and the switch doesn't cover all cases. Would the switch be rewritten with if-else then there will never be a compiler error if a new enum value appears.

Code Sample demonstrating the issue:

public class Foo {

    String foo(Bar bar) {
        return switch (bar) {
            case A -> "A";
            case B -> "B";
        };
    }

    enum Bar {
        A,
        B
    }
}

Expected outcome:

PMD reports a violation at line ..., but that's wrong. That's a false positive.
Even thoug this switch has only two cases this is preferable, as when a new Enum value C is added, the compiler will generate an error.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4