126 lines · plain
1.. title:: clang-tidy - readability-simplify-boolean-expr2 3readability-simplify-boolean-expr4=================================5 6Looks for boolean expressions involving boolean constants and simplifies7them to use the appropriate boolean expression directly. Simplifies8boolean expressions by application of DeMorgan's Theorem.9 10Examples:11 12=========================================== ================13Initial expression Result14------------------------------------------- ----------------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=========================================== ================38 39The 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 the43 opposite condition.44 4. Implicit conversions of pointers, including pointers to members, to45 ``bool`` are replaced with explicit comparisons to ``nullptr`` in C++1146 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 operators49 are replaced with explicit casts to ``bool``.50 7. Implicit conversions of integral types to ``bool`` are replaced with51 explicit comparisons to ``0``.52 53Examples:54 1. The ternary assignment ``bool b = (i < 0) ? true : false;`` has redundant55 parentheses and becomes ``bool b = i < 0;``.56 57 2. The conditional return ``if (!b) return false; return true;`` has an58 implied double negation and becomes ``return b;``.59 60 3. The conditional return ``if (i < 0) return false; return true;`` becomes61 ``return i >= 0;``.62 63 The conditional return ``if (i != 0) return false; return true;`` becomes64 ``return i == 0;``.65 66 4. The conditional return ``if (p) return true; return false;`` has an67 implicit conversion of a pointer to ``bool`` and becomes68 ``return p != nullptr;``.69 70 The ternary assignment ``bool b = (i & 1) ? true : false;`` has an71 implicit conversion of ``i & 1`` to ``bool`` and becomes72 ``bool b = (i & 1) != 0;``.73 74 5. The conditional return ``if (i & 1) return true; else return false;`` has75 an implicit conversion of an integer quantity ``i & 1`` to ``bool`` and76 becomes ``return (i & 1) != 0;``77 78 6. Given ``struct X { explicit operator bool(); };``, and an instance ``x``79 of ``struct X``, the conditional return80 ``if (x) return true; return false;``81 becomes ``return static_cast<bool>(x);``82 83Options84-------85 86.. option:: IgnoreMacros87 88 If `true`, ignore boolean expressions originating from expanded macros.89 Default is `false`.90 91.. option:: ChainedConditionalReturn92 93 If `true`, conditional boolean return statements at the end of an94 ``if/else if`` chain will be transformed. Default is `false`.95 96.. option:: ChainedConditionalAssignment97 98 If `true`, conditional boolean assignments at the end of an ``if/else99 if`` chain will be transformed. Default is `false`.100 101.. option:: SimplifyDeMorgan102 103 If `true`, DeMorgan's Theorem will be applied to simplify negated104 conjunctions and disjunctions. Default is `true`.105 106.. option:: SimplifyDeMorganRelaxed107 108 If `true`, :option:`SimplifyDeMorgan` will also transform negated109 conjunctions and disjunctions where there is no negation on either operand.110 This option has no effect if :option:`SimplifyDeMorgan` is `false`.111 Default is `false`.112 113 When Enabled:114 115 .. code-block::116 117 bool X = !(A && B)118 bool Y = !(A || B)119 120 Would be transformed to:121 122 .. code-block::123 124 bool X = !A || !B125 bool Y = !A && !B126