Skip to content

Feature request: detect unnecessary Objects.requireNonNull #2050

Closed

Description

@estekhin

Inspired by
https://github.com/uber/NullAway/wiki/Configuration#jspecify-jdk-models,
#1616 and #1813,
and https://github.com/uber/NullAway/tree/master/scripts/nullaway-suppression-remover.

Suppose that we use 0.14+ up to 0.14.2 with -XepOpt:NullAway:JSpecifyJDKModels=true.
In order to pass all checks we have to either suppress code that contains Optional.orElseGet or wrap the call into Objects.requireNonNull(optional.orElseGet(() -> guaranteed non-null return)).

With 0.14.3 at least some such cases are handled "as expected", and we could remove unnecessary suppressions and requireNonNull-s.

nullaway-suppression-remover helps with finding and removing the unnecessary suppressions.

It would be good if there was something similar that would help with finding unnecessary requireNonNull calls.
Best case if it is an additional error-prone check or null-away option, so it could be enforced automatically.

Activity

  1. msridhar commented on Oct 23, 2026

    @msridhar
    Contributor

    Could you try setting requireNonNull as the CastToNonNullMethod:

    https://github.com/uber/NullAway/wiki/Configuration#specify-casttononnull-method

    Possibly you'll then get warnings in the places you want?

  2. estekhin commented on Oct 24, 2026

    @estekhin
    Author

    Somehow i was under impression that CastToNonNullMethod has some default value and already does something.

    Adding -XepOpt:NullAway:CastToNonNullMethod=java.util.Objects.requireNonNull does exactly what i want - produces a compilation failure with a message like [NullAway] passing known @NonNull parameter 'xxx' to CastToNonNullMethod.

    Thank you for pointing out this exsiting option!

  3. msridhar commented on Oct 24, 2026

    @msridhar
    Contributor

    Great, so glad it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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