Skip to content

DefaultCharset false negative in method-reference #5760

@Emilyaxe

Description

@Emilyaxe

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

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.

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