Merge branch 'bug-1428'
This commit is contained in:
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||||
|
*/
|
||||||
|
package net.sourceforge.pmd.lang.java.symboltable;
|
||||||
|
|
||||||
|
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
||||||
|
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
|
||||||
|
import net.sourceforge.pmd.util.UnaryFunction;
|
||||||
|
|
||||||
|
public class DeclarationFinderFunction implements UnaryFunction<NameDeclaration> {
|
||||||
|
|
||||||
|
private NameOccurrence occurrence;
|
||||||
|
private NameDeclaration decl;
|
||||||
|
|
||||||
|
public DeclarationFinderFunction(NameOccurrence occurrence) {
|
||||||
|
this.occurrence = occurrence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyTo(NameDeclaration nameDeclaration) {
|
||||||
|
if (isDeclaredBefore(nameDeclaration) && isSameName(nameDeclaration)) {
|
||||||
|
decl = nameDeclaration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isDeclaredBefore(NameDeclaration nameDeclaration) {
|
||||||
|
if (nameDeclaration.getNode() != null && occurrence.getLocation() != null) {
|
||||||
|
return nameDeclaration.getNode().getBeginLine() <=
|
||||||
|
occurrence.getLocation().getBeginLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSameName(NameDeclaration nameDeclaration) {
|
||||||
|
return occurrence.getImage().equals(nameDeclaration.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public NameDeclaration getDecl() {
|
||||||
|
return this.decl;
|
||||||
|
}
|
||||||
|
}
|
@ -53,7 +53,7 @@ public class LocalScope extends AbstractJavaScope {
|
|||||||
if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
|
if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
|
DeclarationFinderFunction finder = new DeclarationFinderFunction(occurrence);
|
||||||
Applier.apply(finder, getVariableDeclarations().keySet().iterator());
|
Applier.apply(finder, getVariableDeclarations().keySet().iterator());
|
||||||
if (finder.getDecl() != null) {
|
if (finder.getDecl() != null) {
|
||||||
result.add(finder.getDecl());
|
result.add(finder.getDecl());
|
||||||
|
@ -533,6 +533,31 @@ public class Foo {
|
|||||||
@Data
|
@Data
|
||||||
public class Foo {
|
public class Foo {
|
||||||
private String bar;
|
private String bar;
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>#1428 False positive in UnusedPrivateField when local variable hides member variable</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class IssueUnusedPrivateField {
|
||||||
|
|
||||||
|
private static Object helper; // PMD warns unused
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpClass() {
|
||||||
|
helper = new Object();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSomething() {
|
||||||
|
String str = helper.toString(); // used here
|
||||||
|
System.out.println("str = " + str);
|
||||||
|
|
||||||
|
String helper = "some new string"; // hidden here
|
||||||
|
System.out.println("helper = " + helper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
@ -14,6 +14,9 @@
|
|||||||
|
|
||||||
**Bugfixes:**
|
**Bugfixes:**
|
||||||
|
|
||||||
* [#1429](https://sourceforge.net/p/pmd/bugs/1429/): Java - Parse Error: Cast in return expression
|
* java-unusedcode/UnusedPrivateField
|
||||||
|
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable hides member variable
|
||||||
|
* General
|
||||||
|
* [#1429](https://sourceforge.net/p/pmd/bugs/1429/): Java - Parse Error: Cast in return expression
|
||||||
|
|
||||||
**API Changes:**
|
**API Changes:**
|
||||||
|
Reference in New Issue
Block a user