[clang-tools-extra] [clang-tidy] Add performance-bool-bitwise-operation check (PR #142324)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 16 07:20:57 PDT 2025
================
@@ -0,0 +1,48 @@
+.. title:: clang-tidy - performance-bool-bitwise-operation
+
+performance-bool-bitwise-operation
+==================================
+
+Finds potentially inefficient use of bitwise operators such as ``&``, ``|``
+and their compound analogues on Boolean values where logical operators like
+``&&`` and ``||`` would be more appropriate.
+
+Bitwise operations on Booleans can incur unnecessary performance overhead due
+to implicit integer conversions and missed short-circuit evaluation.
+
+.. code-block:: c++
+
+ bool invalid = false;
+ invalid |= x > limit.x; // warning: use logical operator '||' for boolean variable 'invalid' instead of bitwise operator '|='
+ invalid |= y > limit.y; // warning: use logical operator '||' for boolean variable 'invalid' instead of bitwise operator '|='
+ invalid |= z > limit.z; // warning: use logical operator '||' for boolean variable 'invalid' instead of bitwise operator '|='
+ if (invalid) {
+ // error handling
+ }
+
+These 3 warnings suggest to assign result of logical ``||`` operation instead
+of using ``|=`` operator:
+
+.. code-block:: c++
+
+ bool invalid = false;
+ invalid = invalid || x > limit.x;
+ invalid = invalid || y > limit.x;
+ invalid = invalid || z > limit.z;
+ if (invalid) {
+ // error handling
+ }
+
+Options
+-------
+
+.. option:: StrictMode
----------------
PiotrZSL wrote:
Actually StrictMode should be more strict (= more findings) when set to true.
Consider renaming this or invert logic.
https://github.com/llvm/llvm-project/pull/142324
More information about the cfe-commits
mailing list