Skip to content

[clang:frontend] Feature request: warn when converting bool to a character type (P3765R0) #167392

@eisenwave

Description

@eisenwave

Hey, I recently presented cplusplus/papers#2384 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3765r0.html) in Kona 2025 during the WG21 meeting.

The goal of the paper was to introduce a deprecation for the implicit conversion in cases like:

std::string_view str = /* ... */;
if (str.ends_with('\n' || str.ends_with('\r'))) {
    // ...
}

This is an extremely evil bug: it's equivalent to checking whether str ends with U+0001 START OF HEADING (assuming ASCII char). See the paper for more discussion. In the ASCII/Unicode interpretation:

  • false is converted to U+0000 NULL
  • true is converted to U+0001 START OF HEADING

The committee wasn't open to having this in the standard, but it seemed like implementers were interested in adding a compiler warning that is enabled by default here. The conversions from bool to char is semantically quite nonsensical (unless char is abused a general integer type), and leads to bugs such as the one shown in the example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions