<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/87128>87128</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Should the -Wformat scoped enum suggestion suggest std::to_underlying? 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          porglezomp
      </td>
    </tr>
</table>

<pre>
    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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE9zuzYQ_TTyZScMloyxDxwcEs_03M706BHSGtQKiUpLXPeQz96RsOPk1_Ty8zAYsf_evreLjNH0DrFh1TOrXlZypsGHZvKht_iPH6dV5_W1aecQ0JG9Mt4C25ZPv599GCWxbQkXQwNIiMpPqAHdPEKc-x4jRTAuYiDjepApLJIko05KxhxJHmhAUEtymJ3GYK_Jm64TFqx8YeUBi75g4rAc2LZcLsX4c7qWt1wYp-ysEZhoVSRtPBOvN-OSJcFSVsYIr3_LcbIIrH6GA7D6hYlbnjdvNOBiZnzH-D45LTYAgCkYR-ds4YxXmlWty4_tPWkCKg4HxvcfSVOBr9iXIw0mQm_eMIKEiwzOuP6_fd6Ooo1-DgpTV-JQMXHgJROHT3GwCAJxQmX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