| 1 |
.. title:: clang-tidy - readability-redundant-casting |
| 2 |
|
| 3 |
readability-redundant-casting |
| 4 |
============================= |
| 5 |
|
| 6 |
Detects explicit type casting operations that involve the same source and |
| 7 |
destination types, and subsequently recommend their removal. Covers a range of |
| 8 |
explicit casting operations, including ``static_cast``, ``const_cast``, C-style |
| 9 |
casts, and ``reinterpret_cast``. Its primary objective is to enhance code |
| 10 |
readability and maintainability by eliminating unnecessary type casting. |
| 11 |
|
| 12 |
.. code-block:: c++ |
| 13 |
|
| 14 |
int value = 42; |
| 15 |
int result = static_cast<int>(value); |
| 16 |
|
| 17 |
In this example, the ``static_cast<int>(value)`` is redundant, as it performs |
| 18 |
a cast from an ``int`` to another ``int``. |
| 19 |
|
| 20 |
Casting operations involving constructor conversions, user-defined conversions, |
| 21 |
functional casts, type-dependent casts, casts between distinct type aliases that |
| 22 |
refer to the same underlying type, as well as bitfield-related casts and casts |
| 23 |
directly from lvalue to rvalue, are all disregarded by the check. |
| 24 |
|
| 25 |
Options |
| 26 |
------- |
| 27 |
|
| 28 |
.. option:: IgnoreMacros |
| 29 |
|
| 30 |
If set to `true`, the check will not give warnings inside macros. Default |
| 31 |
is `true`. |
| 32 |
|
| 33 |
.. option:: IgnoreTypeAliases |
| 34 |
|
| 35 |
When set to `false`, the check will consider type aliases, and when set to |
| 36 |
`true`, it will resolve all type aliases and operate on the underlying |
| 37 |
types. Default is `false`. |