diff --git a/pmd/etc/build.xml b/pmd/etc/build.xml index a0a9f49b04..1ed36cb100 100644 --- a/pmd/etc/build.xml +++ b/pmd/etc/build.xml @@ -36,7 +36,7 @@ - + diff --git a/pmd/regress/test/net/sourceforge/pmd/SymbolTableTest.java b/pmd/regress/test/net/sourceforge/pmd/SymbolTableTest.java index 308b82cd9a..49b65ee22a 100644 --- a/pmd/regress/test/net/sourceforge/pmd/SymbolTableTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/SymbolTableTest.java @@ -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); diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java index ff96ef3757..718edc067b 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedLocalVariableTest.java @@ -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()); + } } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java index 5aa1d1ddc0..05ced7c1c9 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/UnusedPrivateInstanceVariableRuleTest.java @@ -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()); diff --git a/pmd/src/net/sourceforge/pmd/PMD.java b/pmd/src/net/sourceforge/pmd/PMD.java index ac74b5ef06..28288689f4 100644 --- a/pmd/src/net/sourceforge/pmd/PMD.java +++ b/pmd/src/net/sourceforge/pmd/PMD.java @@ -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); diff --git a/pmd/src/net/sourceforge/pmd/SymbolTable.java b/pmd/src/net/sourceforge/pmd/SymbolTable.java index 1fe981fe78..b66c546b85 100644 --- a/pmd/src/net/sourceforge/pmd/SymbolTable.java +++ b/pmd/src/net/sourceforge/pmd/SymbolTable.java @@ -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); } diff --git a/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java b/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java index bc3dae40fb..50e3b235b8 100644 --- a/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/UnusedLocalVariableRule.java @@ -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(); diff --git a/pmd/test-data/Unused11.java b/pmd/test-data/Unused11.java new file mode 100644 index 0000000000..81d58356f8 --- /dev/null +++ b/pmd/test-data/Unused11.java @@ -0,0 +1,12 @@ +public class Unused11 { + + public void foo() { + String x = "hi"; + class Bar { + public void buz() { + String x = "howdy"; + } + } + + } +} diff --git a/pmd/test-data/Unused12.java b/pmd/test-data/Unused12.java new file mode 100644 index 0000000000..5ac4a7e34c --- /dev/null +++ b/pmd/test-data/Unused12.java @@ -0,0 +1,7 @@ +public class Unused12 { + public void foo() { + for (int x = 0; ; ) { // USED + x++; + } + } +} \ No newline at end of file diff --git a/pmd/test-data/Unused13.java b/pmd/test-data/Unused13.java new file mode 100644 index 0000000000..c23f1dd811 --- /dev/null +++ b/pmd/test-data/Unused13.java @@ -0,0 +1,11 @@ +public class Unused13 { + + public void foo() { + final String x = "hi"; + new Runnable() { + public void run() { + x.toString(); + } + }; + } +}