| 1 |
.. title:: clang-tidy - misc-misplaced-const |
| 2 |
|
| 3 |
misc-misplaced-const |
| 4 |
==================== |
| 5 |
|
| 6 |
This check diagnoses when a ``const`` qualifier is applied to a ``typedef``/ |
| 7 |
``using`` to a pointer type rather than to the pointee, because such constructs |
| 8 |
are often misleading to developers because the ``const`` applies to the pointer |
| 9 |
rather than the pointee. |
| 10 |
|
| 11 |
For instance, in the following code, the resulting type is ``int * const`` |
| 12 |
rather than ``const int *``: |
| 13 |
|
| 14 |
.. code-block:: c++ |
| 15 |
|
| 16 |
typedef int *int_ptr; |
| 17 |
void f(const int_ptr ptr) { |
| 18 |
*ptr = 0; // potentially quite unexpectedly the int can be modified here |
| 19 |
ptr = 0; // does not compile |
| 20 |
} |
| 21 |
|
| 22 |
The check does not diagnose when the underlying ``typedef``/``using`` type is a |
| 23 |
pointer to a ``const`` type or a function pointer type. This is because the |
| 24 |
``const`` qualifier is less likely to be mistaken because it would be redundant |
| 25 |
(or disallowed) on the underlying pointee type. |