<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/71543>71543</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[FR] clang tidy check for static_cast of an enum class type
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
FalcoGer
</td>
</tr>
</table>
<pre>
When casting an `enum class` type using `static_cast<T>`, this may result in a bug if the type `T` is not correct. In c++23 we have `std::to_underlying` which should be used instead.
```c++
// Fix, must add, if not already present:
#include <utility> // Provides std::to_underlying()
// NOLINTNEXTLINE(performance-enum-size)
enum class EType : int {};
auto main() -> int
{
// NOLINTNEXTLINE(readability-magic-numbers)
EType e {42};
[[maybe_unused]]
// c++23 and later only
// Original code:
// auto i = static_cast<int>(e);
// ~~~~~~~~~~~~~~~~
// ^ bugprone: use std::to_underlying
// Fix:
auto i = std::to_underlying(e);
return 0;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0lMtu6zYQhp-G2gxsyKQu9kKLOLaLAAc5RWGg3QUUOZLYUqTBS1J10Wc_oCzEToIIgm1Yc_n4z6_h3qveIDak3JPykPEYBuuaE9fC_oYua62cmj8HNCC4D8r0wA2QKkcTRxCae0-qHMJ0QYg-PSZV7gMPSrykBMIez4QdSZUT-ghhUB5GPoFDH3UAZYBDG3tQHYQBr2VIlZ9TTeXB2ADCOocirOHJgCB0T-ieMnhDGPgrXrtJwh4Iewj2JRqJTk_K9KnC26DEAH6wUUtoEyBKUMYH5HJN8gPJH5bPKr_eS4PlX3oi9AQn9W9iH6MPwKVMv1U3o3HtkMsJLg49mpAolkSmjNBRIhD2GIPSKkyEHWGp-Luzr0qih2_Q6ZbQ3Qe-e57nnz-ens_Px7_OP56ej4RuL-g660ZuBK7SWFZe_YfvFW6DguN51pc9gDIBSL0n9YGw_X0PHoOFkStzhYBVwlYmLDH1EgwA8B1O0oS385lXI--VWJk4tuj8O1LKvrJgoijoVw6Y7bgf-dTiSzRpcqQ8pPtz_5snuJGgeUAH1ujpQ7Vb-E-nemW4BmElvk_sLmBWQAFhB_jo46QCSwectWVflfj_0_Ul4P0i5THZ_uKsSQzJmN95YVHjZsV75A-s31jpC63DEJ2B_KZ4ffj0GmSyYXLHdjzDZlPtdqyqS1pnQ1Oxom7ruth2HeasLlvE7ZZvZVnRou1QZKqhOWWbTV5vCkbzet2JXc1ojlLwIq8EJUWOI1d6rfXruLauz5T3EZt6UxYs07xF7eddRKnQPJ2AprXkmhS_amPvSZFr5YO_VQgq6HmBnf4g5QHmPAhKTiAGFP9AZ939LMF2aYndvRhp72TR6WYI4eKTjrPevQpDbNfCjoSeUrfla3Vx9m8UgdDTDO8JPc38vwIAAP__aKCP9w">