Skip to content

Commit 6c657c7

Browse files
committed
bool/int
1 parent 86109d9 commit 6c657c7

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

‎man/checkers/bitwiseOnBoolean.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# bitwiseOnBoolean
22

33
**Message**: Boolean expression 'x' is used in bitwise operation. Did you mean '&&'?<br/>
4-
**Category**: Code Quality<br/>
4+
**Category**: Readability<br/>
55
**Severity**: Style (Inconclusive)<br/>
66
**Language**: C/C++ (also applies to C's `_Bool`)
77

@@ -11,9 +11,20 @@
1111

1212
## Motivation
1313

14-
`&`/`|` and `&&`/`||` look similar but behave very differently: the bitwise operators always evaluate
15-
both sides (no short-circuiting) and, for non-`bool` operands, work bit-by-bit rather than on the
16-
boolean truth value - so a stray single `&`/`|` where `&&`/`||` was meant can silently change behaviour.
14+
This checker is about readability. Readability is subjective - opinions differ about what is more
15+
readable. Please follow your own opinion.
16+
17+
When both operands are `bool`, `&`/`|` on their `0`/`1` representation happens to produce the same
18+
truth value as `&&`/`||`, so this code usually still works correctly today. The main reason to flag it
19+
anyway is common practice: `&&`/`||` is the conventional, unambiguous way to write boolean logic in
20+
C/C++, while `&`/`|` is understood to mean bitwise work - so a stray single `&`/`|` reads as a likely
21+
typo even when it happens to be harmless. There is also one real behavioural difference: `&`/`|` always
22+
evaluates both operands, so if the other side has a side effect, using `&`/`|` instead of `&&`/`||`
23+
changes whether that side effect happens.
24+
25+
When the *other* operand isn't itself boolean (e.g. an integer flag or count), `&`/`|` combines the
26+
boolean's `0`/`1` value with it bit-by-bit, which generally is **not** the same truth value `&&`/`||`
27+
would produce - in that case this points at an actual logic bug, not just a style preference.
1728

1829
## How to fix
1930

‎man/checkers/comparisonOfBoolWithInvalidComparator.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ A boolean literal (`true`/`false`) is compared to something using `<`, `>`, `<=`
1111

1212
## Motivation
1313

14-
`bool` only has two values, so ordering comparisons against a literal `true`/`false` don't express
15-
anything an equality comparison (`==`/`!=`) wouldn't say more clearly - and are easy to get backwards,
16-
since `false < true` is not always the intuitive direction a reader expects.
14+
`<`, `>`, `<=` and `>=` are well-defined against a `bool` literal (`false` is `0`, `true` is `1`), so
15+
this code already compiles and evaluates correctly - there is no functional problem to fix. The reason
16+
to flag it is readability: `bool` only has two values, so an ordering comparison against `true`/`false`
17+
says nothing that `==`/`!=` wouldn't say more directly, and it forces the reader to work out which of
18+
`false`/`true` is "smaller" instead of just reading the equality check. Preferring `==`/`!=` for
19+
two-valued types is the clearer, more idiomatic style.
1720

1821
## How to fix
1922

‎man/checkers/comparisonOfFuncReturningBoolError.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ when both sides are.
1313

1414
## Motivation
1515

16-
`bool` only has two values, so ordering its result doesn't express anything `==`/`!=` wouldn't say more
17-
clearly, and is easy to get backwards since `false < true` is not always the intuitive direction a
18-
reader expects.
16+
`<`, `>`, `<=` and `>=` are well-defined for `bool` results (`false` is `0`, `true` is `1`), so this code
17+
already compiles and evaluates correctly - there is no functional problem to fix. The reason to flag it
18+
is readability: `bool` only has two values, so ordering the result of a bool-returning function says
19+
nothing that `==`/`!=` wouldn't say more directly, and it forces the reader to work out which of
20+
`false`/`true` is "smaller" instead of just reading the equality/logical check. Preferring `==`/`!=` (or
21+
plain `&&`/`!`) for two-valued results is the clearer, more idiomatic style.
1922

2023
## How to fix
2124

0 commit comments

Comments
 (0)