[clang-tools-extra] [clang-tidy] Emit deprecation warning for preformance-faster-string-find (PR #191922)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 14 23:20:56 PDT 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/191922
>From 37bd328f7cfa38301037e1fa6e82e19eaafa3878 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Tue, 14 Apr 2026 09:38:24 +0800
Subject: [PATCH 1/2] [clang-tidy] Emit deprecation warning for
preformance-faster-string-find
---
.../PreferSingleCharOverloadsCheck.cpp | 16 +++++++++-
.../prefer-single-char-overloads-alias.cpp | 32 +++++++++++++++++++
2 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
diff --git a/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp
index 661d5e3d6b911..c29fef3b71c5e 100644
--- a/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "PreferSingleCharOverloadsCheck.h"
+#include "../utils/CheckUtils.h"
#include "../utils/OptionsUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/Support/raw_ostream.h"
@@ -16,6 +17,15 @@ using namespace clang::ast_matchers;
namespace clang::tidy::performance {
+namespace {
+
+constexpr llvm::StringLiteral DeprecatedCheckName =
+ "performance-faster-string-find";
+constexpr llvm::StringLiteral CanonicalCheckName =
+ "performance-prefer-single-char-overloads";
+
+} // namespace
+
static std::optional<std::string>
makeCharacterLiteral(const StringLiteral *Literal) {
std::string Result;
@@ -46,7 +56,11 @@ PreferSingleCharOverloadsCheck::PreferSingleCharOverloadsCheck(
: ClangTidyCheck(Name, Context),
StringLikeClasses(utils::options::parseStringList(
Options.get("StringLikeClasses",
- "::std::basic_string;::std::basic_string_view"))) {}
+ "::std::basic_string;::std::basic_string_view"))) {
+ if (Name == DeprecatedCheckName)
+ utils::diagDeprecatedCheckAlias(*this, *Context, DeprecatedCheckName,
+ CanonicalCheckName);
+}
void PreferSingleCharOverloadsCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
new file mode 100644
index 0000000000000..30f992d85378d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
@@ -0,0 +1,32 @@
+// RUN: %check_clang_tidy %s performance-faster-string-find %t
+// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-find %t -- \
+// RUN: -config="{CheckOptions: \
+// RUN: {performance-faster-string-find.StringLikeClasses: \
+// RUN: '::llvm::StringRef;'}}"
+// RUN: %check_clang_tidy -check-suffix=BOTH %s \
+// RUN: performance-faster-string-find,performance-prefer-single-char-overloads %t
+
+#include <string>
+
+namespace llvm {
+struct StringRef {
+ int find(const char *) const;
+};
+} // namespace llvm
+
+void stringFind() {
+ std::string Str;
+ Str.find("a");
+ // CHECK-MESSAGES: warning: 'performance-faster-string-find' check is deprecated and will be removed in a future release; consider using 'performance-prefer-single-char-overloads' instead [clang-tidy-config]
+ // CHECK-MESSAGES: [[@LINE-2]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-find]
+ // CHECK-FIXES: Str.find('a');
+ // CHECK-MESSAGES-BOTH: [[@LINE-4]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character
+}
+
+void customStringLikeClass() {
+ llvm::StringRef Sr;
+ Sr.find("x");
+ // CHECK-MESSAGES-CUSTOM: warning: 'performance-faster-string-find' check is deprecated and will be removed in a future release; consider using 'performance-prefer-single-char-overloads' instead [clang-tidy-config]
+ // CHECK-MESSAGES-CUSTOM: [[@LINE-2]]:11: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-find]
+ // CHECK-FIXES-CUSTOM: Sr.find('x');
+}
>From 807eece599e00e25d440b751bf87dee8e2cfda6a Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Wed, 15 Apr 2026 14:20:32 +0800
Subject: [PATCH 2/2] better?
---
.../prefer-single-char-overloads-alias.cpp | 21 -------------------
1 file changed, 21 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
index 30f992d85378d..2c09acaf15ef7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp
@@ -1,32 +1,11 @@
// RUN: %check_clang_tidy %s performance-faster-string-find %t
-// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-find %t -- \
-// RUN: -config="{CheckOptions: \
-// RUN: {performance-faster-string-find.StringLikeClasses: \
-// RUN: '::llvm::StringRef;'}}"
-// RUN: %check_clang_tidy -check-suffix=BOTH %s \
-// RUN: performance-faster-string-find,performance-prefer-single-char-overloads %t
#include <string>
-namespace llvm {
-struct StringRef {
- int find(const char *) const;
-};
-} // namespace llvm
-
void stringFind() {
std::string Str;
Str.find("a");
// CHECK-MESSAGES: warning: 'performance-faster-string-find' check is deprecated and will be removed in a future release; consider using 'performance-prefer-single-char-overloads' instead [clang-tidy-config]
// CHECK-MESSAGES: [[@LINE-2]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-find]
// CHECK-FIXES: Str.find('a');
- // CHECK-MESSAGES-BOTH: [[@LINE-4]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character
-}
-
-void customStringLikeClass() {
- llvm::StringRef Sr;
- Sr.find("x");
- // CHECK-MESSAGES-CUSTOM: warning: 'performance-faster-string-find' check is deprecated and will be removed in a future release; consider using 'performance-prefer-single-char-overloads' instead [clang-tidy-config]
- // CHECK-MESSAGES-CUSTOM: [[@LINE-2]]:11: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-find]
- // CHECK-FIXES-CUSTOM: Sr.find('x');
}
More information about the cfe-commits
mailing list