Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on redundant null cases in switch expressions and enhanced switch statements #628

Closed
wmdietl opened this issue Nov 23, 2023 · 0 comments · Fixed by #634
Closed

Warn on redundant null cases in switch expressions and enhanced switch statements #628

wmdietl opened this issue Nov 23, 2023 · 0 comments · Fixed by #634

Comments

@wmdietl
Copy link
Member

wmdietl commented Nov 23, 2023

Take this example:

class SwitchRedundantNullCase {
    void foo(Object o) {
        // :: (nulltest.redundant)
        if (o == null) {
            System.out.println("o is null");
        }
        switch(o) {
        case Number n: System.out.println("Number: " + n); break;
        // :: (nulltest.redundant)
        case null: System.out.println("null"); break;
        default: System.out.println("anything else");
        }
    }
}

When we run ./checker/bin/javac -processor nullness SwitchRedundantNullCase.java -Alint=redundantNullComparison we get:

RedundantSwitchOnNull.java:4: warning: [nulltest.redundant] redundant test against null; "o" is non-null
	  if (o == null) {
	        ^
1 warning

We should also get a warning about a case null when the expression is non-null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant