It turns out that there is no reason to restrict to loop variables. This
allows catching stuff like:
const std::pair<uint32, SomeLargeObject>& kv = *map.begin();
(did you notice the missing const uint32_t) ?
On our codebase, this roughly doubles the number of warnings compared to
the loop-only case.
The warnings mainly fall in two categories:
- Lifetime extended temporary:
const std::function<...>& c = [](...) {};
Which is better written as one of:
const std::function<...> c = [](...) {}; // explicit const auto c = [](...) {}; // no std::function
- Copy + lifetime extension.
const std::pair<uint32, SomeLargeObject>& kv = *map.begin();
Better written as:
// no copy: const std::pair<const uint32, SomeLargeObject>& kv = *map.begin(); const auto& kv = *map.begin(); // explicit const std::pair<uint32, SomeLargeObject> kv = *map.begin(); // explicit, allows automatic move. std::pair<uint32, SomeLargeObject> kv = *map.begin();
And rename the check.
Please fix length and use space after clang-tidy.