llvm / llvm/llvm-project

[clang] `-Wcharacter-conversion` should warn on `wchar_t` conversions

Open
#168,854 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://compiler-explorer.com/z/6zb99Y8Yj

void f(char8_t c8, char16_t c16, wchar_t cw) {
    bool a = c8 == c16;
    bool b = c8 == cw;
}

Currently, only the first line raises -Wcharacter-conversion; the second line doesn't.

However, a comparison between char8_t and wchar_t is just as nonsensical as a comparison between char8_t and char16_t, so this seems like a false negative. That is especially true when considering that wchar_t is often UTF-16 or UTF-32, depending on the platform.

Also note that when seeing the the corresponding paper P3695R2, SG16 Unicode unanimously voted to deprecate conversions between char8_t and wchar_t. We have since flipped on that decision because it is hypothetically possible to have a UTF-8 wchar_t in C2y, but the compiler doesn't really need to care about such far-fetched hypotheticals; it has platform knowledge.

CC @cor3ntin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Compiler Explorer example with Clang and compare the diagnostics for char8_t/char16_t and char8_t/wchar_t under -Wcharacter-conversion. Trace the Clang diagnostic path for the existing character-conversion warning, then add coverage for the wchar_t comparison. Done means the second comparison warns consistently with the first across relevant platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.