Issue 87128
Summary Should the -Wformat scoped enum suggestion suggest std::to_underlying?
Labels new issue
Assignees
Reporter porglezomp
    Currently, `-Wformat` with a scoped enum suggests inserting a `static_cast` to the current underlying type.
e.g.:
```c++
#include <cstdio>

enum class Example { A };
void example() {
    printf("%d\n", Example::A);
}
```
this gives a warning:
```
<source>:5:20: warning: format specifies type 'int' but the argument has type 'Example' [-Wformat]
    5 |     printf("%d\n", Example::A);
      | ~~     ^~~~~~~~~~
      |                    static_cast<int>( )
1 warning generated.
```

If we make this change and the underlying type of the enum changes, we don't get any diagnostic that we need to go update those format sites.

In C++23 mode, we could suggest `std::to_underlying()` from `<utility>` to get an always compatible type.

There's the further question of if we should recommend `std::underlying_type_t<_>` for pre-C++23 code, to make the warning less fragile.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to