================
@@ -0,0 +1,113 @@
+```{title} clang-tidy - modernize-use-to-underlying
+```
+
+# modernize-use-to-underlying
+
+Finds casts from a scoped enumeration (`enum class`) to an integer type and
+replaces them with a call to `std::to_underlying` (introduced in C++23).
+
+Converting a scoped enumeration to a hard-coded integer type is error-prone: if
+the enumeration's underlying type is later changed, every such cast silently
+becomes a narrowing, widening or sign-changing conversion. `std::to_underlying`
+always yields exactly the underlying type and keeps the code correct.
+
+Example:
+
+```cpp
+enum class Color : unsigned char { Red, Green, Blue };
+
+void f(Color c) {
+  // Before:
+  auto value = static_cast<unsigned char>(c);
+  // After:
+  auto value = std::to_underlying(c);
+}
+```
+
+The check matches `static_cast`, C-style casts and functional-style casts.
+
+## Precise and imprecise casts
+
+A cast is *precise* when its destination type is exactly the underlying type of
+the enumeration. A cast is *imprecise* when the destination type is an integer
+type other than the underlying type (a different width or signedness).
+
+
+```cpp
+enum class E : int {};
+
+// precise cast
+int i = static_cast<int>(E{});
+// imprecise cast
+unsigned j = static_cast<unsigned>(E{});
+```
+
+Precise casts are always rewritten.
+
+Imprecise casts perform an additional integer conversion, so there is no
+single correct rewrite. Rewrites of imprecise casts are controlled by the
+{option}`ImpreciseCasts` option.
+
+A cast to a non-integer type (floating point, pointer, another enumeration) is
+never flagged. A cast to `bool` is only flagged when `bool` is the exact
+underlying type of the enumeration; otherwise it is treated as a truthiness
+test and left untouched.
+
+## Options
+
+````{option} ImpreciseCasts
+
+Controls how imprecise casts (whose destination type differs from the
+underlying type) are handled. Precise casts are always diagnosed and fixed
+regardless of this option. Possible values:
+
+- `Ignore`: Do not diagnose imprecise casts.
+
+- `Warn` *(default)*: Diagnose imprecise casts but do not offer a fix-it.
----------------
vbvictor wrote:

Default value should be written in the end `Default it ...`.
It's esasier for users to always know where to find default value.

https://github.com/llvm/llvm-project/pull/210459
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to