| Summary: | new check idea: avoid using Q_FOREACH to iterate over QMap/QHash keys and retrieve values inside loop | ||
|---|---|---|---|
| Product: | [Developer tools] clazy | Reporter: | Nyall Dawson <nyall.dawson> |
| Component: | general | Assignee: | Sergio Martins <smartins> |
| Status: | RESOLVED FIXED | ||
| Severity: | wishlist | CC: | smartins |
| Priority: | NOR | ||
| Version First Reported In: | unspecified | ||
| Target Milestone: | --- | ||
| Platform: | Other | ||
| OS: | Linux | ||
| Latest Commit: | Version Fixed/Implemented In: | ||
| Sentry Crash Report: | |||
The container-anti-pattern will warn when you iterate on keys(), which you should fix with:
Q_FOREACH( const QString& value, map) {}, or using iterator if you need both value and key.
There might be corner-cases where people are doing something very stupid, but it's difficult to make a check for more imaginative cases, so I consider this done
|
Eg: QMap<QString,QString> map; Q_FOREACH( const QString& key, map.keys() ) { QString val = map.value(key); ... } It's inefficient, and it'd be nice to catch this and suggest using QMap::iterator/QHash::Iterator to iterate over both the keys and values at the same time to avoid the unnecessary lookup. Reproducible: Always