Merge branch 'pr-1033'

This commit is contained in:
Juan Martín Sotuyo Dodero
2018-04-25 00:35:26 -03:00
3 changed files with 87 additions and 12 deletions

View File

@ -125,6 +125,7 @@ Other languages are equivalent.
* [#1003](https://github.com/pmd/pmd/issues/1003): \[java] UnnecessaryConstructor triggered on required empty constructor (Dagger @Inject)
* [#1023](https://github.com/pmd/pmd/issues/1023): \[java] False positive for useless parenthesis
* java-errorprone
* [#629](https://github.com/pmd/pmd/issues/629): \[java] NullAssignment false positive
* [#816](https://github.com/pmd/pmd/issues/816): \[java] SingleMethodSingleton false positives with inner classes
* java-performance
* [#586](https://github.com/pmd/pmd/issues/586): \[java] AvoidUsingShortType erroneously triggered on overrides of 3rd party methods

View File

@ -5,21 +5,26 @@
package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTNullLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
// TODO - should check that this is not the first assignment. e.g., this is OK:
// Object x;
// x = null;
public class NullAssignmentRule extends AbstractJavaRule {
public NullAssignmentRule() {
addRuleChainVisit(ASTNullLiteral.class);
}
@Override
public Object visit(ASTNullLiteral node, Object data) {
@ -55,7 +60,19 @@ public class NullAssignmentRule extends AbstractJavaRule {
&& ((AccessNode) ((VariableNameDeclaration) name.getNameDeclaration()).getAccessNodeParent()).isFinal();
}
private boolean isBadTernary(ASTConditionalExpression n) {
return n.isTernary() && !(n.jjtGetChild(0) instanceof ASTEqualityExpression);
private boolean isBadTernary(ASTConditionalExpression ternary) {
boolean isInitializer = false;
ASTVariableInitializer variableInitializer = ternary.getFirstParentOfType(ASTVariableInitializer.class);
if (variableInitializer != null) {
ASTBlockStatement statement = ternary.getFirstParentOfType(ASTBlockStatement.class);
isInitializer = statement == variableInitializer.getFirstParentOfType(ASTBlockStatement.class);
}
return ternary.isTernary()
&& !(ternary.jjtGetChild(0) instanceof ASTEqualityExpression)
&& !isInitializer
&& !(ternary.getNthParent(2) instanceof ASTReturnStatement)
&& !(ternary.getNthParent(2) instanceof ASTLambdaExpression);
}
}

View File

@ -64,10 +64,8 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description><![CDATA[
null assignment in ternary
]]></description>
<expected-problems>1</expected-problems>
<description>null assignment in ternary - initialization</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
@ -77,15 +75,37 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description><![CDATA[
null assignment in ternary, part deux
]]></description>
<description>null assignment in ternary</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
String x;
x = bar() ? "fiz" : null;
}
}
]]></code>
</test-code>
<test-code>
<description>null assignment in ternary, part deux - initialization</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
String x = bar() ? null : "fiz";
}
}
]]></code>
</test-code>
<test-code>
<description>null assignment in ternary, part deux</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
String x;
x = bar() ? null : "fiz";
}
}
]]></code>
</test-code>
@ -129,4 +149,41 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>[java] NullAssignment false positive - initialization #629</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class NullAssignmentFP {
public void foo() {
Type result = someHash.computeIfAbsent(a, _unused -> test ? truthy : null);
}
}
]]></code>
</test-code>
<test-code>
<description>[java] NullAssignment false positive - no direct assignment, but lambda #629</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class NullAssignmentFP {
public void foo() {
Type result;
result = someHash.computeIfAbsent(a, _unused -> test ? truthy : null);
}
}
]]></code>
</test-code>
<test-code>
<description>[java] NullAssignment false positive - return with ternary #629</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class NullAssignmentFP {
public DateTime foo(DateTime dateTime) {
return dateTime.getYear() < 2100 ? dateTime : null;
}
}
]]></code>
</test-code>
</test-data>