Merge branch 'pr/3265'

This commit is contained in:
Clément Fournier
2021-05-07 12:12:22 +02:00
4 changed files with 98 additions and 2 deletions

View File

@ -18,13 +18,18 @@ import net.sourceforge.pmd.lang.symboltable.Scope;
public class MethodArgumentCouldBeFinalRule extends AbstractOptimizationRule {
public MethodArgumentCouldBeFinalRule() {
addRuleChainVisit(ASTConstructorDeclaration.class);
addRuleChainVisit(ASTMethodDeclaration.class);
}
@Override
public Object visit(ASTMethodDeclaration meth, Object data) {
if (meth.isNative() || meth.isAbstract()) {
return data;
}
this.lookForViolation(meth.getScope(), data);
return super.visit(meth, data);
return data;
}
private void lookForViolation(Scope scope, Object data) {
@ -41,7 +46,7 @@ public class MethodArgumentCouldBeFinalRule extends AbstractOptimizationRule {
@Override
public Object visit(ASTConstructorDeclaration constructor, Object data) {
this.lookForViolation(constructor.getScope(), data);
return super.visit(constructor, data);
return data;
}
}

View File

@ -727,4 +727,58 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Consider also default methods in interface</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
public interface InterfaceWithDefaultMethod {
default void foo(int bar) {
for (int i=0; i < 10; i++) {
doSomethingWith(i);
i = 5; // not OK
}
}
}
]]></code>
</test-code>
<test-code>
<description>Consider also classes in interface</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<code><![CDATA[
public interface InterfaceWithClass {
class Inner {
void foo(int bar) {
for (int i=0; i < 10; i++) {
doSomethingWith(i);
i = 5; // not OK
}
}
}
}
]]></code>
</test-code>
<test-code>
<description>Consider also anonymous classes</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>7</expected-linenumbers>
<code><![CDATA[
public class ClassWithAnon {
void bar() {
ClassWithAnon anon = new ClassWithAnon() {
void foo(int bar) {
for (int i=0; i < 10; i++) {
doSomethingWith(i);
i = 5; // not OK
}
}
};
}
}
]]></code>
</test-code>
</test-data>

View File

@ -154,6 +154,42 @@ public class Foo {
public Foo(int a) {
this.field = a;
}
}
]]></code>
</test-code>
<test-code>
<description>#3265 False negative with default methods in interface</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>2,7</expected-linenumbers>
<code><![CDATA[
public interface DefaultMethodInInterface {
default String toString(Object one) {
return toString(one, one);
}
String toString(Object one, Object two);
default Object justReturn(Object o) {
return o;
}
}
]]></code>
</test-code>
<test-code>
<description>#3265 False negative with classes in interfaces</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>3,6</expected-linenumbers>
<code><![CDATA[
public interface InterfaceWithClass {
class Inner {
public Inner(Object o) {
Object a = o;
}
public Object justReturn(Object o) {
return o;
}
}
}
]]></code>
</test-code>