Skip to content

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

@e-kwsm

Description

@e-kwsm
// 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:

$ 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:

--- 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<int *>( )

https://godbolt.org/z/6bW1hY981

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions