forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#11220: False positive in MissingSwitchDefault with p…
…attern in switch label
- Loading branch information
Showing
6 changed files
with
336 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...kstyle/checks/coding/missingswitchdefault/InputMissingSwitchDefaultCaseLabelElements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
MissingSwitchDefault | ||
*/ | ||
|
||
//non-compiled with javac: Compilable with Java17 | ||
package com.puppycrawl.tools.checkstyle.checks.coding.missingswitchdefault; | ||
|
||
public class InputMissingSwitchDefaultCaseLabelElements { | ||
static void m(Object o) { | ||
switch (o) { // ok | ||
case null, String s -> System.out.println("String, including null"); | ||
default -> System.out.println("something else"); | ||
} | ||
|
||
switch (o) { // ok | ||
case null, String s: System.out.println("String, including null"); break; | ||
default: System.out.println("something else"); | ||
} | ||
|
||
switch(o) { | ||
case null: default: // ok | ||
System.out.println("The rest (including null)"); | ||
} | ||
|
||
switch(o) { // ok, pattern totality enforced by compiler | ||
case Object o1: | ||
System.out.println("object"); | ||
} | ||
|
||
switch(o) { // ok, pattern totality enforced by compiler | ||
case Object o1 && o1.toString().length() > 2: | ||
System.out.println("object"); | ||
break; | ||
case Object o1 && o1.toString().length() <= 2: | ||
System.out.println("object"); | ||
break; | ||
case null, Object o1: | ||
break; | ||
} | ||
|
||
switch(o) { // ok | ||
case String str, null -> | ||
System.out.println("null"); | ||
default -> System.out.println("default"); | ||
} | ||
|
||
switch(o) { // ok | ||
case null, default -> | ||
System.out.println("The rest (including null)"); | ||
} | ||
|
||
switch(o) { // ok | ||
case String s && s.length() > 2 -> | ||
System.out.println("The string longer than 2 chars"); | ||
default -> System.out.println("default!"); | ||
} | ||
|
||
switch(o) { // ok | ||
case default, null -> | ||
System.out.println("The rest (including null)"); | ||
} | ||
|
||
switch(o) { // ok | ||
case default, null: | ||
System.out.println("The rest (including null)"); | ||
} | ||
|
||
switch(o) { // ok | ||
case null, default: | ||
System.out.println("The rest (including null)"); | ||
} | ||
|
||
switch(o) { // ok | ||
case null, default: | ||
throw new UnsupportedOperationException("not supported!"); | ||
} | ||
} | ||
|
||
void m2(String s) { | ||
switch (s) { // ok | ||
case "a": throw new AssertionError("Wrong branch."); | ||
case default: break; | ||
} | ||
switch (s) { // ok | ||
case "a" -> throw new AssertionError("Wrong branch."); | ||
case default -> {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,5 +49,9 @@ public void nestedSwitch2() { | |
} | ||
default: break; | ||
} | ||
|
||
switch(i) { // violation | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.