A RetroSearch Logo

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

Search Query:

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

[java] SwitchStmtsShouldHaveDefault false positive with pattern matching · Issue #4813 · pmd/pmd · GitHub

Affects PMD Version: 7.0.0-rc4

Rule:

Please provide the rule name and a link to the rule documentation:

Description:

Code Sample demonstrating the issue:

    public sealed interface AcceptableResult permits Success, Warning {
        public String message();
    }

    public final class Success implements AcceptableResult {

        @Override
        public String message() {
            return "Success!";
        }
    }

    public abstract class Failure {

        abstract public String message();
    }

    public non-sealed class Warning extends Failure implements AcceptableResult {

        @Override
        public String message() {
            return "Oopsie";
        }
    }

    public class ProviderWarning extends Warning {

        @Override
        public String message() {
            return "Ohoh";
        }
    }

    public void test(AcceptableResult result) {
        switch (result) {
            case Warning failure -> System.out.println("WARNING " + failure.message());
            case Success success -> System.out.println(success.message());
        }
    }

Expected outcome:

PMD reports violations on the switch SwitchStmtsShouldHaveDefault and TooFewBranchesForASwitchStatement. For SwitchStmtsShouldHaveDefault it's a false positive since we have covered all of the cases here. As for TooFewBranchesForASwitchStatement I think it's also a false positive in this case, since using a pattern matching switch is the cleanest way to do this kind of stuff even if there are only 2 branches.

Running PMD through: * Maven*

boris-petrov, lexinator and KeepItSimpleStupid


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