readability-qualified-auto¶
Adds pointer qualifications to auto-typed variables that are deduced to
pointers.
LLVM Coding Standards
advises to make it obvious if a auto typed variable is a pointer. This
check will transform auto to auto * when the type is deduced to be a
pointer.
for (auto Data : MutatablePtrContainer) {
change(*Data);
}
for (auto Data : ConstantPtrContainer) {
observe(*Data);
}
Would be transformed into:
for (auto *Data : MutatablePtrContainer) {
change(*Data);
}
for (const auto *Data : ConstantPtrContainer) {
observe(*Data);
}
Note const volatile qualified types will retain their const and
volatile qualifiers. Pointers to pointers will not be fully qualified.
const auto Foo = cast<int *>(Baz1);
const auto Bar = cast<const int *>(Baz2);
volatile auto FooBar = cast<int *>(Baz3);
auto BarFoo = cast<int **>(Baz4);
Would be transformed into:
auto *const Foo = cast<int *>(Baz1);
const auto *const Bar = cast<const int *>(Baz2);
auto *volatile FooBar = cast<int *>(Baz3);
auto *BarFoo = cast<int **>(Baz4);
Options¶
- AddConstToQualified¶
When
true, the check will add const qualifiers to variables defined asauto *orauto &when applicable. Default istrue.
auto Foo1 = cast<const int *>(Bar1);
auto *Foo2 = cast<const int *>(Bar2);
auto &Foo3 = cast<const int &>(Bar3);
If AddConstToQualified is set to false, it will be transformed into:
const auto *Foo1 = cast<const int *>(Bar1);
auto *Foo2 = cast<const int *>(Bar2);
auto &Foo3 = cast<const int &>(Bar3);
Otherwise it will be transformed into:
const auto *Foo1 = cast<const int *>(Bar1);
const auto *Foo2 = cast<const int *>(Bar2);
const auto &Foo3 = cast<const int &>(Bar3);
For the llvm-qualified-auto alias, default is false.
- AllowedTypes¶
A semicolon-separated list of names of types to ignore when
autois deduced to that type or a pointer to that type. Note that this distinguishes type aliases from the original type, so specifying e.g.my_intwill not suppress reports aboutinteven if it is defined as atypedefalias forint. Regular expressions are accepted, e.g.[Rr]ef(erence)?$matches every type with suffixRef,ref,Referenceandreference. If a name in the list contains the sequence::it is matched against the qualified type name (i.e.namespace::Type), otherwise it is matched against only the type name (i.e.Type). E.g. to suppress reports forstd::arrayiterators usestd::array<.*>::(const_)?iteratorstring. Default is an empty string.
- IgnoreAliasing¶
When
true, the check will use the underlying type to determine the type thatautois deduced to. Whenfalse, the check will not look beyond the first type alias. Default istrue.using IntPtr = int*; IntPtr foo(); auto bar = foo();
If
IgnoreAliasingis set totrue, it will be transformed into:auto *bar = foo();
Otherwise no changes will occur.
Limitations¶
When IgnoreAliasing is set to false, there are cases where
Clang has not preserved the type alias and the underlying type will be used so
false positives may occur.
For example:
using IntPtr = int *;
void loopPtr(const std::vector<IntPtr> &VectorIntPtr) {
// May fail for IgnoreAliasing==false as AST does not have the 'IntPtr'
for (auto Data : VectorIntPtr) {
}
}