Checker to validate string constructor parameters.
A common mistake is to swap parameter for the fill-constructor.
std::string str('x', 4); std::string str('4', x);
Paths
| Differential D19146
[clang-tidy] New checker to detect suspicious string constructor. ClosedPublic Authored by etienneb on Apr 14 2016, 8:29 PM.
Details Summary Checker to validate string constructor parameters. A common mistake is to swap parameter for the fill-constructor. std::string str('x', 4); std::string str('4', x);
Diff Detail Event TimelineComment Actions I wonder whether misc-swapped-arguments can be tuned to handle these cases in a more generic way? The only missing part so far seems to be the lack of cxxConstructExpr support.
Comment Actions
And by "these cases" I mean the swapped arguments mistake, not the more string-specific ones. etienneb marked 2 inline comments as done. etienneb edited edge metadata. Comment Actionsalexfh comments. Comment Actions
What about this comment? alexfh edited edge metadata. Comment ActionsLooks good with a nit.
As per offline discussion, there might be value in having the check for swapped std::string ctor arguments here, since this specialized check is likely to have much lower false positive rate than the more generic misc-swapped-arguments check.
This revision is now accepted and ready to land.Apr 19 2016, 4:15 PM
Revision Contents
Diff 53894 clang-tidy/misc/CMakeLists.txt
clang-tidy/misc/MiscTidyModule.cpp
clang-tidy/misc/StringConstructorCheck.h
clang-tidy/misc/StringConstructorCheck.cpp
docs/clang-tidy/checks/list.rst
docs/clang-tidy/checks/misc-string-constructor.rst
test/clang-tidy/misc-string-constructor.cpp
|
It seems to be wasteful to have several matchers sharing a bunch checks (the LiteralStringConstructor part). I'd suggest to merge the matchers and use anyOf to handle different sub-cases: