• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. title:: clang-tidy - readability-simplify-boolean-expr
2
3readability-simplify-boolean-expr
4=================================
5
6Looks for boolean expressions involving boolean constants and simplifies
7them to use the appropriate boolean expression directly.
8
9Examples:
10
11===========================================  ================
12Initial expression                           Result
13-------------------------------------------  ----------------
14``if (b == true)``                             ``if (b)``
15``if (b == false)``                            ``if (!b)``
16``if (b && true)``                             ``if (b)``
17``if (b && false)``                            ``if (false)``
18``if (b || true)``                             ``if (true)``
19``if (b || false)``                            ``if (b)``
20``e ? true : false``                           ``e``
21``e ? false : true``                           ``!e``
22``if (true) t(); else f();``                   ``t();``
23``if (false) t(); else f();``                  ``f();``
24``if (e) return true; else return false;``     ``return e;``
25``if (e) return false; else return true;``     ``return !e;``
26``if (e) b = true; else b = false;``           ``b = e;``
27``if (e) b = false; else b = true;``           ``b = !e;``
28``if (e) return true; return false;``          ``return e;``
29``if (e) return false; return true;``          ``return !e;``
30===========================================  ================
31
32The resulting expression ``e`` is modified as follows:
33  1. Unnecessary parentheses around the expression are removed.
34  2. Negated applications of ``!`` are eliminated.
35  3. Negated applications of comparison operators are changed to use the
36     opposite condition.
37  4. Implicit conversions of pointers, including pointers to members, to
38     ``bool`` are replaced with explicit comparisons to ``nullptr`` in C++11
39     or ``NULL`` in C++98/03.
40  5. Implicit casts to ``bool`` are replaced with explicit casts to ``bool``.
41  6. Object expressions with ``explicit operator bool`` conversion operators
42     are replaced with explicit casts to ``bool``.
43  7. Implicit conversions of integral types to ``bool`` are replaced with
44     explicit comparisons to ``0``.
45
46Examples:
47  1. The ternary assignment ``bool b = (i < 0) ? true : false;`` has redundant
48     parentheses and becomes ``bool b = i < 0;``.
49
50  2. The conditional return ``if (!b) return false; return true;`` has an
51     implied double negation and becomes ``return b;``.
52
53  3. The conditional return ``if (i < 0) return false; return true;`` becomes
54     ``return i >= 0;``.
55
56     The conditional return ``if (i != 0) return false; return true;`` becomes
57     ``return i == 0;``.
58
59  4. The conditional return ``if (p) return true; return false;`` has an
60     implicit conversion of a pointer to ``bool`` and becomes
61     ``return p != nullptr;``.
62
63     The ternary assignment ``bool b = (i & 1) ? true : false;`` has an
64     implicit conversion of ``i & 1`` to ``bool`` and becomes
65     ``bool b = (i & 1) != 0;``.
66
67  5. The conditional return ``if (i & 1) return true; else return false;`` has
68     an implicit conversion of an integer quantity ``i & 1`` to ``bool`` and
69     becomes ``return (i & 1) != 0;``
70
71  6. Given ``struct X { explicit operator bool(); };``, and an instance ``x`` of
72     ``struct X``, the conditional return ``if (x) return true; return false;``
73     becomes ``return static_cast<bool>(x);``
74
75Options
76-------
77
78.. option:: ChainedConditionalReturn
79
80   If `true`, conditional boolean return statements at the end of an
81   ``if/else if`` chain will be transformed. Default is `false`.
82
83.. option:: ChainedConditionalAssignment
84
85   If `true`, conditional boolean assignments at the end of an ``if/else
86   if`` chain will be transformed. Default is `false`.
87