diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/SimplifyConditionalTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/SimplifyConditionalTest.java index a224a9c6e4..6d5e1f2b6e 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/SimplifyConditionalTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/SimplifyConditionalTest.java @@ -20,6 +20,7 @@ public class SimplifyConditionalTest extends SimpleAggregatorTst { new TestDescriptor(TEST3, "transpose x and null, still bad", 1, rule), new TestDescriptor(TEST4, "conditional or and !(instanceof)", 1, rule), new TestDescriptor(TEST5, "indexing into array is ok", 0, rule), + new TestDescriptor(TEST6, "test for null on unrelated object is ok", 0, rule), }); } @@ -58,4 +59,11 @@ public class SimplifyConditionalTest extends SimpleAggregatorTst { " }" + PMD.EOL + "}"; + private static final String TEST6 = + "public class Foo {" + PMD.EOL + + " void bar(Object x) {" + PMD.EOL + + " if (x instanceof String && ((String)x).substring(2) != null) {}" + PMD.EOL + + " }" + PMD.EOL + + "}"; + } diff --git a/pmd/rulesets/design.xml b/pmd/rulesets/design.xml index b01d8d8fca..1152d6958d 100644 --- a/pmd/rulesets/design.xml +++ b/pmd/rulesets/design.xml @@ -1020,7 +1020,7 @@ No need to check for null before an instanceof; the instanceof keyword returns f UnaryExpressionNotPlusMinus [@Image='!']//InstanceOfExpression[PrimaryExpression //Name/@Image = ancestor::ConditionalOrExpression/EqualityExpression - //PrimaryPrefix/Name/@Image]] + /PrimaryExpression/PrimaryPrefix/Name/@Image]] or ConditionalAndExpression [EqualityExpression[@Image='!=']//NullLiteral @@ -1028,7 +1028,7 @@ ConditionalAndExpression InstanceOfExpression [PrimaryExpression[count(PrimarySuffix[@ArrayDereference='true'])=0] //Name/@Image = ancestor::ConditionalAndExpression - /EqualityExpression//PrimaryPrefix/Name/@Image]]] + /EqualityExpression/PrimaryExpression/PrimaryPrefix/Name/@Image]]] ]]>