| 1 |
.. title:: clang-tidy - readability-qualified-auto |
| 2 |
|
| 3 |
readability-qualified-auto |
| 4 |
========================== |
| 5 |
|
| 6 |
Adds pointer qualifications to ``auto``-typed variables that are deduced to |
| 7 |
pointers. |
| 8 |
|
| 9 |
`LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto>`_ |
| 10 |
advises to make it obvious if a ``auto`` typed variable is a pointer. This |
| 11 |
check will transform ``auto`` to ``auto *`` when the type is deduced to be a |
| 12 |
pointer. |
| 13 |
|
| 14 |
.. code-block:: c++ |
| 15 |
|
| 16 |
for (auto Data : MutatablePtrContainer) { |
| 17 |
change(*Data); |
| 18 |
} |
| 19 |
for (auto Data : ConstantPtrContainer) { |
| 20 |
observe(*Data); |
| 21 |
} |
| 22 |
|
| 23 |
Would be transformed into: |
| 24 |
|
| 25 |
.. code-block:: c++ |
| 26 |
|
| 27 |
for (auto *Data : MutatablePtrContainer) { |
| 28 |
change(*Data); |
| 29 |
} |
| 30 |
for (const auto *Data : ConstantPtrContainer) { |
| 31 |
observe(*Data); |
| 32 |
} |
| 33 |
|
| 34 |
Note ``const`` ``volatile`` qualified types will retain their ``const`` and |
| 35 |
``volatile`` qualifiers. Pointers to pointers will not be fully qualified. |
| 36 |
|
| 37 |
.. code-block:: c++ |
| 38 |
|
| 39 |
const auto Foo = cast<int *>(Baz1); |
| 40 |
const auto Bar = cast<const int *>(Baz2); |
| 41 |
volatile auto FooBar = cast<int *>(Baz3); |
| 42 |
auto BarFoo = cast<int **>(Baz4); |
| 43 |
|
| 44 |
Would be transformed into: |
| 45 |
|
| 46 |
.. code-block:: c++ |
| 47 |
|
| 48 |
auto *const Foo = cast<int *>(Baz1); |
| 49 |
const auto *const Bar = cast<const int *>(Baz2); |
| 50 |
auto *volatile FooBar = cast<int *>(Baz3); |
| 51 |
auto *BarFoo = cast<int **>(Baz4); |
| 52 |
|
| 53 |
Options |
| 54 |
------- |
| 55 |
|
| 56 |
.. option:: AddConstToQualified |
| 57 |
|
| 58 |
When set to `true` the check will add const qualifiers variables defined as |
| 59 |
``auto *`` or ``auto &`` when applicable. |
| 60 |
Default value is `true`. |
| 61 |
|
| 62 |
.. code-block:: c++ |
| 63 |
|
| 64 |
auto Foo1 = cast<const int *>(Bar1); |
| 65 |
auto *Foo2 = cast<const int *>(Bar2); |
| 66 |
auto &Foo3 = cast<const int &>(Bar3); |
| 67 |
|
| 68 |
If AddConstToQualified is set to `false`, it will be transformed into: |
| 69 |
|
| 70 |
.. code-block:: c++ |
| 71 |
|
| 72 |
const auto *Foo1 = cast<const int *>(Bar1); |
| 73 |
auto *Foo2 = cast<const int *>(Bar2); |
| 74 |
auto &Foo3 = cast<const int &>(Bar3); |
| 75 |
|
| 76 |
Otherwise it will be transformed into: |
| 77 |
|
| 78 |
.. code-block:: c++ |
| 79 |
|
| 80 |
const auto *Foo1 = cast<const int *>(Bar1); |
| 81 |
const auto *Foo2 = cast<const int *>(Bar2); |
| 82 |
const auto &Foo3 = cast<const int &>(Bar3); |
| 83 |
|
| 84 |
Note in the LLVM alias, the default value is `false`. |