[clang-tools-extra] [clang-tidy] check `std::string_view` and custom string-like classes in `readability-string-compare` (PR #88636)

Vadim D. via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 14 10:15:09 PDT 2024


================
@@ -7,42 +7,70 @@
 //===----------------------------------------------------------------------===//
 
 #include "StringCompareCheck.h"
-#include "../utils/FixItHintUtils.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Tooling/FixIt.h"
+#include "llvm/ADT/StringRef.h"
+#include <vector>
 
 using namespace clang::ast_matchers;
+namespace optutils = clang::tidy::utils::options;
 
 namespace clang::tidy::readability {
 
 static const StringRef CompareMessage = "do not use 'compare' to test equality "
                                         "of strings; use the string equality "
                                         "operator instead";
 
+static const std::vector<StringRef> StringClasses = {
+    "::std::basic_string", "::std::basic_string_view"};
+
+StringCompareCheck::StringCompareCheck(StringRef Name,
+                                       ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      StringLikeClasses(
+          optutils::parseStringList(Options.get("StringLikeClasses", ""))) {}
----------------
vvd170501 wrote:

> put StringClasses as default

I thought about implementing the option this way, but it seems to me it'd be a bit harder to use than a list of additional classes.
if the user wanted to add custom classes, they'd need to also include `std::string` and `std::string_view` in the list to keep the check enabled for these classes.
So, I decided to append custom classes to the default list instead of overwriting it.

> Or call it like "AdditionalStringLikeClasses"

Ok.

> but still have common handling

Could you elaborate? I'm not sure what you mean by "common handling". Removing the lambda?

https://github.com/llvm/llvm-project/pull/88636


More information about the cfe-commits mailing list