Implemented MisleadingVariableName in the naming ruleset

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3960 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-11-04 14:08:15 +00:00
parent 5a2b87d1d7
commit d2d6e25653
5 changed files with 81 additions and 4 deletions

View File

@ -1,5 +1,5 @@
????, 2005 - 3.4:
New rules: NonThreadSafeSingleton (design ruleset), DefaultPackage (controversial ruleset)
New rules: NonThreadSafeSingleton (design ruleset), DefaultPackage (controversial ruleset), MisleadingVariableName (naming ruleset)
Fixed bug 1292745 - Removed unused source file ExceptionTypeChecking.java
Fixed bug 1292609 - The JDK 1.3 parser now correctly handles certain 'assert' usages. Also added a 'JDK 1.3' menu item to the Designer.
Fixed bug 1292689 - Description wrong for UnnecessaryLocalBeforeReturn
@ -481,4 +481,6 @@ Moved Ant task to the net.sourceforge.pmd.ant package
Added new HTML report format
June 25 2002 - 0.1:
Initial release
Initial release
Using PMD? Get "PMD Applied", the book! http://pmdapplied.com/

View File

@ -0,0 +1,43 @@
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.RuleSetNotFoundException;
import net.sourceforge.pmd.PMD;
public class MisleadingVariableNameTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("naming", "MisleadingVariableName");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "misnamed param", 1, rule),
new TestDescriptor(TEST2, "misnamed local", 1, rule),
new TestDescriptor(TEST3, "all's well", 0, rule),
});
}
private static final String TEST1 =
"public class Foo {" + PMD.EOL +
" void main(String m_foo) {" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + PMD.EOL +
" void main() {" + PMD.EOL +
" int m_foo = 42;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + PMD.EOL +
" private int m_bar;" + PMD.EOL +
" public static final int m_buz = 42;" + PMD.EOL +
" private void buz(String biz) {}" + PMD.EOL +
"}";
}

View File

@ -441,6 +441,38 @@ public class SomeClass {
]]>
</example>
</rule>
<rule name="MisleadingVariableName"
message="Avoid naming non-fields with the prefix 'm_'"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
Detects when a non-field has a name starting with 'm_'. This usually
indicates a field and thus is confusing.
</description>
<properties>
<property name="xpath" pluginname="true">
<value>
<![CDATA[
//VariableDeclaratorId
[starts-with(@Image, 'm_')]
[not (../../../FieldDeclaration)]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
private int m_foo; // OK
public void bar(String m_baz) { // Bad
int m_boz = 42; // Bad
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -7,6 +7,7 @@ These are new rules that are still in progress
</description>
<rule name="Test"
message="There may be multiple accessor methods for ''{0}''"
class="net.sourceforge.pmd.rules.SymbolTableTestRule"
@ -20,7 +21,6 @@ Test
]]>
</example>
</rule>
<!--
<rule name="UselessAssignment"
message="This assignment to ''{0}'' is useless"

View File

@ -45,6 +45,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Dave Brosius - suggested MisleadingVariableName rule, a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
<li>Johan Stuyts - nice patch to clean up build environment</li>
<li>Brian Remedios - code improvements to Eclipse plugin</li>
<li>Chris Grindstaff - fixed SWTException when Eclipse plugin is run on a file with syntax error</li>
@ -93,7 +94,6 @@
<li>Bruno Juillet - bug report for missing package/class/method names, patch for Ant task's excludeMarker attribute, bug report on ruleset overrides</li>
<li>Ian Flanigan - reported CPD JNLP breakage</li>
<li>Glen Cordrey - Reported bug involved JavaCC string handling</li>
<li>Dave Brosius - a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
<li>Oto 'tapik' Buchta - Patched XMLRenderer for UTF8 support</li>
<li>Arent-Jan Banck - Reported bug with JDK 1.5 annotation handling</li>
<li>Fred Hartman - Reported exact location of bug in TooManyFields, fixed bug in UnnecessaryBooleanAssertion</li>