File: simplify-boolean-expr.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (119 lines) | stat: -rw-r--r-- 4,835 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
.. title:: clang-tidy - readability-simplify-boolean-expr

readability-simplify-boolean-expr
=================================

Looks for boolean expressions involving boolean constants and simplifies
them to use the appropriate boolean expression directly.  Simplifies
boolean expressions by application of DeMorgan's Theorem.

Examples:

===========================================  ================
Initial expression                           Result
-------------------------------------------  ----------------
``if (b == true)``                             ``if (b)``
``if (b == false)``                            ``if (!b)``
``if (b && true)``                             ``if (b)``
``if (b && false)``                            ``if (false)``
``if (b || true)``                             ``if (true)``
``if (b || false)``                            ``if (b)``
``e ? true : false``                           ``e``
``e ? false : true``                           ``!e``
``if (true) t(); else f();``                   ``t();``
``if (false) t(); else f();``                  ``f();``
``if (e) return true; else return false;``     ``return e;``
``if (e) return false; else return true;``     ``return !e;``
``if (e) b = true; else b = false;``           ``b = e;``
``if (e) b = false; else b = true;``           ``b = !e;``
``if (e) return true; return false;``          ``return e;``
``if (e) return false; return true;``          ``return !e;``
``!(!a || b)``                                 ``a && !b``
``!(a || !b)``                                 ``!a && b``
``!(!a || !b)``                                ``a && b``
``!(!a && b)``                                 ``a || !b``
``!(a && !b)``                                 ``!a || b``
``!(!a && !b)``                                ``a || b``
===========================================  ================

The resulting expression ``e`` is modified as follows:
  1. Unnecessary parentheses around the expression are removed.
  2. Negated applications of ``!`` are eliminated.
  3. Negated applications of comparison operators are changed to use the
     opposite condition.
  4. Implicit conversions of pointers, including pointers to members, to
     ``bool`` are replaced with explicit comparisons to ``nullptr`` in C++11
     or ``NULL`` in C++98/03.
  5. Implicit casts to ``bool`` are replaced with explicit casts to ``bool``.
  6. Object expressions with ``explicit operator bool`` conversion operators
     are replaced with explicit casts to ``bool``.
  7. Implicit conversions of integral types to ``bool`` are replaced with
     explicit comparisons to ``0``.

Examples:
  1. The ternary assignment ``bool b = (i < 0) ? true : false;`` has redundant
     parentheses and becomes ``bool b = i < 0;``.

  2. The conditional return ``if (!b) return false; return true;`` has an
     implied double negation and becomes ``return b;``.

  3. The conditional return ``if (i < 0) return false; return true;`` becomes
     ``return i >= 0;``.

     The conditional return ``if (i != 0) return false; return true;`` becomes
     ``return i == 0;``.

  4. The conditional return ``if (p) return true; return false;`` has an
     implicit conversion of a pointer to ``bool`` and becomes
     ``return p != nullptr;``.

     The ternary assignment ``bool b = (i & 1) ? true : false;`` has an
     implicit conversion of ``i & 1`` to ``bool`` and becomes
     ``bool b = (i & 1) != 0;``.

  5. The conditional return ``if (i & 1) return true; else return false;`` has
     an implicit conversion of an integer quantity ``i & 1`` to ``bool`` and
     becomes ``return (i & 1) != 0;``

  6. Given ``struct X { explicit operator bool(); };``, and an instance ``x`` of
     ``struct X``, the conditional return ``if (x) return true; return false;``
     becomes ``return static_cast<bool>(x);``

Options
-------

.. option:: ChainedConditionalReturn

   If `true`, conditional boolean return statements at the end of an
   ``if/else if`` chain will be transformed. Default is `false`.

.. option:: ChainedConditionalAssignment

   If `true`, conditional boolean assignments at the end of an ``if/else
   if`` chain will be transformed. Default is `false`.

.. option:: SimplifyDeMorgan

   If `true`, DeMorgan's Theorem will be applied to simplify negated
   conjunctions and disjunctions.  Default is `true`.

.. option:: SimplifyDeMorganRelaxed

   If `true`, :option:`SimplifyDeMorgan` will also transform negated 
   conjunctions and disjunctions where there is no negation on either operand. 
   This option has no effect if :option:`SimplifyDeMorgan` is `false`.
   Default is `false`.

   When Enabled:

   .. code-block::

      bool X = !(A && B)
      bool Y = !(A || B)

   Would be transformed to:

   .. code-block::

      bool X = !A || !B
      bool Y = !A && !B