Skip to content

UnusedVariable behaves inconsistently for method references and equivalent lambda expressions. #5758

@Emilyaxe

Description

@Emilyaxe

Error Prone version

2.49.0 (error_prone_core)

Description

UnusedVariable behaves inconsistently for method references and equivalent lambda expressions. When transform(String name) is called via this::transform, Error Prone does not report name as unused. After rewriting the call site to n -> transform(n), the same parameter is flagged. Since the method body is unchanged and name is never used, the method-reference case is a false negative.

Reproducer

package demo;

import java.util.List;
import java.util.stream.Collectors;

public class TypeParameterMethodRef<T> {

    // BEFORE: no UnusedVariable warning on `name`
    public List<List<T>> processRef(List<String> names) {
        return names.stream()
            .map(this::transform)
            .collect(Collectors.toList());
    }

    // AFTER: UnusedVariable reported on `name`
    public List<List<T>> processLambda(List<String> names) {
        return names.stream()
            .map(n -> transform(n))
            .collect(Collectors.toList());
    }

    private List<T> transform(String name) {
        throw new UnsupportedOperationException("transform not implemented");
    }
}

Actual behavior

UnusedVariable is reported only when transform is called from a lambda; the method-reference call site suppresses the warning for the same unused parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions