Merge branch 'pr-1779'

This commit is contained in:
Andreas Dangel
2019-04-19 18:15:11 +02:00
5 changed files with 65 additions and 1 deletions

View File

@ -41,6 +41,7 @@ Being based on a proper Antlr grammar, CPD can:
* [#1532](https://github.com/pmd/pmd/issues/1532): \[java] NPE with incomplete auxclasspath
* [#1729](https://github.com/pmd/pmd/issues/1729): \[java] JavaRuleViolation loses information in `className` field when class has package-private access level
* java-bestpractices
* [#1190](https://github.com/pmd/pmd/issues/1190): \[java] UnusedLocalVariable/UnusedPrivateField false-positive
* [#1720](https://github.com/pmd/pmd/issues/1720): \[java] UnusedImports false positive for Javadoc link with array type
* java-codestyle
* [#1755](https://github.com/pmd/pmd/issues/1775): \[java] False negative in UnnecessaryLocalBeforeReturn when splitting statements across multiple lines

View File

@ -14,6 +14,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTResource;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
@ -87,9 +88,12 @@ public class JavaNameOccurrence implements NameOccurrence {
primaryExpression = location.jjtGetParent().jjtGetParent();
} else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent();
} else if (location.jjtGetParent() instanceof ASTResource) {
return false;
} else {
throw new RuntimeException(
"Found a NameOccurrence (" + location + ") that didn't have an ASTPrimary Expression as parent or grandparent. Parent = "
"Found a NameOccurrence (" + location + ") that didn't have an ASTPrimary Expression"
+ " as parent or grandparent nor is a concise resource. Parent = "
+ location.jjtGetParent() + " and grandparent = " + location.jjtGetParent().jjtGetParent()
+ " (location line " + location.getBeginLine() + " col " + location.getBeginColumn() + ")");
}

View File

@ -7,8 +7,11 @@ package net.sourceforge.pmd.lang.java.symboltable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTResource;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.Scope;
@ -21,6 +24,20 @@ public class OccurrenceFinder extends JavaParserVisitorAdapter {
private final Set<NameDeclaration> additionalDeclarations = new HashSet<>();
@Override
public Object visit(ASTResource node, Object data) {
// is this a concise resource reference?
if (node.jjtGetNumChildren() == 1) {
ASTName nameNode = (ASTName) node.jjtGetChild(0);
for (StringTokenizer st = new StringTokenizer(nameNode.getImage(), "."); st.hasMoreTokens();) {
JavaNameOccurrence occ = new JavaNameOccurrence(nameNode, st.nextToken());
new Search(occ).execute();
}
}
return super.visit(node, data);
}
@Override
public Object visit(ASTPrimaryExpression node, Object data) {
NameFinder nameFinder = new NameFinder(node);

View File

@ -386,6 +386,25 @@ public class Test {
double result = Math.sqrt((a) - b);
System.out.println(result);
}
}
]]></code>
</test-code>
<test-code>
<description>#1190 [java] UnusedLocalVariable/UnusedPrivateField false-positive</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.InputStream;
public class UnusedLocalVariable {
public void testSomething() {
InputStream is = new InputStream();
try (is) {
System.out.println("foo!");
}
}
}
]]></code>
</test-code>

View File

@ -597,6 +597,29 @@ public class IssueUnusedPrivateField {
String helper = "some new string"; // hidden here
System.out.println("helper = " + helper);
}
}
]]></code>
</test-code>
<test-code>
<description>#1190 [java] UnusedLocalVariable/UnusedPrivateField false-positive</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.InputStream;
public class IssueUnusedPrivateField {
private InputStream is;
public IssueUnusedPrivateField(InputStream is) {
this.is = is;
}
public void testSomething() {
try (is) {
System.out.println("foo!");
}
}
}
]]></code>
</test-code>