llvm / llvm/llvm-project

clang-tidy warns on use of C-style cast when assigning from an enum value template parameter

Open
#222,793 3 comments 0 reactions 0 assignees View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://cxx.godbolt.org/z/hMTPzW7Mq

```cpp
enum E {
Foo
};

template
constexpr E ETemplate = Value;

auto F() -> auto {
[[maybe_unused]] auto a = ETemplate;
}
```

Produces the tidy warning:
```
:6:25: warning: C-style casts are discouraged; use static_cast [google-readability-casting]
6 | constexpr E ETemplate = Value;
| ^
| static_cast<>( )
1 warning generated.
```

This was not the case in clang-19 at least, but it is in 23 and trunk.

It appears that clang is synthesizing a cast to/from the enum value and choosing to do so with a C-style case, and clang-tidy is finding that.

Contributor guide

Open the contributing guide

Research direction

Start with the Compiler Explorer reproduction at https://cxx.godbolt.org/z/hMTPzW7Mq and compare the clang-tidy warning under clang-19, clang-23, and trunk. Trace the synthesized enum conversion responsible for the C-style cast; done means the valid template assignment no longer produces the google-readability-casting warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.