Issue 71543
Summary [FR] clang tidy check for static_cast of an enum class type
Labels clang
Assignees
Reporter FalcoGer
    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;
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to