-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ern matching and support switch expressions (pmd#5251) Merge pull request pmd#5251 from adangel:issue-5249-5250
- Loading branch information
Showing
6 changed files
with
163 additions
and
77 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
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
72 changes: 0 additions & 72 deletions
72
.../net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForASwitchStatement.xml
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
.../resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml
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,141 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<test-data | ||
xmlns="http://pmd.sourceforge.net/rule-tests" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> | ||
|
||
<test-code> | ||
<description>Switch Statement with no case, ok</description> | ||
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
public class DumbSwitch { | ||
public void foo(int i) { | ||
switch (i) { } // This is detected by EmptyControlStatement | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Switch Statement with only one case, not ok</description> | ||
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class DumbSwitch { | ||
public void foo(int i) { | ||
switch (i) { | ||
case 0: | ||
{ | ||
System.err.println("I am a fish."); | ||
} | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Switch Expression with only one case, not ok #5250</description> | ||
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class DumbSwitch { | ||
public String foo(int i) { | ||
return switch (i) { | ||
case 0: | ||
{ | ||
yield "I am a fish."; | ||
} | ||
}; | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Even two branches is not enough for a switch statement</description> | ||
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class DumbSwitch { | ||
public void foo(int i) { | ||
switch (i) { | ||
case 0: | ||
{ | ||
System.err.println("I am a fish."); | ||
} | ||
case 1: | ||
{ | ||
System.exit(); | ||
} | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Three branches in a switch statement is ok.</description> | ||
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
public class ValidSwitch { | ||
public void foo(int i) { | ||
switch (i) { | ||
case 0: | ||
{ | ||
System.err.println("I am a fish."); | ||
} | ||
case 1: | ||
{ | ||
System.exit(); | ||
} | ||
case 2: | ||
{ | ||
// ... | ||
} | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>[java] TooFewBranchesForASwitchStatement false positive for Pattern Matching #5249</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
sealed interface S permits A {} | ||
final class A implements S {} | ||
public class Sample { | ||
public void simpleSwitchStatment(S s) { | ||
switch(s) { | ||
case A a -> System.out.println("a"); | ||
} | ||
} | ||
public void simpleSwitchExpression(S s) { | ||
String result = switch(s) { | ||
case A a -> "a"; | ||
}; | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Record patterns are ignored, too #5249</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
record R(int i) {} | ||
public class SwitchWithRecordPattern { | ||
public void check(R r) { | ||
switch(r) { | ||
case R(int a) -> System.out.println(a); | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
</test-data> |