Checked in Chad's UnconditionalIfStatement rule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2518 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2004-01-20 20:36:50 +00:00
parent 8f6c1ddc3b
commit d532b57e84
5 changed files with 93 additions and 15 deletions

View File

@ -1,5 +1,5 @@
????, 2004 - 1.5:
New rules: DontImportSunRule, EmptyFinalizer, EmptyStaticInitializer, AvoidDollarSigns
New rules: DontImportSunRule, EmptyFinalizer, EmptyStaticInitializer, AvoidDollarSigns, UnconditionalIfStatement
TODO - fix it so tests and rules don't duplicate the xpath expressions
Added isTrue() to ASTBooleanLiteral.
Added UnaryExpression to the AST.

View File

@ -0,0 +1,48 @@
package test.net.sourceforge.pmd.rules;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.rules.XPathRule;
public class UnconditionalIfStatementRuleTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() {
rule = new XPathRule();
rule.addProperty("xpath", "//IfStatement/Expression/ConditionalAndExpression/InstanceOfExpression/UnaryExpression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "if (true)", 1, rule),
new TestDescriptor(TEST2, "if (false)", 1, rule),
new TestDescriptor(TEST3, "no constant folding", 0, rule)
});
}
private static final String TEST1 =
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" if (true) {}" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" if (false) {}" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + PMD.EOL +
" private static final boolean DEBUG = \"false\";" + PMD.EOL +
" void bar() {" + PMD.EOL +
" if (DEBUG) {}" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -37,38 +37,38 @@ public class UnnecessaryConstructorRuleTest extends SimpleAggregatorTst {
}
private static final String TEST1 =
"public class UnnecessaryConstructor1 {" + PMD.EOL +
" public UnnecessaryConstructor1() {}" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" public Foo() {}" + PMD.EOL +
"}";
private static final String TEST2 =
"public class UnnecessaryConstructor2 {" + PMD.EOL +
" private UnnecessaryConstructor2() {}" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private Foo() {}" + PMD.EOL +
"}";
private static final String TEST3 =
"public class UnnecessaryConstructor3 {" + PMD.EOL +
" public UnnecessaryConstructor3(int x) {}" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" public Foo(int x) {}" + PMD.EOL +
"}";
private static final String TEST4 =
"public class UnnecessaryConstructor4 {" + PMD.EOL +
" public UnnecessaryConstructor4() { " + PMD.EOL +
"public class Foo {" + PMD.EOL +
" public Foo() { " + PMD.EOL +
" int x = 2;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST5 =
"public class UnnecessaryConstructor5 {" + PMD.EOL +
" public UnnecessaryConstructor5() throws IOException { " + PMD.EOL +
"public class Foo {" + PMD.EOL +
" public Foo() throws IOException { " + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST6 =
"public class UnnecessaryConstructor6 {" + PMD.EOL +
" public UnnecessaryConstructor6() {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" public Foo() {" + PMD.EOL +
" }" + PMD.EOL +
" public UnnecessaryConstructor6(String foo) {}" + PMD.EOL +
" public Foo(String foo) {}" + PMD.EOL +
"}";
private static final String TEST7 =

View File

@ -116,6 +116,36 @@ These are new rules for the next release
</rule>
<rule name="UnconditionalIfStatement"
message="Do not use 'if' statements that are always true or always false"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
Do not use "if" statements that are always true or always false.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//IfStatement/Expression/ConditionalAndExpression
/InstanceOfExpression/UnaryExpression
/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void close() {
if (true) {
// ...
}
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -38,8 +38,8 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Chad Loder - UnconditionalIfStatement, AvoidDollarSigns, EmptyStaticInitializer, EmptyFinalizerMethod rule, DontImportSun rule, improvements to ASTBooleanLiteral</li>
<li>Maik Schreiber - AccessNode bug report, other bug reports</li>
<li>Chad Loder - AvoidDollarSigns, EmptyStaticInitializer, EmptyFinalizerMethod rule, DontImportSun rule, improvements to ASTBooleanLiteral</li>
<li>Lokesh Gupta - improvements to the AST viewer</li>
<li>Jesse Glick - improvements to VariableNamingConventionsRule, patch for UnusedModifierRule, bug fix for VariableNameDeclarations rule, an excellent discussion on the UnnecessaryConstructorRule</li>
<li>Nicolas Liochon - CloneShouldCallSuperCloneRule implementation</li>