Skip to content

ReturnMissingNullable should ignore @DoNotCall and always throwing methods #2910

@PhilippWendler

Description

@PhilippWendler

Sometimes we have methods that we need to implement due to an interface requiring them, but we do not want to support them and make them always throw, for example when implementing immutable collections. ReturnMissingNullable wants to add @Nullable in such cases if the interface method is known to be nullable, but this is useless. It would be nice if ReturnMissingNullable would ignore such cases.

Concrete example:

public class Test {

  abstract interface MyNavigableSet<E> extends NavigableSet<E> {

    /**
     * @throws UnsupportedOperationException Always.
     * @deprecated Unsupported operation.
     */
    @Deprecated
    @DoNotCall
    @Override
    /** Always throws. */
    E pollFirst();
  }

  abstract class MySet<E> implements MyNavigableSet<E> {

    @Override
    public E pollFirst() {
      throw new UnsupportedOperationException();
    }

    @Override
    public E pollLast() {
      throw new UnsupportedOperationException();
    }
  }
}

ReturnMissingNullable from Error Prone 2.11.0 warns about all 3 methods:

    [javac] Test.java:16: warning: [ReturnMissingNullable] Method returns a definitely null value but is not annotated @Nullable
    [javac]     E pollFirst();
    [javac]       ^
    [javac]     (see https://errorprone.info/bugpattern/ReturnMissingNullable)
    [javac]   Did you mean '@Nullable @Deprecated'?
    [javac] Test.java:22: warning: [ReturnMissingNullable] Method returns a definitely null value but is not annotated @Nullable
    [javac]     public E pollFirst() {
    [javac]              ^
    [javac]     (see https://errorprone.info/bugpattern/ReturnMissingNullable)
    [javac]   Did you mean '@Nullable @Override'?
    [javac] Test.java:27: warning: [ReturnMissingNullable] Method returns a definitely null value but is not annotated @Nullable
    [javac]     public E pollLast() {
    [javac]              ^
    [javac]     (see https://errorprone.info/bugpattern/ReturnMissingNullable)
    [javac]   Did you mean '@Nullable @Override'?

Concrete suggestions:

  1. ReturnMissingNullable should ignore all methods marked @DoNotCall or if the method implements a @DoNotCall method from an interface.
  2. ReturnMissingNullable should ignore all methods that can be detected to always throw. At least cases where the method body consists of a single throw should be possible, I guess.

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