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:
- ReturnMissingNullable should ignore all methods marked
@DoNotCall or if the method implements a @DoNotCall method from an interface.
- 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.
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
@Nullablein 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:
ReturnMissingNullable from Error Prone 2.11.0 warns about all 3 methods:
Concrete suggestions:
@DoNotCallor if the method implements a@DoNotCallmethod from an interface.throwshould be possible, I guess.