llvm / llvm/llvm-project

clang-tidy: google-readability-casting, aka modernize-avoid-c-style-cast, for nullptr

Open
#173,147 3 comments 0 reactions 1 assignee Claimed by @SonareMradul View on GitHub
clang-tidy false-negative
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```cpp
// a.cpp
void f(int *) {}
void f(double *) {}

int main() {
f((int *)nullptr);
}
```

clang-tidy 21.1.6 warns the cast but does not provide fix:

```console
$ clang-tidy '--checks=-*,google-readability-casting' --fix a.cpp
1 warning generated.
/tmp/a.cpp:6:5: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [google-readability-casting]
5 | f((int *)nullptr);
| ^
```

However, `static_cast` is the valid choice, so it can be fixed:

```diff
--- current behaviour
+++ suggestion
-/tmp/a.cpp:6:5: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [google-readability-casting]
+/tmp/a.cpp:6:5: warning: C-style casts are discouraged; use static_cast [google-readability-casting]
5 | f((int *)nullptr);
- | ^
+ | ^~~~~~~
+ | static_cast( )
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.