Fixed bug 1556594 - Wonky detection of NullAssignment

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6418 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch 2008-08-28 01:56:07 +00:00
parent 263b91d556
commit 5f2d71cdde
3 changed files with 19 additions and 2 deletions

View File

@ -13,6 +13,7 @@ Fixed bug 2002722 - false + in UseStringBufferForStringAppends
Fixed bug 2056318 - False positive for AvoidInstantiatingObjectsInLoops
Fixed bug 1977438 - False positive for UselessStringValueOf
Fixed bug 2050064 - False + SuspiciousOctalEscape with backslash literal
Fixed bug 1556594 - Wonky detection of NullAssignment
Optimizations and false positive fixes in PreserveStackTrace
@SuppressWarnings("all") disables all warnings
All comment types are now stored in ASTCompilationUnit, not just formal ones

View File

@ -113,4 +113,17 @@ public class Foo {
}
]]></code>
</test-code>
</test-data>
<test-code>
<description><![CDATA[
1556594 - Wonky detection of NullAssignment
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void foo() {
assert isRoot() ? parentContext == null : parentContext != null;
}
}
]]></code>
</test-code>
</test-data>

View File

@ -7,6 +7,7 @@ import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTAssignmentOperator;
import net.sourceforge.pmd.ast.ASTConditionalExpression;
import net.sourceforge.pmd.ast.ASTEqualityExpression;
import net.sourceforge.pmd.ast.ASTExpression;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTNullLiteral;
import net.sourceforge.pmd.ast.ASTStatementExpression;
@ -30,10 +31,12 @@ public class NullAssignmentRule extends AbstractRule {
addViolation(data, node);
}
} else if (node.getNthParent(4) instanceof ASTConditionalExpression) {
// "false" expression of ternary
if (isBadTernary((ASTConditionalExpression)node.getNthParent(4))) {
addViolation(data, node);
}
} else if (node.getNthParent(5) instanceof ASTConditionalExpression) {
} else if (node.getNthParent(5) instanceof ASTConditionalExpression && node.getNthParent(4) instanceof ASTExpression) {
// "true" expression of ternary
if (isBadTernary((ASTConditionalExpression)node.getNthParent(5))) {
addViolation(data, node);
}