I am working on a software project and have found numerous examples where find_first_of(), find_first_not_of(), find_last_of(), and find_last_not_of() are incorrectly used. These std::string methods take a string argument for a set of characters to search for whereas, the developer really means to use compare(), find(), or rfind() which take a string argument for a string to search for.
Now it would be great to educate everyone and never have this occur again, however, I would like to create a code inspector to identify suspect usages and flag them for review to attempt to semi-automate the resolution of this issue in a semi-general fashion.
I’m looking for good heuristics to find most issues with few false positives. What regular expression search patterns might work, or can someone think of other suggestions to attempt to automate the solution to this issue to get most occurrences?
Please no snappy answers about hiring better developers or the like, I wish to ameliorate the situation given the resources available.
This may actually be a situation where you need to visually inspect every call to
find_first_ofand friends. It doesn’t seem like there would normally be a lot of calls, and you can tag them in some way as you verify that they are correct. By inspecting all the calls you have confidence that the existing issues are resolved.The only heuristic I can imagine is that if the “compare to” string is not a literal or constant it’s more likely to be incorrect.