https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/186946
>From e2bf54187e64db0316cbe6f226f85c9533350b27 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Tue, 17 Mar 2026 11:29:49 +0800 Subject: [PATCH 1/2] [clang-tidy] Rename performance-faster-string-find to performance-faster-string-operation --- .../clang-tidy/performance/CMakeLists.txt | 2 +- ...eck.cpp => FasterStringOperationCheck.cpp} | 13 +++---- ...ndCheck.h => FasterStringOperationCheck.h} | 12 +++---- .../performance/PerformanceTidyModule.cpp | 6 ++-- clang-tools-extra/docs/ReleaseNotes.rst | 16 +++++++-- .../docs/clang-tidy/checks/list.rst | 1 + .../checks/performance/faster-string-find.rst | 30 +++------------- .../performance/faster-string-operation.rst | 35 +++++++++++++++++++ ...g-find.cpp => faster-string-operation.cpp} | 8 ++--- 9 files changed, 77 insertions(+), 46 deletions(-) rename clang-tools-extra/clang-tidy/performance/{FasterStringFindCheck.cpp => FasterStringOperationCheck.cpp} (89%) rename clang-tools-extra/clang-tidy/performance/{FasterStringFindCheck.h => FasterStringOperationCheck.h} (87%) create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst rename clang-tools-extra/test/clang-tidy/checkers/performance/{faster-string-find.cpp => faster-string-operation.cpp} (95%) diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 5b3e849fb20a8..1c9c7bab2b46a 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -6,7 +6,7 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyPerformanceModule STATIC AvoidEndlCheck.cpp EnumSizeCheck.cpp - FasterStringFindCheck.cpp + FasterStringOperationCheck.cpp ForRangeCopyCheck.cpp ImplicitConversionInLoopCheck.cpp InefficientAlgorithmCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp similarity index 89% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp rename to clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp index 1d9325166e341..0b94eb09ee0bc 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "FasterStringFindCheck.h" +#include "FasterStringOperationCheck.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -42,19 +42,20 @@ makeCharacterLiteral(const StringLiteral *Literal) { return Result; } -FasterStringFindCheck::FasterStringFindCheck(StringRef Name, - ClangTidyContext *Context) +FasterStringOperationCheck::FasterStringOperationCheck( + StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), StringLikeClasses(utils::options::parseStringList( Options.get("StringLikeClasses", "::std::basic_string;::std::basic_string_view"))) {} -void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { +void FasterStringOperationCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StringLikeClasses", utils::options::serializeStringList(StringLikeClasses)); } -void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { +void FasterStringOperationCheck::registerMatchers(MatchFinder *Finder) { const auto SingleChar = ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")); @@ -78,7 +79,7 @@ void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { this); } -void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) { +void FasterStringOperationCheck::check(const MatchFinder::MatchResult &Result) { const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal"); const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h similarity index 87% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h rename to clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h index f5d36d805498e..4569bdf00690d 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H #include "../ClangTidyCheck.h" @@ -21,10 +21,10 @@ namespace clang::tidy::performance { /// The character literal overload is more efficient. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-find.html -class FasterStringFindCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-operation.html +class FasterStringOperationCheck : public ClangTidyCheck { public: - FasterStringFindCheck(StringRef Name, ClangTidyContext *Context); + FasterStringOperationCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; } @@ -41,4 +41,4 @@ class FasterStringFindCheck : public ClangTidyCheck { } // namespace clang::tidy::performance -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 21274fae43795..0deab21a8ea3a 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -10,7 +10,7 @@ #include "../ClangTidyModule.h" #include "AvoidEndlCheck.h" #include "EnumSizeCheck.h" -#include "FasterStringFindCheck.h" +#include "FasterStringOperationCheck.h" #include "ForRangeCopyCheck.h" #include "ImplicitConversionInLoopCheck.h" #include "InefficientAlgorithmCheck.h" @@ -39,8 +39,10 @@ class PerformanceModule : public ClangTidyModule { void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<AvoidEndlCheck>("performance-avoid-endl"); CheckFactories.registerCheck<EnumSizeCheck>("performance-enum-size"); - CheckFactories.registerCheck<FasterStringFindCheck>( + CheckFactories.registerCheck<FasterStringOperationCheck>( "performance-faster-string-find"); + CheckFactories.registerCheck<FasterStringOperationCheck>( + "performance-faster-string-operation"); CheckFactories.registerCheck<ForRangeCopyCheck>( "performance-for-range-copy"); CheckFactories.registerCheck<ImplicitConversionInLoopCheck>( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 077335c16c331..9e79981fe888a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -49,6 +49,12 @@ Major New Features Potentially Breaking Changes ---------------------------- +- Deprecated the :program:`clang-tidy` check :doc:`performance-faster-string-find + <clang-tidy/checks/performance/faster-string-find>`. It has been renamed to + :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>`. + The original check will be removed in the 25th release. + Improvements to clangd ---------------------- @@ -174,6 +180,12 @@ New check aliases <clang-tidy/checks/portability/no-assembler>`. The `hicpp-no-assembler` name is kept as an alias. +- Renamed :doc:`performance-faster-string-find + <clang-tidy/checks/performance/faster-string-find>` to + :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>`. + The `performance-faster-string-find` name is kept as an alias. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -292,8 +304,8 @@ Changes in existing checks - Improved the ignore list to correctly handle ``typedef`` and ``enum``. -- Improved :doc:`performance-faster-string-find - <clang-tidy/checks/performance/faster-string-find>` check: +- Improved :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>` check: - Now analyzes calls to the ``starts_with``, ``ends_with``, ``contains``, and ``operator+=`` string member functions. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 362455796bd50..0854bb5676d6e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -351,6 +351,7 @@ Clang-Tidy Checks :doc:`performance-avoid-endl <performance/avoid-endl>`, "Yes" :doc:`performance-enum-size <performance/enum-size>`, :doc:`performance-faster-string-find <performance/faster-string-find>`, "Yes" + :doc:`performance-faster-string-operation <performance/faster-string-operation>`, "Yes" :doc:`performance-for-range-copy <performance/for-range-copy>`, "Yes" :doc:`performance-implicit-conversion-in-loop <performance/implicit-conversion-in-loop>`, :doc:`performance-inefficient-algorithm <performance/inefficient-algorithm>`, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst index e7ed869acc8ad..3dc2ff04d53e6 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst @@ -1,30 +1,10 @@ .. title:: clang-tidy - performance-faster-string-find +.. meta:: + :http-equiv=refresh: 5;URL=faster-string-operation.html performance-faster-string-find ============================== -Optimize calls to ``std::string::find()`` and friends when the needle passed is -a single character string literal. The character literal overload is more -efficient. - -Examples: - -.. code-block:: c++ - - str.find("A"); - - // becomes - - str.find('A'); - -Options -------- - -.. option:: StringLikeClasses - - Semicolon-separated list of names of string-like classes. By default only - ``::std::basic_string`` and ``::std::basic_string_view`` are considered. - Within these classes, the check will only consider member functions named - ``find``, ``rfind``, ``find_first_of``, ``find_first_not_of``, - ``find_last_of``, ``find_last_not_of``, ``starts_with``, ``ends_with``, - ``contains``, or ``operator+=``. +The `performance-faster-string-find check` is an alias, please see +:doc:`performance-faster-string-operation <faster-string-operation>` +for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst new file mode 100644 index 0000000000000..25907b2f5e6d3 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst @@ -0,0 +1,35 @@ +.. title:: clang-tidy - performance-faster-string-operation + +performance-faster-string-operation +=================================== + +Optimize calls to ``std::string::find()`` and friends when the needle passed is +a single character string literal. The character literal overload is more +efficient. + +Examples: + +.. code-block:: c++ + + str.find("A"); + str += "B"; + + // becomes + + str.find('A'); + str += 'B'; + +This check flags passing strings of size 1 to miscellaneous member functions +as well as ``operator+=``. + +Options +------- + +.. option:: StringLikeClasses + + Semicolon-separated list of names of string-like classes. By default only + ``::std::basic_string`` and ``::std::basic_string_view`` are considered. + Within these classes, the check will only consider member functions named + ``find``, ``rfind``, ``find_first_of``, ``find_first_not_of``, + ``find_last_of``, ``find_last_not_of``, ``starts_with``, ``ends_with``, + ``contains``, or ``operator+=``. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp similarity index 95% rename from clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp rename to clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp index 3999049a10718..1c093afa41ef0 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp @@ -1,7 +1,7 @@ -// RUN: %check_clang_tidy %s performance-faster-string-find %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-find %t -- \ +// RUN: %check_clang_tidy %s performance-faster-string-operation %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-operation %t -- \ // RUN: -config="{CheckOptions: \ -// RUN: {performance-faster-string-find.StringLikeClasses: \ +// RUN: {performance-faster-string-operation.StringLikeClasses: \ // RUN: '::llvm::StringRef;'}}" -- -fno-delayed-template-parsing #include <string> @@ -23,7 +23,7 @@ void StringFind() { std::string Str; Str.find("a"); - // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more effective overload accepting a character [performance-faster-string-find] + // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more effective overload accepting a character [performance-faster-string-operation] // CHECK-FIXES: Str.find('a'); // Works with the pos argument. >From 7f59a56fa2718f46fb091397d3ce8cbd39c0e7f9 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Mon, 23 Mar 2026 13:48:13 +0800 Subject: [PATCH 2/2] fix header --- .../clang-tidy/performance/FasterStringOperationCheck.cpp | 1 - .../clang-tidy/performance/FasterStringOperationCheck.h | 1 - 2 files changed, 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp index def8964e40aeb..d64d4ed5c5aac 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp @@ -8,7 +8,6 @@ #include "FasterStringOperationCheck.h" #include "../utils/OptionsUtils.h" -#include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/Support/raw_ostream.h" #include <optional> diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h index 4569bdf00690d..55413171263a4 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h @@ -11,7 +11,6 @@ #include "../ClangTidyCheck.h" -#include <string> #include <vector> namespace clang::tidy::performance { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
