typetools / typetools/checker-framework

Avoid warning suppression when using ThreadLocal.withInitial(nonNullSupplier)

Open
#5,073 1 comment 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Jan 28, 2026.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

I am using the Gradle plugin (v 0.6.8) to invoke the CheckerFramework (v 3.21.2).

When using code of the form

private static final ThreadLocal<@NonNull Object> THING = ThreadLocal.withInitial(Object::new);

CF currently outputs the following complaint:

[type.argument] incompatible type argument for type parameter T of ThreadLocal.

	private static final ThreadLocal<@NonNull Object> THING = ThreadLocal.withInitial(Object::new);
	                                 ^
  found   : @Initialized @NonNull Object

  required: [extends @Initialized @Nullable Object super null (NullType)]

Currently, also per note in the annotated JDK, one has to suppress the warning via

@SuppressWarnings("nullness:type.argument")

ThreadLocal is annotated to disallow non-null content types by default.
I can see that ThreadLocal's might in principle store nullable values. But if I create a ThreadLocal with a supplier that guarantees to return a non-null value, this error should be unnecessary.

Would it be possible for CF to somehow be smarter about whether or not this error is actually necessary in a particular case, such as the one in this example?

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.