Skip to content

[JSpecifyExperimental] False positive dereferenced expression is @Nullable in Stream pipeline after Class::cast #1808

Closed

Description

@toellrich

Description

When running NullAway with the experimental JSpecify engine enabled (-XepOpt:NullAway:JSpecifyExperimental=true), NullAway reports a false positive [NullAway] dereferenced expression 's' is @Nullable inside a stream pipeline.

Even though Class::cast guarantees a non-null return value of that specific type, the experimental generics engine fails to propagate this non-null type argument into the subsequent stream operations, causing the next lambda parameter s to be treated as nullable.

Note: This error does not occur when JSpecifyExperimental is disabled.

Steps to Reproduce

Compile the following code snippet with NullAway and -XepOpt:NullAway:JSpecifyExperimental=true enabled:

import java.io.Serializable;
import java.util.List;

public class Tester {

  public static void main(String[] args) {
    List<Serializable> list = List.of("Hello", 100);
    List<String> stringsLongerThanThree = list.stream()
        .filter(String.class::isInstance)
        .map(String.class::cast) // Yields Stream<String>, should be non-null generic argument
        .filter(s -> s.length() > 3) // <-- Fails here only with JSpecifyExperimental active
        .toList();
    System.out.println(stringsLongerThanThree);
  }
}

Expected Behavior

The experimental JSpecify mode should properly propagate the type argument from .map(String.class::cast) as a non-null String type, meaning s in .filter(s -> ...) should be recognized as safely non-null.

Actual Behavior

The compilation fails with:

Tester.java:12: error: [NullAway] dereferenced expression 's' is @Nullable
        .filter(s -> s.length() > 3)
                      ^

Environment

  • NullAway Version: 0.14.1
  • Error Prone Version: 2.50.0
  • Java/JDK Version: 21
  • Relevant Flags: -XepOpt:NullAway:JSpecifyExperimental=true

Activity

  1. added
    jspecify
    on Sep 3, 2026
  2. msridhar commented on Sep 3, 2026

    @msridhar
    Contributor

    Yup, agreed on this one, in fact I've seen it before on other programs. Will work on getting a fix in soon-ish.

  3. self-assigned this
    on Sep 9, 2026
  4. msridhar commented on Oct 3, 2026

    @msridhar
    Contributor
  5. added a commit that references this issue on Oct 20, 2026
    6a501ec
  6. added a commit that references this issue on Oct 22, 2026
    3035507
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

false positivejspecify

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions