[PATCH] D27210: [clang-tidy] misc-string-compare. Adding a new check to clang-tidy

Piotr Padlewski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 15 11:17:46 PST 2016

Prazek added a comment.

Good job.
I think it is resonable to warn in cases like:

  if (str.compare(str2) == 1)

or even

  if(str.compare(str2) == -1)

Sometimes people check for 1 or -1 instead of > 0 and < 0. If I remember corectly PVS studio found some bugs like this.

Comment at: clang-tidy/misc/StringCompareCheck.cpp:27
+                               hasName("::std::basic_string"))))),
+      hasArgument(0, declRefExpr()), callee(memberExpr()));
malcolm.parsons wrote:
> I don't think you need to check what the first argument is.
+1, just check if you are calling function with 1 argument.
you can still use hasArgument(0, expr().bind("str2")) to bind argument

Comment at: clang-tidy/misc/StringCompareCheck.cpp:25
+    return;
+  const auto strCompare = cxxMemberCallExpr(
+      callee(cxxMethodDecl(hasName("compare"),
Start with upper case


More information about the cfe-commits mailing list