| Issue |
167392
|
| Summary |
[clang:frontend] Feature request: warn when converting `bool` to a character type
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
eisenwave
|
Hey, I recently presented https://github.com/cplusplus/papers/issues/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:
```cpp
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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs