error-prone-support icon indicating copy to clipboard operation
error-prone-support copied to clipboard

CollectorMutability: Suggest immutable Guava alternative

Open rickie opened this issue 5 months ago • 0 comments

Problem

In https://github.com/checkstyle/checkstyle/pull/14229 we are enabling the CollectorMutability check in the Checkstyle repository. The Checkstyle team tries to minimize the dependencies they have on Guava. Therefore, the rewrite to Immutable{List,Map,Set}.toImmutable{List,Map,Set}() is not desirable. An alternative would be to use the Collectors.toUnmodifiable{List,Map,Set}(). This is a good alternative as this also clearly communicates that a list is used that should not be modified.

Description of the proposed new feature

  • [ ] Support a stylistic preference.
  • [x] Avoid a common gotcha, or potential problem.
  • [ ] Improve performance.

I would like to rewrite the following code:

.collect(Collectors.toSet());

to:

.collect(Collectors.toUnmodifiableSet());

Considerations

Two things:

  • As @Stephan202 notes, this should take into account that this rewrite is only valid when JDK > 9.
  • This XXX in ThirdPartyLibrary should be resolved as well.

Participation

  • [x] I am willing to submit a pull request to implement this improvement.

rickie avatar Jan 08 '24 06:01 rickie