Fixed bug 1209719 - MethodArgumentCouldBeFinal no longer triggers on arguments which are modified using postfix or prefix expressions.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3548 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-06-09 19:05:06 +00:00
parent f1c5e69d63
commit 5ead87cf4d
5 changed files with 29 additions and 15 deletions

View File

@ -5,6 +5,7 @@ Fixed bug 1201577 - PMD now correctly parses method declarations that return gen
Fixed bug 1205709 - PMD no longer takes a long time to report certain parsing errors.
Fixed bug 1052356 - ImmutableField no longer triggers on fields which are assigned to in a constructor's try statement.
Fixed bug 1215854 - Package/class/method name are now filled in whenever possible, and the XML report includes all three.
Fixed bug 1209719 - MethodArgumentCouldBeFinal no longer triggers on arguments which are modified using postfix or prefix expressions.
Fixed bug which caused MissingSerialVersionUID to trigger on all interfaces that implemented other interfaces.
Added two new node types - ASTCatchStatement and ASTFinallyStatement.
Modified rule XML definition; it no longer includes a symboltable attribute.

View File

@ -84,13 +84,12 @@ public class AvoidReassigningParametersTest extends SimpleAggregatorTst {
"}";
public static final String TEST8 =
"public class Foo {" + PMD.EOL +
" void foo(int x) {" + PMD.EOL +
" try {" + PMD.EOL +
" x = 2;" + PMD.EOL +
" } catch (Throwable t) { " + PMD.EOL +
" } " + PMD.EOL +
" }" + PMD.EOL +
"}";
"public class Foo {" + PMD.EOL +
" void foo(int x) {" + PMD.EOL +
" try {" + PMD.EOL +
" x = 2;" + PMD.EOL +
" } catch (Throwable t) { " + PMD.EOL +
" } " + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -30,6 +30,8 @@ public class MethodArgumentCouldBeFinalTest extends SimpleAggregatorTst {
new TestDescriptor(TEST7, "Shouldn't trigger on try blocks", 0, rule),
new TestDescriptor(TEST8, "Skip native methods", 0, rule),
new TestDescriptor(TEST9, "Skip abstract methods", 0, rule),
new TestDescriptor(TEST10, "self assignment of a method param means it can't be final", 0, rule),
new TestDescriptor(TEST11, "same as above but prefix vs postfix", 0, rule),
});
}
@ -93,4 +95,17 @@ public class MethodArgumentCouldBeFinalTest extends SimpleAggregatorTst {
" public abstract void bar(Object x);" + PMD.EOL +
"}";
private static final String TEST10 =
"public class Foo {" + PMD.EOL +
" public void bar(int a) {" + PMD.EOL +
" x[a++] = 1;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST11 =
"public class Foo {" + PMD.EOL +
" public void bar(int a) {" + PMD.EOL +
" x[--a] = 1;" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -33,11 +33,9 @@ public class MethodArgumentCouldBeFinal extends AbstractOptimizationRule {
Map decls = s.getVariableDeclarations();
for (Iterator i = decls.keySet().iterator(); i.hasNext();) {
VariableNameDeclaration var = (VariableNameDeclaration)i.next();
if (!var.getAccessNodeParent().isFinal() && (var.getAccessNodeParent() instanceof ASTFormalParameter)) {
if (!assigned((List)decls.get(var))) {
RuleContext ctx = (RuleContext)data;
ctx.getReport().addRuleViolation(createRuleViolation(ctx, var.getAccessNodeParent(), MessageFormat.format(getMessage(), new Object[]{var.getImage()})));
}
if (!var.getAccessNodeParent().isFinal() && (var.getAccessNodeParent() instanceof ASTFormalParameter) && !assigned((List)decls.get(var))) {
RuleContext ctx = (RuleContext)data;
ctx.getReport().addRuleViolation(createRuleViolation(ctx, var.getAccessNodeParent(), MessageFormat.format(getMessage(), new Object[]{var.getImage()})));
}
}
return data;
@ -46,7 +44,7 @@ public class MethodArgumentCouldBeFinal extends AbstractOptimizationRule {
private boolean assigned(List usages) {
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
if (occ.isOnLeftHandSide()) {
if (occ.isOnLeftHandSide() || occ.isSelfAssignment()) {
return true;
}
continue;

View File

@ -43,6 +43,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>tskariah - bug report MethodArgumentCouldBeFinal</li>
<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>Wim Deblauwe - Clover license coordination and implementation, UseCorrectExceptionLogging, coordinated and coded a much nicer asXML() implementation, suggested cleanup of UnusedFormalParameter, Javadoc patch, SystemPrintln bug report, helped get Ant task and CLI squared away with JDK 1.5 params, JDK 1.5-specific bug reports, suggested improvements for ExceptionSignatureDeclaration</li>
<li>Ian Flanigan - reported CPD JNLP breakage</li>