Skip to content

Address some minor Error Prone warnings.#2842

Merged
eamonnmcmanus merged 3 commits intogoogle:mainfrom
eamonnmcmanus:warnings
Jul 14, 2025
Merged

Address some minor Error Prone warnings.#2842
eamonnmcmanus merged 3 commits intogoogle:mainfrom
eamonnmcmanus:warnings

Conversation

@eamonnmcmanus
Copy link
Copy Markdown
Member

No description provided.

@Override
public Set<Entry<K, V>> entrySet() {
return Set.of();
return Collections.emptySet();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, which Error Prone bug pattern reported this? And what is the issue with Set.of() here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally, at least, we prefer for people to use ImmutableSet.of(...) instead of Set.of(...). The reasoning is that the spec List.of(...) doesn't have guarantees about what the returned list is like. (Is it mutable? thread safe?) I've personally never been very convinced by that argument, but in this case it's easy enough to use Collections.emptySet() to shut the warning up. If considerably more verbose. Of course we can't use ImmutableSet.of() here because we don't depend on Guava.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set.of only works on Java 9+.
By the way, I viewed the code of OpenJDK 17:

    @SafeVarargs
    @SuppressWarnings("varargs")
    static <E> Set<E> of(E... elements) {
        switch (elements.length) { // implicit null check of elements
            case 0:
                @SuppressWarnings("unchecked")
                var set = (Set<E>) ImmutableCollections.EMPTY_SET;
                return set;
            case 1:
                return new ImmutableCollections.Set12<>(elements[0]);
            case 2:
                return new ImmutableCollections.Set12<>(elements[0], elements[1]);
            default:
                return new ImmutableCollections.SetN<>(elements);
        }
    }

And Set.of will always return an immutable set (also thread-safe because we only read from it)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only run tests on JDK ≥11, so the fact that this test currently uses Set.of() is not an issue for JDK-compatibility reasons.

I'm realizing that the explanation I gave wasn't right. I was thinking of the explanation for avoiding Stream.collect(toSet()) and the like. The set returned by Set.of is explicitly specified to be unmodifiable, which basically means immutable here. And therefore it is thread-safe too. The reason for preferring ImmutableSet.of() is mostly that it reflects the immutability explicitly in the type. I'm not really convinced by that argument either, and it applies just as well to Collections.emptySet(). But there is another thing that I am convinced by, and that is that Set.of().contains(null) throws NullPointerException. I think that is a giant wart which is a legitimate reason to avoid Set.of and the like. Collections.emptySet() doesn't do that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Collections.EMPTY_SET and ImmutableCollections.EMPTY_SET follow different design patterns. All immutable collections introduced from Java 9 don't allow null elements (or K/V for maps), so I approve to use Collections.emptySet here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not allowing null elements is fine. Throwing with contains(null) is not fine. It doesn't make a difference in this test context, but it's enough to make me not want to use Set.of despite its convenience.)

@Marcono1234
Copy link
Copy Markdown
Contributor

@eamonnmcmanus do you want to merge these changes? Or is there a reason why you have waited with it?

@eamonnmcmanus
Copy link
Copy Markdown
Member Author

[Google context: cl/745573037.]

@eamonnmcmanus eamonnmcmanus merged commit 81d15f5 into google:main Jul 14, 2025
11 checks passed
@eamonnmcmanus eamonnmcmanus deleted the warnings branch July 14, 2025 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants