Skip to content

Feature Request: Use an annotation like @MonotonicNonNull for better reasoning about field nullability inside lambdas #1148

@agrieve

Description

@agrieve

Similar to #570, but not stream-specific.

We've hit this scenario 3 or 4 times in chrome so far:

import org.jspecify.annotations.Nullable;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class Foo {
  @Nullable Object mThing;

  void method(Object a, @Nullable Object b) {
    mThing = new Object();
    Runnable r = () -> mThing.hashCode();
    r.run();
  }
}
Foo.java:10: warning: [NullAway] dereferenced expression mThing is @Nullable
    Runnable r = () -> mThing.hashCode();
                             ^
    (see http://t.uber.com/nullaway )
  • When the field in question starts as null, but has only non-null assignments, then it is safe to assume it is non-null for dereferences that occur after an assignment (since it can never return to being null).
  • For lambdas that are constructed after an assignment, we should not warn about dereferences made by lambdas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions