From 420c6d45a3c4250aba4c0817eb3a5bb3a99300b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 23 Jan 2017 01:43:40 -0300 Subject: [PATCH 1/2] Fixes #199 - False positives with ternary operator - I deected a couple of extra related issues with ternary operators which are now fixed. --- .../resources/rulesets/java/unnecessary.xml | 7 +++- .../unnecessary/UnnecessaryRulesTest.java | 2 +- .../unnecessary/xml/UselessParentheses.xml | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/resources/rulesets/java/unnecessary.xml b/pmd-java/src/main/resources/rulesets/java/unnecessary.xml index d6efe8fe2b..2651fbe26b 100644 --- a/pmd-java/src/main/resources/rulesets/java/unnecessary.xml +++ b/pmd-java/src/main/resources/rulesets/java/unnecessary.xml @@ -230,11 +230,13 @@ public class Test { count(*)=1 and count(./CastExpression)=0 and count(./EqualityExpression/MultiplicativeExpression)=0 and + count(./ConditionalExpression[@Ternary='true'])=0 and count(./ConditionalOrExpression)=0] | //Expression/ConditionalOrExpression/PrimaryExpression/PrimaryPrefix/Expression[ count(*)=1 and count(./CastExpression)=0 and + count(./ConditionalExpression[@Ternary='true'])=0 and count(./EqualityExpression/MultiplicativeExpression)=0] | //Expression/ConditionalExpression/PrimaryExpression/PrimaryPrefix/Expression[ @@ -242,7 +244,7 @@ public class Test { count(./CastExpression)=0 and count(./EqualityExpression)=0] | -//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral = 'true'])]/PrimaryExpression[1]/PrimaryPrefix/Expression[ +//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral='true'])]/PrimaryExpression[1]/PrimaryPrefix/Expression[ count(*)=1 and not(./CastExpression) and not(./AdditiveExpression[@Image = '-']) and @@ -263,6 +265,7 @@ public class Test { count(./AndExpression)=0 and count(./InclusiveOrExpression)=0 and count(./ExclusiveOrExpression)=0 and + count(./ConditionalExpression)=0 and count(./ConditionalAndExpression)=0 and count(./ConditionalOrExpression)=0 and count(./EqualityExpression)=0] @@ -339,4 +342,4 @@ public class Foo { - \ No newline at end of file + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/unnecessary/UnnecessaryRulesTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/unnecessary/UnnecessaryRulesTest.java index 1bd462dcf7..f48e1ad479 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/unnecessary/UnnecessaryRulesTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/unnecessary/UnnecessaryRulesTest.java @@ -6,7 +6,7 @@ package net.sourceforge.pmd.lang.java.rule.unnecessary; import net.sourceforge.pmd.testframework.SimpleAggregatorTst; /** - * Rule tests for the runnecessary ruleset + * Rule tests for the unnecessary ruleset */ public class UnnecessaryRulesTest extends SimpleAggregatorTst { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unnecessary/xml/UselessParentheses.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unnecessary/xml/UselessParentheses.xml index 120cd03e2b..e76724d8a0 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unnecessary/xml/UselessParentheses.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unnecessary/xml/UselessParentheses.xml @@ -292,6 +292,46 @@ class ExampleClass { eGood = (a || b) + c; eGood = (a ? b : c) + d; } +} + ]]> + + + #199 UselessParentheses false positive on multiple logical and operators + 0 + + + + #199 UselessParentheses false positive on multiple logical or operators + 0 + + + + #199 UselessParentheses false positive on equals operator + 0 + From 6b1d00639a34fa09a8afc8feba40686247f4cfd5 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 27 Jan 2017 16:44:12 +0100 Subject: [PATCH 2/2] Update changelog --- src/site/markdown/overview/changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index f66896370c..170c9d127d 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -51,6 +51,8 @@ See also [bugfix #1556](https://sourceforge.net/p/pmd/bugs/1556/). * [#1546](https://sourceforge.net/p/pmd/bugs/1546/): \[java] UnnecessaryFullyQualifiedNameRule doesn't take into consideration conflict resolution * [#1547](https://sourceforge.net/p/pmd/bugs/1547/): \[java] UnusedImportRule - False Positive for only usage in Javadoc - {@link ClassName#CONSTANT} * [#1555](https://sourceforge.net/p/pmd/bugs/1555/): \[java] UnnecessaryFullyQualifiedName: Really necessary fully qualified name +* java-unnecessary + * [#199](https://github.com/pmd/pmd/issues/199): \[java] UselessParentheses: Parentheses in return statement are incorrectly reported as useless * XML * [#1518](https://sourceforge.net/p/pmd/bugs/1518/): \[xml] Error while processing xml file with ".webapp" in the file or directory name * psql