[PATCH] D24349: [clang-tidy] Extend readability-container-size-empty to arbitrary class with size() and empty()

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 09:17:24 PDT 2016


aaron.ballman added a subscriber: aaron.ballman.
aaron.ballman added a reviewer: aaron.ballman.

================
Comment at: clang-tidy/readability/ContainerSizeEmptyCheck.cpp:33
@@ +32,3 @@
+  const auto validContainer = namedDecl(
+      has(functionDecl(isPublic(), hasName("size"), returns(isInteger()))),
+      has(functionDecl(isPublic(), hasName("empty"), returns(booleanType()))));
----------------
While it's not entirely unreasonable, it makes me uneasy to assume that anything with a size() and empty() member function must be a container. Is there a way to silence false positives aside from disabling the check entirely?

Perhaps this should instead be a user-configurable list of containers that's prepopulated with STL container names?

Also note: `returns(isInteger())` seems a bit permissive. Consider:
```
bool size() const;

enum E { one };
enum E size() const;
```
These both will qualify for that matcher, but don't seem like particularly valid indications that the object is a container.


https://reviews.llvm.org/D24349





More information about the cfe-commits mailing list