openrewrite / openrewrite/rewrite-static-analysis

Change mutable public collections to immutable versions

Open
#125 3 comments 0 reactions 1 assignee View on GitHub

@coiouhkc is already working on this.

Since Jun 26, 2023.

recipe
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description


public static final List<String> entries = Arrays.asList("A","B");

public static final List<String> entries = Collections.unmodifiableList(Arrays.asList("A","B"));

While List.of is also an option, it is important to note that List.of throws NPE for null access patterns (like contains) and that can be problematic.


var a = Collections.unmodifiableList(Arrays.asList("A","B"));
var b =Arrays.asList("A","B");
var c = List.of("A","B");

a.contains(null) // false
b.contains(null) // false
c.contains(null) // NPE

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.