Add rule for unnecessary boolean in conditionals espressions

Summary: Add rule for unnecessary literals boolean in conditionals espressions

Test Plan: mvn test

Reviewers: jmsotuyo

Reviewed By: jmsotuyo

Differential Revision: http://ph.monits.com/D11961
This commit is contained in:
Damian Techeira
2015-07-23 17:07:15 -03:00
parent 62202a5293
commit e0cd7adeef
3 changed files with 142 additions and 0 deletions

View File

@ -795,6 +795,59 @@ public class Count {
</example>
</rule>
<rule name="SimplifiedTernary"
language="java"
since="5.3.2"
message="Ternary operators that can be simplified with || or &amp;&amp;"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#SimplifiedTernary">
<description>
<![CDATA[
Look for ternary operators with the form condition ? literalBoolean : foo
or condition ? foo : literalBoolean.
These expressions can be simplified respectively to
condition || foo when the literalBoolean is true
!condition && foo when the literalBoolean is false
or
!condition || foo when the literalBoolean is true
condition && foo when the literalBoolean is false
]]>
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//ConditionalExpression[not(PrimaryExpression//BooleanLiteral) and (Expression//BooleanLiteral)]
|
//ConditionalExpression[not(Expression//BooleanLiteral) and (PrimaryExpression//BooleanLiteral)]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean test() {
return condition ? true : something(); // can be as simple as return condition || something();
}
public void test2() {
final boolean value = condition ? false : something(); // can be as simple as value = !condition && something();
}
public boolean test3() {
return condition ? something() : true; // can be as simple as return !condition || something();
}
public void test4() {
final boolean otherValue = condition ? something() : false; // can be as simple as condition && something();
}
}
]]>
</example>
</rule>
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyCatchBlock" />
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyIfStmt" />
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyWhileStmt" />

View File

@ -37,5 +37,6 @@ public class BasicRulesTest extends SimpleAggregatorTst {
addRule(RULESET, "ReturnFromFinallyBlock");
addRule(RULESET, "DontCallThreadRun");
addRule(RULESET, "DontUseFloatTypeForLoopIndices");
addRule(RULESET, "SimplifiedTernary");
}
}

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description>
<![CDATA[
condition ? true : foo
]]>
</description>
<expected-problems>1</expected-problems>
<code>
<![CDATA[
public class Foo {
public boolean test() {
return condition ? true : something();
}
}
]]>
</code>
</test-code>
<test-code>
<description>
<![CDATA[
condition ? false : foo
]]>
</description>
<expected-problems>1</expected-problems>
<code>
<![CDATA[
public class Foo {
public void test() {
final boolean value = condition ? false : something();
}
}
]]>
</code>
</test-code>
<test-code>
<description>
<![CDATA[
condition ? foo : true
]]>
</description>
<expected-problems>1</expected-problems>
<code>
<![CDATA[
public class Foo {
public boolean test() {
return condition ? something() : true;
}
}
]]>
</code>
</test-code>
<test-code>
<description>
<![CDATA[
condition ? foo : false
]]>
</description>
<expected-problems>1</expected-problems>
<code>
<![CDATA[
public class Foo {
public void test() {
final boolean otherValue = condition ? something() : false;
}
}
]]>
</code>
</test-code>
<test-code>
<description>
<![CDATA[
condition ? true : false
]]>
</description>
<expected-problems>0</expected-problems>
<code>
<![CDATA[
public class Foo {
public boolean test() {
return condition ? true : false; // Existing rule
}
}
]]>
</code>
</test-code>
</test-data>