google / google/error-prone

Java Records should automatically be flagged as @Immutable

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

Records are frequently going to be immutable types, and I think it's kind of annoying to have to annotate them with `@Immutable` when the modifier should be enough of an indication.

I am aware that records may contain mutable types, but my idea was that the `Immutable` checker would also validate that a record does not contain those either, which should be straightforward.

I encountered this because I have an enum which contains a `Vector3`:
```java
record Vector3(double x, double y, double z) {}

enum Direction {
NORTH(new Vector3(0, 0, -1)),
EAST(new Vector3(1, 0, 0)),
SOUTH(new Vector3(0, 0, 1)),
WEST(new Vector3(-1, 0, 0));

private final Vector3 direction;

Direction(Vector3 vector) {
this.direction = vector;
}
}
```

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.