google / google/error-prone

== true test

Open
#132 2 comments 0 reactions 1 assignee Claimed by @eaftan View on GitHub
migrated Priority-High Type-NewCheck
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

_[Original issue](https://code.google.com/p/error-prone/issues/detail?id=132) created by **grosse@google.com** on 2013-05-15 at 04:26 PM_

---

Tests of the form (expr == true) are a potential security issue when interacting with untrusted bytecode, as the bytecode may return a boolean that is not equal to true or false. Booleans are compiled to 32bit ints, while the constants true and false are compiled to 1 and 0. Without an explicit equality test, all nonzero values are treated as true, so booleans will behave as Java programmers expect.

It might make sense to warn about both (== true) and (== false) even though the later doesn't present the same risk. People rarely write code like this, so it may indicate a programmer mistake.

Code containing these tests can be easily fixed. (var == true) is replaced with (var) while (var == false) is replaced with (!var).

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.