google / google/error-prone

`@CanIgnoreReturnValue` and inheritance

Open
#682 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

Consider the following class:
```java
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import javax.annotation.CheckReturnValue;

@CheckReturnValue
public final class Container {
interface MyInterface {
@CanIgnoreReturnValue
boolean myOperation1();

@CanIgnoreReturnValue
boolean myOperation2();
}

abstract static class MyAbstractClass implements MyInterface {
@Override
public abstract boolean myOperation1();

@CanIgnoreReturnValue
public abstract boolean myOperation3();
}

static final class MySubclass extends MyAbstractClass {
@Override
public boolean myOperation1() {
/* Defined by interface, "redeclared" by superclass. */
return false;
}

@Override
public boolean myOperation2() {
/* Defined by interface, not "redeclared" by superclass. */
return false;
}

@Override
public boolean myOperation3() {
/* Defined by superclass, not declared by the interface. */
return false;
}

@CanIgnoreReturnValue
public boolean myOperation4() {
/* Not defined by any super type. */
return false;
}
}

void interfaceInvocations() {
MyInterface o = new MySubclass();
o.myOperation1();
o.myOperation2();
}

void abstractClassInvocations() {
MyAbstractClass o = new MySubclass();
o.myOperation1(); // triggers error
o.myOperation2();
o.myOperation3();
}

void subclassInvocations() {
MySubclass o = new MySubclass();
o.myOperation1(); // triggers error
o.myOperation2(); // triggers error
o.myOperation3(); // triggers error
o.myOperation4();
}
}
```

Compiling with Error Prone's default settings yields:
```
Container.java:56: error: [CheckReturnValue] Ignored return value of method that is annotated with @CheckReturnValue
o.myOperation1(); // triggers error
^
(see http://errorprone.info/bugpattern/CheckReturnValue)
Did you mean to remove this line?
Container.java:63: error: [CheckReturnValue] Ignored return value of method that is annotated with @CheckReturnValue
o.myOperation1(); // triggers error
^
(see http://errorprone.info/bugpattern/CheckReturnValue)
Did you mean to remove this line?
Container.java:64: error: [CheckReturnValue] Ignored return value of method that is annotated with @CheckReturnValue
o.myOperation2(); // triggers error
^
(see http://errorprone.info/bugpattern/CheckReturnValue)
Did you mean to remove this line?
Container.java:65: error: [CheckReturnValue] Ignored return value of method that is annotated with @CheckReturnValue
o.myOperation3(); // triggers error
^
(see http://errorprone.info/bugpattern/CheckReturnValue)
Did you mean to remove this line?
4 errors
```

As you can see, the declared type of the object on which these methods are invoked determines whether the `CheckReturnValue` error is emitted. Is this intentional? I see `@CanIgnoreReturnValue` is not `@Inherited`, so perhaps it is. But when I noticed this I found it quite surprising.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.