diff options
Diffstat (limited to 'docs/checks/README-container-anti-pattern.md')
-rw-r--r-- | docs/checks/README-container-anti-pattern.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/docs/checks/README-container-anti-pattern.md b/docs/checks/README-container-anti-pattern.md new file mode 100644 index 00000000..b7a77d18 --- /dev/null +++ b/docs/checks/README-container-anti-pattern.md @@ -0,0 +1,24 @@ +# container-anti-pattern + +Finds when temporary containers are being created needlessly. +These cases are usually easy to fix by using iterators, avoiding memory allocations. + +Matches code like: + + {QMap, QHash, QSet}.values().* + {QMap, QHash}.keys().* + {QVector, QSet}.toList().* + QList::toVector().* + QSet::intersect(other).isEmpty() + for (auto i : {QHash, QMap}.values()) {} + foreach (auto i, {QHash, QMap}.values()) {} + +#### Example + + set.toList()[0]; // use set.constFirst() instead + hash.values().size(); // Use hash.size() instead + hash.keys().contains(); // Use hash.contains() instead + hash.values().contains(); // Use std::find(hash.cbegin(), hash.cend(), myValue) instead + map.values(k).foo ; // Use QMap::equal_range(k) instead + for (auto i : hash.values()) {} // Iterate the hash directly instead: for (auto i : hash) {} + QSet::intersect(other).isEmpty() // Use QSet::intersects() instead, avoiding memory allocations and iterations, since Qt 5.6 |