1 .. title:: clang-tidy - readability-simplify-boolean-expr
3 readability-simplify-boolean-expr
4 =================================
6 Looks for boolean expressions involving boolean constants and simplifies
7 them to use the appropriate boolean expression directly. Simplifies
8 boolean expressions by application of DeMorgan's Theorem.
12 =========================================== ================
13 Initial expression Result
14 ------------------------------------------- ----------------
15 ``if (b == true)`` ``if (b)``
16 ``if (b == false)`` ``if (!b)``
17 ``if (b && true)`` ``if (b)``
18 ``if (b && false)`` ``if (false)``
19 ``if (b || true)`` ``if (true)``
20 ``if (b || false)`` ``if (b)``
21 ``e ? true : false`` ``e``
22 ``e ? false : true`` ``!e``
23 ``if (true) t(); else f();`` ``t();``
24 ``if (false) t(); else f();`` ``f();``
25 ``if (e) return true; else return false;`` ``return e;``
26 ``if (e) return false; else return true;`` ``return !e;``
27 ``if (e) b = true; else b = false;`` ``b = e;``
28 ``if (e) b = false; else b = true;`` ``b = !e;``
29 ``if (e) return true; return false;`` ``return e;``
30 ``if (e) return false; return true;`` ``return !e;``
31 ``!(!a || b)`` ``a && !b``
32 ``!(a || !b)`` ``!a && b``
33 ``!(!a || !b)`` ``a && b``
34 ``!(!a && b)`` ``a || !b``
35 ``!(a && !b)`` ``!a || b``
36 ``!(!a && !b)`` ``a || b``
37 =========================================== ================
39 The resulting expression ``e`` is modified as follows:
40 1. Unnecessary parentheses around the expression are removed.
41 2. Negated applications of ``!`` are eliminated.
42 3. Negated applications of comparison operators are changed to use the
44 4. Implicit conversions of pointers, including pointers to members, to
45 ``bool`` are replaced with explicit comparisons to ``nullptr`` in C++11
46 or ``NULL`` in C++98/03.
47 5. Implicit casts to ``bool`` are replaced with explicit casts to ``bool``.
48 6. Object expressions with ``explicit operator bool`` conversion operators
49 are replaced with explicit casts to ``bool``.
50 7. Implicit conversions of integral types to ``bool`` are replaced with
51 explicit comparisons to ``0``.
54 1. The ternary assignment ``bool b = (i < 0) ? true : false;`` has redundant
55 parentheses and becomes ``bool b = i < 0;``.
57 2. The conditional return ``if (!b) return false; return true;`` has an
58 implied double negation and becomes ``return b;``.
60 3. The conditional return ``if (i < 0) return false; return true;`` becomes
63 The conditional return ``if (i != 0) return false; return true;`` becomes
66 4. The conditional return ``if (p) return true; return false;`` has an
67 implicit conversion of a pointer to ``bool`` and becomes
68 ``return p != nullptr;``.
70 The ternary assignment ``bool b = (i & 1) ? true : false;`` has an
71 implicit conversion of ``i & 1`` to ``bool`` and becomes
72 ``bool b = (i & 1) != 0;``.
74 5. The conditional return ``if (i & 1) return true; else return false;`` has
75 an implicit conversion of an integer quantity ``i & 1`` to ``bool`` and
76 becomes ``return (i & 1) != 0;``
78 6. Given ``struct X { explicit operator bool(); };``, and an instance ``x`` of
79 ``struct X``, the conditional return ``if (x) return true; return false;``
80 becomes ``return static_cast<bool>(x);``
85 .. option:: ChainedConditionalReturn
87 If `true`, conditional boolean return statements at the end of an
88 ``if/else if`` chain will be transformed. Default is `false`.
90 .. option:: ChainedConditionalAssignment
92 If `true`, conditional boolean assignments at the end of an ``if/else
93 if`` chain will be transformed. Default is `false`.
95 .. option:: SimplifyDeMorgan
97 If `true`, DeMorgan's Theorem will be applied to simplify negated
98 conjunctions and disjunctions. Default is `true`.
100 .. option:: SimplifyDeMorganRelaxed
102 If `true`, :option:`SimplifyDeMorgan` will also transform negated
103 conjunctions and disjunctions where there is no negation on either operand.
104 This option has no effect if :option:`SimplifyDeMorgan` is `false`.
114 Would be transformed to: