[clang-tidy] misc-string-integer-assignment: fix false positive
authorClement Courbet <courbet@google.com>
Thu, 28 Feb 2019 10:33:32 +0000 (10:33 +0000)
committerClement Courbet <courbet@google.com>
Thu, 28 Feb 2019 10:33:32 +0000 (10:33 +0000)
Summary:
using CodePoint = uint32_t;
CodePoint cp;
basic_string<CodePoint> s;
s += cp;

See PR27723.

Reviewers: xazax.hun, alexfh

Subscribers: rnkovacs, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58606

llvm-svn: 355076

clang-tools-extra/clang-tidy/bugprone/StringIntegerAssignmentCheck.cpp
clang-tools-extra/test/clang-tidy/bugprone-string-integer-assignment.cpp

index e9bde2a..4288c2d 100644 (file)
@@ -26,7 +26,8 @@ void StringIntegerAssignmentCheck::registerMatchers(MatchFinder *Finder) {
                 hasOverloadedOperatorName("+=")),
           callee(cxxMethodDecl(ofClass(classTemplateSpecializationDecl(
               hasName("::std::basic_string"),
-              hasTemplateArgument(0, refersToType(qualType().bind("type"))))))),
+              hasTemplateArgument(0, refersToType(hasCanonicalType(
+                                         qualType().bind("type")))))))),
           hasArgument(
               1,
               ignoringImpCasts(
@@ -34,7 +35,11 @@ void StringIntegerAssignmentCheck::registerMatchers(MatchFinder *Finder) {
                        // Ignore calls to tolower/toupper (see PR27723).
                        unless(callExpr(callee(functionDecl(
                            hasAnyName("tolower", "std::tolower", "toupper",
-                                      "std::toupper"))))))
+                                      "std::toupper"))))),
+                       // Do not warn if assigning e.g. `CodePoint` to
+                       // `basic_string<CodePoint>`
+                       unless(hasType(qualType(
+                           hasCanonicalType(equalsBoundNode("type"))))))
                       .bind("expr"))),
           unless(isInTemplateInstantiation())),
       this);
index 0317354..2b73f89 100644 (file)
@@ -53,8 +53,8 @@ int main() {
 
   std::basic_string<MyArcaneChar> as;
   as = 6;
-// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: an integer is interpreted as a chara
-// CHECK-FIXES: {{^}}  as = 6;{{$}}
+  as = static_cast<MyArcaneChar>(6);
+  as = 'a';
 
   s += toupper(x);
   s += tolower(x);