google / google/error-prone

Confusing message from AlreadyChecked

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

Description

In our code base, the new check AlreadyChecked in Error Prone 2.11.0 reported a finding that I misinterpreted at first. Our code looked like this:
```java
public class Test {

public static void main(String[] args) {
boolean b = a();

if (b) {
if (b == b()) {
throw new AssertionError("never reached in this example");
}
}
}

static boolean a() {
return true;
}

static boolean b() {
return false;
}
}
```
The Error Prone output for this is:

[javac] Test.java:15: warning: [AlreadyChecked] This condition (on b) is already known to be true; it (or its complement) has already been checked.
[javac] if (b == b()) {}
[javac] ^
[javac] (see https://errorprone.info/bugpattern/AlreadyChecked)

I understood the wording `This condition (on b)` in the message such that Error Prone claims that the condition `b == b()` is known to be true, which of course would be wrong. Only while starting to write an issue about the alleged wrong result I got the idea that Error Prone probably means that the condition `b` is already known to be true.

So I would suggest to change the message to use the wording `The condition "b" is already known to be true`. A suggested fix like `Did you mean "if (b()) {}"` would also help.

Side note: If I enable all Error Prone checks, I get this additional result from RedundantCondition:

[javac] Test.java:7: warning: [RedundantCondition] Redundant usage of a boolean expression [b] that is known to be `true`
[javac] if (b == b()) {
[javac] ^
[javac] (see https://errorprone.info/bugpattern/RedundantCondition)

Here the message is better and clearly indicates that `b` is known to be true. However, the arrow actually points to the opening parenthesis, which would refer to the full condition `b == b()`, instead of correctly to `b` like the message for AlreadyChecked. Furthermore, I wonder why there are two so similar checks?

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.