google / google/error-prone

`DefaultCharset` false negative in method-reference

Open
#5,760 0 comments 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)

### Check name

DefaultCharset

### Description

Both forms create an `InputStreamReader` without specifying a charset, so both should trigger `DefaultCharset`. However, Error Prone reports the lambda form but misses the equivalent constructor method reference `InputStreamReader::new`. Since both forms call the same constructor, the method-reference case is a false negative.

### Reproducer

```java
package demo;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Optional;

public class DefaultEncodingUsageCheck {

public void streamUsage(String resourcePath) throws IOException {
try (var is = DefaultEncodingUsageCheck.class.getResourceAsStream(resourcePath)) {

// BEFORE — NOT flagged by DefaultCharset (false negative)
Optional.ofNullable(is)
.map(InputStreamReader::new)
.map(reader -> reader.hashCode() + 1);

// AFTER — flagged by DefaultCharset
Optional.ofNullable(is)
.map(is2 -> new InputStreamReader(is2))
.map(reader -> reader.hashCode() + 1);
}
}
}
```

### Actual behavior

`DefaultCharset` fires only on the lambda form; the method reference form is not reported.

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.