Merge branch 'pr-2773' into master
[java] issue-2738: Adding null check to avoid npe when switch case is default #2773
This commit is contained in:
@@ -17,7 +17,8 @@ This is a {{ site.pmd.release_type }} release.
|
||||
### Fixed Issues
|
||||
|
||||
* pmd-java
|
||||
* [#2708](https://github.com/pmd/pmd/pull/2708): \[java] False positive FinalFieldCouldBeStatic when using lombok Builder.Default
|
||||
* [#2708](https://github.com/pmd/pmd/issues/2708): \[java] False positive FinalFieldCouldBeStatic when using lombok Builder.Default
|
||||
* [#2738](https://github.com/pmd/pmd/issues/2738): \[java] Custom rule with @ExhaustiveEnumSwitch throws NPE
|
||||
|
||||
|
||||
### API Changes
|
||||
@@ -25,6 +26,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
### External Contributions
|
||||
|
||||
* [#2747](https://github.com/pmd/pmd/pull/2747): \[java] Don't trigger FinalFieldCouldBeStatic when field is annotated with lombok @Builder.Default - [Ollie Abbey](https://github.com/ollieabbey)
|
||||
* [#2773](https://github.com/pmd/pmd/pull/2773): \[java] issue-2738: Adding null check to avoid npe when switch case is default - [Nimit Patel](https://github.com/nimit-patel)
|
||||
|
||||
{% endtocmaker %}
|
||||
|
||||
|
@@ -86,7 +86,10 @@ public class ASTSwitchStatement extends AbstractJavaNode implements Iterable<AST
|
||||
// since this is an enum switch, the labels are necessarily
|
||||
// the simple name of some enum constant.
|
||||
|
||||
constantNames.remove(label.getFirstDescendantOfType(ASTName.class).getImage());
|
||||
// descendant can be null for default case
|
||||
if (label.getFirstDescendantOfType(ASTName.class) != null) {
|
||||
constantNames.remove(label.getFirstDescendantOfType(ASTName.class).getImage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ASTSwitchStatementTest extends BaseParserTest {
|
||||
|
||||
@Test
|
||||
public void exhaustiveEnumSwitchWithDefault() {
|
||||
ASTSwitchStatement switchStatement = getNodes(ASTSwitchStatement.class,
|
||||
"import java.nio.file.AccessMode; class Foo { void bar(AccessMode m) {"
|
||||
+ "switch (m) { case READ: break; default: break; } } }")
|
||||
.get(0);
|
||||
Assert.assertFalse(switchStatement.isExhaustiveEnumSwitch()); // this should not throw a NPE...
|
||||
Assert.assertTrue(switchStatement.hasDefaultCase());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user