xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-casting.rst (revision 1ad1f981a62213c1fc3145e8330f3c9b0dbe2b85)
1.. title:: clang-tidy - readability-redundant-casting
2
3readability-redundant-casting
4=============================
5
6Detects explicit type casting operations that involve the same source and
7destination types, and subsequently recommend their removal. Covers a range of
8explicit casting operations, including ``static_cast``, ``const_cast``, C-style
9casts, and ``reinterpret_cast``. Its primary objective is to enhance code
10readability 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
17In this example, the ``static_cast<int>(value)`` is redundant, as it performs
18a cast from an ``int`` to another ``int``.
19
20Casting operations involving constructor conversions, user-defined conversions,
21functional casts, type-dependent casts, casts between distinct type aliases that
22refer to the same underlying type, as well as bitfield-related casts and casts
23directly from lvalue to rvalue, are all disregarded by the check.
24
25Options
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`.
38