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#11187: OperatorWrapCheck throws NPE on guarded patterns
- Loading branch information
Showing
4 changed files
with
120 additions
and
5 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
84 changes: 84 additions & 0 deletions
84
...awl/tools/checkstyle/checks/whitespace/operatorwrap/InputOperatorWrapGuardedPatterns.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,84 @@ | ||
/* | ||
OperatorWrap | ||
option = (default)nl | ||
tokens = (default)QUESTION, COLON, EQUAL, NOT_EQUAL, DIV, PLUS, MINUS, STAR, MOD, \ | ||
SR, BSR, GE, GT, SL, LE, LT, BXOR, BOR, LOR, BAND, LAND, TYPE_EXTENSION_AND, \ | ||
LITERAL_INSTANCEOF | ||
*/ | ||
//non-compiled with javac: Compilable with Java17 | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.whitespace.operatorwrap; | ||
|
||
public class InputOperatorWrapGuardedPatterns { | ||
String typeGuardAfterParenthesizedTrueSwitchStatement1(Object o) { | ||
switch (o) { | ||
case (Integer i) | ||
&& i == 0: o = String.valueOf(i); return "true"; // ok | ||
case ((Integer i) | ||
&& i == 2): o = String.valueOf(i); return "second"; // ok | ||
case Object x: return "any"; | ||
} | ||
} | ||
|
||
String typeGuardAfterParenthesizedTrueSwitchExpression1(Object o) { | ||
return switch (o) { | ||
case (Integer i) | ||
&& i == 0: o = String.valueOf(i); yield "true"; // ok | ||
case ((Integer i) | ||
&& i == 2): o = String.valueOf(i); yield "second"; // ok | ||
case Object x: yield "any"; | ||
}; | ||
} | ||
|
||
String typeGuardAfterParenthesizedTrueIfStatement1(Object o) { | ||
if (o != null | ||
&& o instanceof ((Integer i) // ok | ||
&& i == 0)) { // ok | ||
return "true"; | ||
} else if (o != null | ||
&& o instanceof (((Integer i) // ok | ||
&& i == 2)) // ok | ||
&& (o = i) != null) { // ok | ||
return "second"; | ||
} else { | ||
return "any"; | ||
} | ||
} | ||
|
||
String typeGuardAfterParenthesizedTrueSwitchStatement2(Object o) { | ||
switch (o) { | ||
case (Integer i) && // violation | ||
i == 0: o = String.valueOf(i); return "true"; | ||
case ((Integer i) && // violation | ||
i == 2): o = String.valueOf(i); return "second"; | ||
case Object x: return "any"; | ||
} | ||
} | ||
|
||
String typeGuardAfterParenthesizedTrueSwitchExpression2(Object o) { | ||
return switch (o) { | ||
case (Integer i) && // violation | ||
i == 0: o = String.valueOf(i); yield "true"; | ||
case ((Integer i) && // violation | ||
i == 2): o = String.valueOf(i); yield "second"; | ||
case Object x: yield "any"; | ||
}; | ||
} | ||
|
||
String typeGuardAfterParenthesizedTrueIfStatement2(Object o) { | ||
if (o != null && // violation | ||
o instanceof ((Integer i) && // violation | ||
i == 0)) { | ||
return "true"; | ||
} else if (o != null && // violation | ||
o instanceof (((Integer i) && // violation | ||
i == 2)) && // violation | ||
(o = i) != null) { | ||
return "second"; | ||
} else { | ||
return "any"; | ||
} | ||
} | ||
} |