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

    <tr>
        <th>Summary</th>
        <td>
            Add -Wenum-conversion that works also on non-constexpr contexts
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Clang recently added the error `-Wenum-constexpr-conversion` (downgradable to warning). This detects cases where casting an integer outside the valid range of an enum into said enum is UB. The compiler is required to diagnose this in constant expressions, where UB is not allowed.

I feel this is a quite useful warning that would also be good to have in regular, non-constexpr code (in that case, classified as an opt-in warning). Since the majority of the implementation for detecting this is already in place, it seems to me it should be fairly doable to add such a warning for the general case.

Example:

```cpp
enum Foo
{
  A=0,
  B,
};

constexpr Foo x = static_cast<Foo>(123);  // Hard error, UB (-Wenum-constexpr-conversion)

int main()
{
   Foo y = static_cast<Foo>(123);  // Warning (-Wenum-conversion)
}
```

What do you think, would this be feasible?

Thanks!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVE1v4zgM_TX2hWjgj6RJDj407RSz551BjwvaYmxNFckjyUmzv35JOdmkM8ACCwSJKFLke49kWqfOzbNB24Onjmw0Z0ClSEEcCMh75yF7LB7eyE6Hh87ZEOlj9HI6kg_aWfZCVm2UO9neo8LWEEQHJ_RW2z6rtgv4NugAiiJ1MUCHgQKcBvIk58hBgBa0jdSTBzfFoBWl8kc0WoFncARuL1GCQkIdBGTXbAb4vpMinM8dRm04C995-jlpL0QcKI29dUGyskdbSETQRhAyFIRHyKrnC6zvO0lgXQQ0xp1ILbLiJSue5u8_YE9kLqkCIHCdSDAF2k_mypvdGOHkJqM4SXDQEvTOJTQDHklAeOong17qWmdv4jI6FoA15ZiURiSTqM4gQ91rJoVB5HBjfOCge63_1Lab1TvgD-d1PIt0YuvDaOjALcbIdGHPnZ17MsO9sDGeUJ0F3miwS2V1hEB0CIL9QMkcEjHmtEfteWSUu_adhwfC1A2sy1UKqSQAerLk0SQ6nxT98oGCLauf7m95sOZPN47zTWr3q3MX_3o3HwCesvqlYKhXe_fvOVu_ZPXuPu1NZs4EH8BPIYgm3V8yjln9LBXqL6x_WdWsKb8H7sYrf-ArejVvhQjDc8JR_7Ub_PquNA8ud0Xz9ebmubFIgM7_D9DbReNPOH6rziJ8lvQe1ZuMmOLKbpIxsO9pEVKD01RIlwmDbqVDr_cvvw1o33lvypya8vFx87guinqZq6ZW23qLedTRUPPEE_Ebtut--Pcw7wdf_boE_I_wEUM-edMMMY5B5iOx7nUcpnbB286GMcfrz8Po3Q8eaDZ1CBMxtNfVtliu8qGpqaW66raFonZTlav9squrdkn1imizVSo32JIJTbbaZauXXDdVUVVlWa6LdbUul4sW62pZ0mbfrbe4pWW2LIh7aRZSeOF8n_smYWinPrDT6BDDzSmL21uia36c4uB806E3LvRojvT3mCfQTUL8Dw6IzxY">