google / error-prone

Catch common Java mistakes as compile-time errors

Home Page:https://errorprone.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FieldCanBeLocal false positive for record with constructor

fprochazka opened this issue · comments

I have the following record as an inner class

public record DatasetProductIdentifiersPair(
    Product.Id productId,
    @Nullable String identifier
)
{

    public DatasetProductIdentifiersPair(
        final Product.Id productId, 
        @Nullable final String identifier
    )
    {
        this.productId = Objects.requireNonNull(productId, "productId must not be null");
        this.identifier = identifier;
    }

}

when I Compile, error prone reports incorrectly

[WARNING] ProductRepository.java:[300,43] [FieldCanBeLocal] This field can be replaced with a local variable in the methods that use it.
    (see https://errorprone.info/bugpattern/FieldCanBeLocal)
  Did you mean ','?
[WARNING] ProductRepository.java:[301,26] [FieldCanBeLocal] This field can be replaced with a local variable in the methods that use it.
    (see https://errorprone.info/bugpattern/FieldCanBeLocal)
  Did you mean to remove this line?

If I remove the constructor, the code compiles... it also compiles when I replace the parametrized constructor with

    public DatasetProductIdentifiersPair
    {
        Objects.requireNonNull(productId, "productId must not be null");
    }

but IMHO it should also compile with my original code

I am working on it