It is possible to assign arbitrary integer types to strings. Sometimes it is the result of missing to_string call or apostrophes.
This check aims to find such errors.
Differential D15411
[clang-tidy] Check for suspicious string assignments. xazax.hun on Dec 10 2015, 2:52 AM. Authored by
Details It is possible to assign arbitrary integer types to strings. Sometimes it is the result of missing to_string call or apostrophes. This check aims to find such errors.
Diff Detail Event TimelineComment Actions That's quite a surprising behavior. Looks like a bug in the library implementation (or in the standard) to me. But the check is awesome. Thank you!
Comment Actions BTW, Richard, is the possibility of assignment of an integer to a std::string a bug in the standard? Comment Actions Thank you for the review. I have seen a few false positives in the llvm codebase. I agree that these kind of conversions are unfortunate but I am sure fixing this would cause a huge backward compatibility problem. Nontheless I also think that it might be something that is worth to consider.
Comment Actions This is the result of: basic_string& operator=(charT c); Returns: *this = basic_string(1,c). So I believe it is by design. Comment Actions I see how the current behavior can be explained by the standard, however I'm not sure this was the intention of the standard to specifically allow this behavior. And if it's unintended (and I'd say undesired), the standard could also be changed to enforce a mechanism to disallow assignment of std::string from other integer types (e.g. a templated deleted operator= enable_if'd for all integral types except for charT, or something like that). Maybe it wouldn't be reasonable for some reason, but it's worth trying ;)
Comment Actions
Comment Actions Looks good with a few nits. Thank you for the new awesome check!
Comment Actions Thank you for the review. I committed the new matcher to clang as well. I went with isAnyCharacter name for now, but we can rename it anytime. |
I suggest making the name more specific, e.g. StringIntegerAssignmentCheck. Same for the check name (misc-string-integer-assignment).