From dc68f9a8fb72e952a754af3a2e1a43d9b1e49a69 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Wed, 20 Sep 2006 03:54:09 +0000 Subject: [PATCH] Patch to fix false+ in SimplifyConditional; thanks again to Xavier! git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4515 51baf565-9d33-0410-a72c-fc3788e3496d --- .../sourceforge/pmd/rules/SimplifyConditionalTest.java | 8 ++++++++ pmd/rulesets/design.xml | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) 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]]] ]]>