Unused parameter of private method taken as lambda reference incorrectly triggers warnings
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 935
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 17
Description
### Apache NetBeans version
Apache NetBeans 28
### What happened
Example:
```java
class Example
{
public void testA() { doIt(this::methodA); }
private void methodA(String unusedA) { System.out.println("A"); }
// warning here: ^^^^^^^
interface Interface { void method(String param); }
static void doIt(Interface impl) { impl.method("test"); }
public void testB() { doIt(this::methodB); }
void methodB(String unusedB) { System.out.println("B"); }
public void testC() { doIt(unusedC -> { System.out.println("C"); }); }
public void testD() { doIt(implD); }
private final Interface implD = unusedD -> { System.out.println("D"); };
public void testE() { doIt(implE); }
private final Interface implE = new Interface() { @Override public void method(String unusedE) { System.out.println("E"); } };
public void testF() { doIt(new Interface() { @Override public void method(String unusedF) { System.out.println("F"); } }); }
}
```
Parameter `unusedA` incorrectly shows a warning "Variable unusedA is never read".
For comparison, parameter `unusedB` correctly does _not_ show the warning, even though `methodB` isn't otherwise used in the package. Cases C to F illustrate further variations of providing an interface implementations where correctly also no warning is shown for the unused parameters.
A method reference effectively turns the referenced method definition into the implementation of an abstract method. Unused parameters in implementations of abstract methods generally do not cause a warning, because it is normal that not all implementations need all parameters. Therefore one would expect there to be no warning here either, in presence of a method reference.
### Language / Project Type / NetBeans Component
Java, Hints
### How to reproduce
See example above.
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
Windows
### JDK
JDK 21
### Apache NetBeans packaging
Apache NetBeans binary zip
### Anything else
_No response_
### Are you willing to submit a pull request?
No
Contributor guide
Research direction
Start with the NetBeans Java Hints implementation that reports unused parameters, then compare how it analyzes method references with lambdas and anonymous Interface implementations. Reproduce the supplied Example and verify that unusedA no longer receives the warning while the other cases retain their current behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100