new rule AvoidLiteralsInIfCondition added to controversial ruleset

Patch contributed by Nadhamuni Kothapalle

https://sourceforge.net/tracker/?func=detail&atid=479923&aid=2591627&group_id=56262


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6888 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2009-03-22 22:50:57 +00:00
parent f7cefa4a7d
commit 2321376151
4 changed files with 60 additions and 0 deletions

View File

@ -6,6 +6,7 @@ Fixed bug 2606609 - False "UnusedImports" positive in package-info.java
Fixed bug 2645268 - ClassCastException in UselessOperationOnImmutable.getDeclaration Fixed bug 2645268 - ClassCastException in UselessOperationOnImmutable.getDeclaration
New rule: New rule:
Controversial : AvoidLiteralsInIfCondition (patch 2591627)
StrictExceptions : AvoidCatchingGenericException StrictExceptions : AvoidCatchingGenericException
February 08, 2009 - 4.2.5: February 08, 2009 - 4.2.5:

View File

@ -10,6 +10,7 @@ public class ControversialRulesTest extends SimpleAggregatorTst {
public void setUp() { public void setUp() {
addRule("controversial", "AssignmentInOperand"); addRule("controversial", "AssignmentInOperand");
addRule("controversial", "AvoidFinalLocalVariable"); addRule("controversial", "AvoidFinalLocalVariable");
addRule("controversial", "AvoidLiteralsInIfCondition");
addRule("controversial", "AvoidUsingNativeCode"); addRule("controversial", "AvoidUsingNativeCode");
addRule("controversial", "AvoidUsingShortType"); addRule("controversial", "AvoidUsingShortType");
addRule("controversial", "AvoidUsingVolatile"); addRule("controversial", "AvoidUsingVolatile");

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description>basic test</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class PrimitiveType {
public void downCastPrimitiveType() {
if(i==1) {
}
}
}
]]></code>
</test-code>
<test-code>
<description>basic test, part2</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class PrimitiveType {
public static final int PRIMITIVE_TYPE = 1;
public void downCastPrimitiveType() {
if(i==PRIMITIVE_TYPE) {
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -682,6 +682,36 @@ public void doSomething() { // Explicit gc call ! Runtime.getRuntime().gc(); } }
</example> </example>
</rule> </rule>
<rule name="AvoidLiteralsInIfCondition"
since="4.2.6"
message="Avoid using Literals in Conditional Statements"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/controversial.html#AvoidLiteralsInIfCondition">
<description>
Avoid using hard coded literals in conditional statements, declare those as static variables or private members.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//IfStatement/Expression/*/PrimaryExpression/PrimaryPrefix/Literal
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class PrimitiveType {
public void downCastPrimitiveType() {
if(i==1) {
}
}
}
]]>
</example>
</rule>
</ruleset> </ruleset>