File: AlreadyChecked.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (28 lines) | stat: -rw-r--r-- 581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Checking a provably-identical condition twice can be a sign of a logic error.

For example:

```java
public Optional<T> first(Optional<T> a, Optional<T> b) {
  if (a.isPresent()) {
    return a;
  } else if (a.isPresent()) { // Oops--should be checking `b`.
    return b;
  } else {
    return Optional.empty();
  }
}
```

It can also be a sign of redundancy, which can just be removed.

```java
public void act() {
  if (enabled) {
    frobnicate();
  } else if (!enabled) { // !enabled is guaranteed to be true here, so the check can be removed
    doSomethingElse();
  }
}

```