Added piair's new StringBufferInstantiationWithChar rule, thanks piair!

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4788 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-11-13 02:19:51 +00:00
parent 5b1b058a60
commit cf59280967
6 changed files with 109 additions and 3 deletions

View File

@ -3,6 +3,7 @@ New rules:
Basic ruleset: BigIntegerInstantiation(1.4 and 1.5)
Codesize ruleset: NPathComplexity, NcssTypeCount, NcssMethodCount, NcssConstructorCount
Design ruleset: UseCollectionIsEmpty
Strings ruleset: StringBufferInstantiationWithChar
Typeresolution ruleset: Loose Coupling - This is a re-implementation using the new Type Resolution facility
Fixed bug 1570915 - AvoidRethrowingException no longer reports a false positive for certain nested exceptions.
Fixed bug 1571324 - UselessStringValueOf no longer reports a false positive for additive expressions.

View File

@ -0,0 +1,36 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package test.net.sourceforge.pmd.rules.strings;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Rule;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
public class StringBufferInstantiationWithCharTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() throws Exception {
rule = findRule("strings", "StringBufferInstantiationWithChar");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "OK", 0, rule),
new TestDescriptor(TEST2, "failure case", 1, rule),
});
}
private static final String TEST1 =
"public class Foo {" + PMD.EOL +
" StringBuffer sb = new StringBuffer(\"c\");" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + PMD.EOL +
" StringBuffer sb = new StringBuffer('c');" + PMD.EOL +
"}";
}

View File

@ -72,7 +72,7 @@ public class RuleTst extends TestCase {
public void runTestFromString(TestDescriptor test,
SourceType sourceType) throws Throwable {
int res = processUsingStringReader(test.getCode(), test.getRule(), sourceType).size();
assertEquals("\"" + test.getDescription() + "\" test resulted in wrong amount of failures,",
assertEquals("\"" + test.getDescription() + "\" test resulted in wrong number of failures,",
test.getNumberOfProblemsExpected(), res);
}

View File

@ -10,9 +10,41 @@ These are new rules that are still in progress.
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
</rule>
<rule name="StringBufferInstantiationWithChar"
message="Do not instantiate a StringBuffer with a char"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strings.html#StringBufferInstantiationWithChar">
<description>
StringBuffer sb = new StringBuffer('c'); The
char will be converted into int to intialize
StringBuffer size.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression/ClassOrInterfaceType
[@Image='StringBuffer']
/../Arguments/ArgumentList/Expression/PrimaryExpression
/PrimaryPrefix/
Literal [(starts-with(@Image, "'"))][(ends-with
(@Image, "'"))]
]]>
</value>
</property>
</properties>
<priority>4</priority>
<example>
<![CDATA[
class Foo {
StringBuffer sb1 = new StringBuffer('c'); //Bad
StringBuffer sb2 = new StringBuffer("c"); //Better
}
]]>
</example>
</rule>
<!--
<rule name="UselessAssignment"
message="This assignment to ''{0}'' is useless"

View File

@ -288,5 +288,41 @@ public String convert(int i) {
</example>
</rule>
<rule name="StringBufferInstantiationWithChar"
message="Do not instantiate a StringBuffer with a char"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strings.html#StringBufferInstantiationWithChar">
<description>
StringBuffer sb = new StringBuffer('c'); The
char will be converted into int to intialize
StringBuffer size.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression/ClassOrInterfaceType
[@Image='StringBuffer']
/../Arguments/ArgumentList/Expression/PrimaryExpression
/PrimaryPrefix/
Literal [(starts-with(@Image, "'"))][(ends-with
(@Image, "'"))]
]]>
</value>
</property>
</properties>
<priority>4</priority>
<example>
<![CDATA[
class Foo {
StringBuffer sb1 = new StringBuffer('c'); //Bad
StringBuffer sb2 = new StringBuffer("c"); //Better
}
]]>
</example>
</rule>
</ruleset>

View File

@ -52,6 +52,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>piair - Implemented StringBufferInstantiationWithChar</li>
<li>Christopher Eagan - Reported bug in VariableNamingConventions</li>
<li>Jason Bennett - Noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor</li>
<li><a href="http://www.livejournal.com/users/insac/">Fabio Insaccanebbia</a> - Improvement for UseArraysAsList, UnusedNullCheckInEquals, MisplacedNullCheck, UselessOperationOnImmutable, AvoidArrayLoops, UseArraysAsList, AvoidConstantsInterface, AvoidDecimalLiteralsInBigDecimalConstructor, ClassCastExceptionWithToArray, BigIntegerInstantiation_1.4, BigIntegerInstantiation_1.5</li>