Skip to content

Commit e4ba5ce

Browse files
authored
Merge pull request #787 from hmakholm/pr/fix-useless-parameter
Fix false positives in java/unused parameter
2 parents fb8d0f5 + b8a0346 commit e4ba5ce

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)