google / google/error-prone

`UnusedVariable` behaves inconsistently for method references and equivalent lambda expressions.

Open
#5,758 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

### 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

```java
package demo;

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

public class TypeParameterMethodRef {

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

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

private List 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.