[clang] [Clang] Implement Wpointer-bool-conversion-strict (PR #131523)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 16 20:14:57 PDT 2025


MagentaTreehouse wrote:

We should not flag [contextual conversions](https://en.cppreference.com/w/cpp/language/implicit_conversion#Contextual_conversions) to `bool`. This is supported by C++ Core Guidelines [ES.87: Don’t add redundant `==` or `!=` conditions](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es87-dont-add-redundant--or--to-conditions):

> ```c++
> // These all mean "if p is not nullptr"
> if (p) { ... }            // good
> if (p != nullptr) { ... } // redundant !=nullptr, not recommended
> ```

> ```c++
> // These all mean "if p is nullptr"
> if (!p) { ... }           // good
> if (p == nullptr) { ... } // redundant == nullptr, not recommended
> ```

It looks like we can interpret the issue request to be an implemenation of a specific case of [ES.46: Avoid lossy (narrowing, truncating) arithmetic conversions](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es46-avoid-lossy-narrowing-truncating-arithmetic-conversions):

> **Note** This rule does not apply to [contextual conversions to bool](https://en.cppreference.com/w/cpp/language/implicit_conversion#Contextual_conversions):
> ```c++
> if (ptr) do_something(*ptr);   // OK: ptr is used as a condition
> bool b = ptr;                  // bad: narrowing
> ```

(Side note: I think it is good to be a Clang-Tidy check under the `cppcoreguidelines` group)


https://github.com/llvm/llvm-project/pull/131523


More information about the cfe-commits mailing list