google / error-prone

Catch common Java mistakes as compile-time errors

Home Page:https://errorprone.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Confusing message from AlreadyChecked

PhilippWendler opened this issue · comments

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:

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?

Thanks, this is really useful feedback. I agree the finding description should be improved: it'd be great if we could work out an appropriate about of context to say "this is equivalent to true == b()", though that may be hard.

The overlap with RedundantCondition was entirely accidental. :)