Skip to content

Commit b8a0346

Browse files
author
Henning Makholm
committed
Fix false positives in java/unused parameter
Methods that are mentioned in a member reference expression should count as rootdefs for the unused parameter query. Such methods have to match the functional interface of the reference expression, so it is to be expected that they will sometimes have to declare parameters that they don't actually use.
1 parent 87c5872 commit b8a0346

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

java/ql/src/semmle/code/java/deadcode/DeadCode.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class RootdefCallable extends Callable {
292292
// a body that also doesn't.
293293
not hasUsefulBody(this) and
294294
not exists(Method m | hasUsefulBody(m) | m.overridesOrInstantiates+(this))
295+
or
296+
// Methods that are the target of a member reference need to implement
297+
// the exact signature of the resulting functional interface.
298+
exists(MemberRefExpr mre | mre.getReferencedCallable() = this)
295299
}
296300
}
297301

java/ql/test/query-tests/dead-code/UselessParameter/Test.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ abstract class C implements I {
4040
}
4141
}
4242

43+
interface F {
44+
void doSomething(int arg2);
45+
}
46+
4347
public class Test {
4448
// OK: external interface
4549
public static void main(String[] args) {}
@@ -54,6 +58,13 @@ public static void premain(String arg, java.lang.instrument.Instrumentation i) {
5458
public static void foo(Object bar) {
5559
throw new UnsupportedOperationException();
5660
}
61+
62+
public static F getF() {
63+
return Test::myFImplementation;
64+
}
65+
66+
// OK: mentioned in member reference
67+
private static void myFImplementation(int foo) {}
5768

5869
// OK: native method
5970
native int baz(int x);

0 commit comments

Comments
 (0)