fixing unused local variable bugs

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@365 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-15 21:31:04 +00:00
parent 974c6c9f21
commit b17e43333b
10 changed files with 57 additions and 32 deletions

View File

@ -36,7 +36,7 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<!--<pmd reportFile="c:\pmd.html" rulesetfiles="rulesets/basic.xml" format="html">-->
<pmd reportFile="c:\jdk14.html" rulesetfiles="rulesets/new_for_0_5.xml" format="html">
<pmd reportFile="c:\jdk14.html" rulesetfiles="rulesets/unusedcode.xml" format="html">
<!--<fileset dir="c:\data\pmd\pmd\src">-->
<!--<fileset dir="c:\j2sdk1.4.0\src\com\sun\corba">-->
<fileset dir="c:\j2sdk1.4.0\src\">

View File

@ -36,18 +36,6 @@ public class SymbolTableTest extends TestCase {
assertEquals(child.getParent(), parent);
}
public void testAddSameSymbol() {
SymbolTable parent = new SymbolTable();
parent.add(FOO);
SymbolTable child = new SymbolTable(parent);
try {
child.add(FOO);
} catch (RuntimeException e) {
return; // cool
}
throw new RuntimeException("Should have thrown RuntimeException");
}
public void testParentContains2() {
SymbolTable parent = new SymbolTable();
SymbolTable child = new SymbolTable(parent);

View File

@ -78,11 +78,22 @@ public class UnusedLocalVariableTest extends RuleTst {
assertTrue(report.isEmpty());
}
/*
public void testUnusedLocal10() throws Throwable {
Report report = process("Unused10.java", rule);
assertTrue(report.isEmpty());
}
*/
public void testUnusedLocal11() throws Throwable {
Report report = process("Unused11.java", rule);
assertEquals(2, report.size());
}
public void testUnusedLocal12() throws Throwable {
Report report = process("Unused12.java", rule);
assertTrue(report.isEmpty());
}
public void testUnusedLocal13() throws Throwable {
Report report = process("Unused13.java", rule);
assertTrue(report.isEmpty());
}
}

View File

@ -29,8 +29,6 @@ public class UnusedPrivateInstanceVariableRuleTest extends RuleTst {
public void testUnusedPrivateInstanceVar1() throws Throwable {
Report report = process("UnusedPrivateInstanceVar1.java", rule);
assertEquals(1, report.size());
Iterator i = report.iterator();
assertEquals(rule, ((RuleViolation)i.next()).getRule());
}
public void testUnusedPrivateInstanceVar2() throws Throwable {
@ -41,8 +39,6 @@ public class UnusedPrivateInstanceVariableRuleTest extends RuleTst {
public void testUnusedPrivateInstanceVar3() throws Throwable {
Report report = process("UnusedPrivateInstanceVar3.java", rule);
assertEquals(1, report.size());
Iterator i = report.iterator();
assertEquals(rule, ((RuleViolation)i.next()).getRule());
}
public void testUnusedPrivateInstanceVar4() throws Throwable {
@ -58,6 +54,7 @@ public class UnusedPrivateInstanceVariableRuleTest extends RuleTst {
Report report = process("UnusedPrivateInstanceVar7.java", rule);
assertTrue(report.isEmpty());
}
public void testUnusedPrivateInstanceVar8() throws Throwable {
Report report = process("UnusedPrivateInstanceVar8.java", rule);
assertTrue(report.isEmpty());

View File

@ -30,7 +30,7 @@ public class PMD {
try {
JavaParser parser = new JavaParser(reader);
ASTCompilationUnit c = parser.CompilationUnit();
//c.dump("");
// c.dump("");
List acus = new ArrayList();
acus.add(c);
ruleSet.apply(acus, ctx);

View File

@ -31,9 +31,6 @@ public class SymbolTable {
if (usageCounts.containsKey(symbol)) {
throw new RuntimeException(symbol + " is already in the symbol table");
}
if (parent != null && parent.contains(symbol)) {
throw new RuntimeException(symbol + " is already in the parent symbol table");
}
usageCounts.put(symbol, ZERO);
}

View File

@ -33,28 +33,31 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{
return data;
}
// these AST types trigger creation of a new symbol table scope
// these AST types trigger a new scope
public Object visit(ASTBlock node, Object data){return addTable(node, data);}
public Object visit(ASTConstructorDeclaration node, Object data){return addTable(node, data);}
public Object visit(ASTMethodDeclaration node, Object data){return addTable(node, data);}
public Object visit(ASTFieldDeclaration node, Object data){return addTable(node, data);}
public Object visit(ASTTryStatement node, Object data){return addTable(node, data);}
public Object visit(ASTForStatement node, Object data){return addTable(node, data);}
// these AST types trigger creation of a new symbol table
// these AST types trigger a new scope
// these AST types are variable/name usages
/**
* This collects the symbols for later reference
*/
public Object visit(ASTVariableDeclaratorId node, Object data) {
//System.out.println("ASTVariableDeclaratorId.getImage() = " + node.getImage());
if (!(node.jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration)) {
return super.visit(node, data);
}
Namespace group = (Namespace)nameSpaces.peek();
group.peek().add(new Symbol(node.getImage(), node.getBeginLine()));
Namespace nameSpace = (Namespace)nameSpaces.peek();
nameSpace.peek().add(new Symbol(node.getImage(), node.getBeginLine()));
return super.visit(node, data);
}
/**
* This records usage of a symbol
*/
public Object visit(ASTName node, Object data) {
//System.out.println("ASTName.getImage() = " + node.getImage() + "; " + node.getBeginLine());
if (node.jjtGetParent() instanceof ASTPrimaryPrefix) {
String img = (node.getImage().indexOf('.') == -1) ? node.getImage() : node.getImage().substring(0, node.getImage().indexOf('.'));
Namespace group = (Namespace)nameSpaces.peek();
@ -62,7 +65,6 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{
}
return super.visit(node, data);
}
// these AST types are variable/name usages
private void reportUnusedLocals(RuleContext ctx, SymbolTable table) {
for (Iterator i = table.getUnusedSymbols(); i.hasNext();) {
@ -72,9 +74,9 @@ public class UnusedLocalVariableRule extends AbstractRule implements Rule{
}
private Object addTable(SimpleNode node, Object data) {
RuleContext ctx = (RuleContext)data;
Namespace group = (Namespace)nameSpaces.peek();
group.addTable();
RuleContext ctx = (RuleContext)data;
super.visit(node, ctx);
reportUnusedLocals(ctx, group.peek());
group.removeTable();

View File

@ -0,0 +1,12 @@
public class Unused11 {
public void foo() {
String x = "hi";
class Bar {
public void buz() {
String x = "howdy";
}
}
}
}

View File

@ -0,0 +1,7 @@
public class Unused12 {
public void foo() {
for (int x = 0; ; ) { // USED
x++;
}
}
}

View File

@ -0,0 +1,11 @@
public class Unused13 {
public void foo() {
final String x = "hi";
new Runnable() {
public void run() {
x.toString();
}
};
}
}