Skip to content

Unused parameter of private method taken as lambda reference incorrectly triggers warnings #9168

@nmatt

Description

@nmatt

Apache NetBeans version

Apache NetBeans 28

What happened

Example:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)hintskind:bugBug report or fixneeds:triageRequires attention from one of the committers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions