Implementing feature request.
This patch extends readability-container-size-empty check allowing it to produce warnings not only for stl containers.
Differential D24349
[clang-tidy] Extend readability-container-size-empty to arbitrary class with size() and empty() omtcyfz on Sep 8 2016, 8:57 AM. Authored by
Details
Implementing feature request. This patch extends readability-container-size-empty check allowing it to produce warnings not only for stl containers.
Diff Detail Event Timeline
Comment Actions Probably check should have options to extend list of containers and also to assume all classes with integer type size() const and bool empty() const as containers. It may be not trivial to find out all custom containers and last option will be helpful to assemble such list. Comment Actions I think this should actually have two options: one is a list of default-checked containers (default is the list of STL containers we previously had), and the other is a Boolean option for guessing at what might be a container based on function signature (default is true). This gives people a way to silence the diagnostic while still being useful. I would document what function signatures we use as our heuristic, but note that the function signatures we care about may change (for instance, we may decide that const-qualification is unimportant, or that we want to require begin()/end() functions, etc). I think <standard integer type> size() const and bool empty() const are a reasonable heuristic for a first pass.
Comment Actions Should we also check for absence of parameters in size() and empty() as well as const? Comment Actions I was thinking about that, but I'm not sure people are misusing size() and empty() so much. I do not agree about the const part. It should be encouraged to use const for these, but I do not think they should be a requirement. Comment Actions True. But my point is that they are not required to do that if they're just not marked const. Comment Actions I agree with Eugene. But I think a good consensus could be to warn on the non-const case but do not do the rewrite. What do you think? Comment Actions I think that's reasonable, depending on whether we find false positives with the warning as well (I have a slight concern about size() and empty() being unrelated operations on a non-container class that someone writes). Comment Actions Agreed. I'd like to try LLVM as an example of a huge codebase tomorrow to see how many false-positives there actually are. Generally, I can see the point of requiring const and providing options to reduce the set of allowed classes to std::container's, but at the same time my thoughts are as follows:
I'm very curious to see how many false-positives there would be in LLVM codebase, for example. I'll try it tomorrow.
Comment Actions Thank you for the patch! While I understand your concerns, I would suggest to wait for actual bug reports before introducing more options. The duck typing approach worked really well for readability-redundant-smartptr-get and I expect it to work equally well here. The constraints checked here are pretty strict and should make the check safe.
Yep, documentation should be updated accordingly.
Comment Actions That means there is no way to silence this check on false positives aside from disabling it entirely, which is not a particularly good user experience. However, I do agree that if we constrain the heuristic appropriately, the number of false positives should be low.
Comment Actions Just checked: readability-container-size-empty with the patch finds a few thousand new warnings related to about 400 unique container names in our code. A representative sample shows no false positives and we've found just a single false negative (that triggers the check without the patch) - a container's size() method was not const. Which makes me pretty sure that:
Comment Actions LG
|
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:
These both will qualify for that matcher, but don't seem like particularly valid indications that the object is a container.