title: How to enable C++ warnings for bitwise operators with boolean arguments
body: <p>While working with a rather large C++ code base and the GCC toolchain on Linux, I have encountered code which performs a boolean check as follows:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() {
     bool foo = true;
  
     if (~foo) {
       // do some expensive operation
       printf(&quot;This can be bad...\n&quot;);
     }
     return 0;
  }
  </code></pre>
  <p>This looks like an obvious bug, since the <code>~</code> operator represents bit-wise NOT in C++, and not logical NOT, as it would, say, in MATLAB. The above code would always evaluate to true</p>
  <p>Luckily, the issue caused by this bug was not major (it was just a small performance hit), but it got me thinking about how this bug hadn't been found in so much time.</p>
  <p>Since the bit-wise operator triggers an implicit cast from the boolean to an integer, which is a promotion, there's nothing wrong with it <em>per se</em>. However, to me it seems as though at least something like <code>clang-tidy</code> should be able to pick this up as a logical error, since it is pretty clear that in most cases, the <em>intent</em> is not to apply a bit-wise operation to a bool, but instead a logical one.</p>
  <p><code>g++</code> doesn't seem to care about this issue even with <code>-Wall -Wextra -Wconversion</code> enabled, which is sensible given that, as I mentioned before, this isn't against the standard. (I even tried <code>g++</code> 6.3, which should have a lot of new checks, and still got nothing.)</p>
  <p>Using <code>clang-tidy</code> with all checks enabled (which can get really noisy really fast) does warn about the implicit conversion itself (&quot;implicit cast bool -&gt; 'int'&quot;), but there doesn't seem to be a specific warning related to applying bit-wise operators to booleans.</p>
  <p>Writing the if-statement differently as <code>if(~foo == true)</code>, while verbose and leading to an always-false scenario, does lead to more meaningful errors which can bring the bug into attention, but this doesn't happen when the terser <code>if(~foo)</code> form is used.</p>
  <p>Are there any ways/tools to check for such issues, which are 100% correct C++, but very likely bugs?</p>