llvm / llvm/llvm-project

[clang-tidy]: cppcoreguidelines-pro-type-reinterpret-cast should not warn about casting `signed char *` <-> `unsigned char *` <-> `std::byte *`

Open
#222,079 1 comment 0 reactions 0 assignees View on GitHub
clang-tidy enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

If I get `unsigned char *` from one external C library and want to pass to C++ library that accept
`signed char *`, I have to use reinterpret_cast, according to "ES.49: If you must use a cast, use a named cast" according to "C++ core guidelines", but clang-tidy gives warning for such code:

```c++
#include

size_t alloc_buf(unsigned char **datap);
void qt_write(const char *output, size_t len);

void f()
{
unsigned char *data = nullptr;
size_t nbytes = alloc_buf(&data);
//...
qt_write(reinterpret_cast(data), nbytes);
}
```

```
:11:13: warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]
11 | qt_write(reinterpret_cast(data), nbytes);
| ^
```

https://godbolt.org/z/8MTM4MYT4

but it is impossible avoid `reinterpret_cast` here, and actually "C++ core guidelines" contains example
for similar case with `reinterpret_cast`, as "good" example, because of it is safe to
cast between `signed char *` <-> `unsigned char *` <-> `std::byte *`.

So I suppose it would be nice, if clang-tidy do not warn in such cases.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the warning in the clang-tidy cppcoreguidelines-pro-type-reinterpret-cast check using the signed char*, unsigned char*, and std::byte* cases described in the issue. Trace how that check classifies reinterpret_cast expressions, then verify that the requested character-byte pointer conversions no longer warn while other casts retain their diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.