google / google/error-prone

FieldCanBeLocal false positive for record with constructor

Open
#3,002 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

I have the following record as an inner class

```java
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

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

but IMHO it should also compile with my original code

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.