sizeof(some_std_string) is likely to be an error. This check finds this
pattern and suggests using .size() instead.
Details
- Reviewers
klimek aaron.ballman djasper - Commits
- rG7532d3e93d39: [clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl…
rCTE247297: [clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl
rL247297: [clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl
Diff Detail
Event Timeline
Great idea for a checker! Some comments below.
clang-tidy/misc/SizeofContainerCheck.cpp | ||
---|---|---|
37 | Do you need the expr() matcher? | |
39 | I think that this should be checking for more than just basic_string and vector. Why not other containers? For instance, anything that exposes a member function with a size() function seems like it would be reasonable. | |
49 | I think the period should be replaced with a semicolon (it's not a sentence, so the period is also wrong). e.g. (with some wording tweaks), "sizeof() does not return the size of the container; did you mean to call size()?" Also, is this actually emitting the diagnostic? If so, a comment would be good explaining why the early return for macros is located where it is, so no one does a drive-by "fix." |
clang-tidy/misc/SizeofContainerCheck.cpp | ||
---|---|---|
38 | Yes, I do. sizeOfExpr().bind("...") doesn't compile for some reason. tools/clang/tools/extra/clang-tidy/misc/SizeofContainerCheck.cpp:24:35: error: no member named 'bind' in 'clang::ast_matchers::internal::Matcher<clang::Stmt>' .bind("expr"))).bind("sizeof"), ~~~~~~~~~~~~~~~ ^ | |
40 | I'd like to limit this to the STL containers first. So "any recordDecl named std::.* with a .size() method" might work. | |
50 |
|
I noticed a few more things, but mostly nitpicky at this point.
clang-tidy/misc/SizeofContainerCheck.cpp | ||
---|---|---|
23 | Should we also ignore parens for when people do crazy things, like sizeof((((some_string+other_string))))? | |
26 | I missed this earlier, but what about other 2-arg overloaded operators, like placement operator new & operator delete, or operator delete with size (and array forms)? | |
39 | Why is "|::string" needed? |
Don't complain on the ARRAYSIZE(<array of containers>) pattern (where
sizeof(container) is used as a denominator).
clang-tidy/misc/SizeofContainerCheck.cpp | ||
---|---|---|
23 | I don't think we need to remove anything beyond the most external pair of parentheses. | |
26 | Do you have an example of an expression that will break when a .size() is appended to it? Note, that it should be an expression of a class type. | |
39 | Needed for code bases that use a std::string-like string class defined in the global namespace. Maybe we need a configuration option for custom container regexps. But this will likely be a follow up. |
This appears to have broken one of the bots:
http://bb.pgr.jp/builders/ninja-x64-msvc-RA-centos6/builds/15065
Should we also ignore parens for when people do crazy things, like sizeof((((some_string+other_string))))?