PicnicSupermarket / error-prone-support

Error Prone extensions: extra bug checkers and a large battery of Refaster rules.
https://error-prone.picnic.tech
MIT License
188 stars 36 forks source link

Rewrite `ImmutableMap.Builder#build` to `buildOrThrow` #1223

Open EnricSala opened 2 months ago

EnricSala commented 2 months ago

Problem

An ImmutableMap.Builder has two terminal methods which accomplish the same thing: build or buildOrThrow.

The buildOrThrow method should be preferred, because the documentation of build specifies:

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method will throw an exception if there are duplicate keys. The {@code build()} method will soon be deprecated.

Description of the proposed new feature

I would like to rewrite the following code:

ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
builder.build();

to:

ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
builder.buildOrThrow();