Author: Zeyi Xu Date: 2026-03-30T14:19:27+08:00 New Revision: 032f36bdbc73b74c4e03bdd9b7e03e60979a0d31
URL: https://github.com/llvm/llvm-project/commit/032f36bdbc73b74c4e03bdd9b7e03e60979a0d31 DIFF: https://github.com/llvm/llvm-project/commit/032f36bdbc73b74c4e03bdd9b7e03e60979a0d31.diff LOG: [clang-tidy] Rename performance-faster-string-find to performance-prefer-single-char-overloads (#186946) Related discussion: https://github.com/llvm/llvm-project/pull/182697#issuecomment-3986242476 Closes https://github.com/llvm/llvm-project/issues/186853 --------- Co-authored-by: Victor Chernyakin <[email protected]> Added: clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.cpp Modified: clang-tools-extra/clang-tidy/performance/CMakeLists.txt clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst Removed: clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp ################################################################################ diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 5b3e849fb20a8..0c778b5a9f36b 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyPerformanceModule STATIC AvoidEndlCheck.cpp EnumSizeCheck.cpp - FasterStringFindCheck.cpp ForRangeCopyCheck.cpp ImplicitConversionInLoopCheck.cpp InefficientAlgorithmCheck.cpp @@ -21,6 +20,7 @@ add_clang_library(clangTidyPerformanceModule STATIC NoexceptMoveConstructorCheck.cpp NoexceptSwapCheck.cpp PerformanceTidyModule.cpp + PreferSingleCharOverloadsCheck.cpp StringViewConversionsCheck.cpp TriviallyDestructibleCheck.cpp TypePromotionInMathFnCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 21274fae43795..a4c1cdacab496 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -10,7 +10,6 @@ #include "../ClangTidyModule.h" #include "AvoidEndlCheck.h" #include "EnumSizeCheck.h" -#include "FasterStringFindCheck.h" #include "ForRangeCopyCheck.h" #include "ImplicitConversionInLoopCheck.h" #include "InefficientAlgorithmCheck.h" @@ -23,6 +22,7 @@ #include "NoexceptDestructorCheck.h" #include "NoexceptMoveConstructorCheck.h" #include "NoexceptSwapCheck.h" +#include "PreferSingleCharOverloadsCheck.h" #include "StringViewConversionsCheck.h" #include "TriviallyDestructibleCheck.h" #include "TypePromotionInMathFnCheck.h" @@ -39,7 +39,7 @@ 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<PreferSingleCharOverloadsCheck>( "performance-faster-string-find"); CheckFactories.registerCheck<ForRangeCopyCheck>( "performance-for-range-copy"); @@ -64,6 +64,8 @@ class PerformanceModule : public ClangTidyModule { "performance-noexcept-move-constructor"); CheckFactories.registerCheck<NoexceptSwapCheck>( "performance-noexcept-swap"); + CheckFactories.registerCheck<PreferSingleCharOverloadsCheck>( + "performance-prefer-single-char-overloads"); CheckFactories.registerCheck<StringViewConversionsCheck>( "performance-string-view-conversions"); CheckFactories.registerCheck<TriviallyDestructibleCheck>( diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp similarity index 88% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp rename to clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp index cd85136baf5ec..661d5e3d6b911 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp @@ -6,9 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "FasterStringFindCheck.h" +#include "PreferSingleCharOverloadsCheck.h" #include "../utils/OptionsUtils.h" -#include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/Support/raw_ostream.h" #include <optional> @@ -42,19 +41,20 @@ makeCharacterLiteral(const StringLiteral *Literal) { return Result; } -FasterStringFindCheck::FasterStringFindCheck(StringRef Name, - ClangTidyContext *Context) +PreferSingleCharOverloadsCheck::PreferSingleCharOverloadsCheck( + 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 PreferSingleCharOverloadsCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StringLikeClasses", utils::options::serializeStringList(StringLikeClasses)); } -void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { +void PreferSingleCharOverloadsCheck::registerMatchers(MatchFinder *Finder) { const auto SingleChar = ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")); @@ -78,7 +78,8 @@ void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { this); } -void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) { +void PreferSingleCharOverloadsCheck::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/PreferSingleCharOverloadsCheck.h similarity index 71% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h rename to clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h index f5d36d805498e..848ad0be9724c 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h +++ b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h @@ -6,12 +6,11 @@ // //===----------------------------------------------------------------------===// -#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_PREFERSINGLECHAROVERLOADSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_PREFERSINGLECHAROVERLOADSCHECK_H #include "../ClangTidyCheck.h" -#include <string> #include <vector> namespace clang::tidy::performance { @@ -21,10 +20,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/prefer-single-char-overloads.html +class PreferSingleCharOverloadsCheck : public ClangTidyCheck { public: - FasterStringFindCheck(StringRef Name, ClangTidyContext *Context); + PreferSingleCharOverloadsCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; } @@ -41,4 +40,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_PREFERSINGLECHAROVERLOADSCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 79eea57ce5d06..b1fd95db9e395 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-prefer-single-char-overloads + <clang-tidy/checks/performance/prefer-single-char-overloads>`. + The original check will be removed in the 25th release. + Improvements to clangd ---------------------- @@ -189,6 +195,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-prefer-single-char-overloads + <clang-tidy/checks/performance/prefer-single-char-overloads>`. + The `performance-faster-string-find` name is kept as an alias. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -357,14 +369,6 @@ 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: - - - Now analyzes calls to the ``starts_with``, ``ends_with``, ``contains``, - and ``operator+=`` string member functions. - - - Fixes false negatives when using ``std::set`` from ``libstdc++``. - - Improved :doc:`performance-inefficient-string-concatenation <clang-tidy/checks/performance/inefficient-string-concatenation>` check by adding support for detecting inefficient string concatenation in ``do-while`` @@ -379,6 +383,14 @@ Changes in existing checks <clang-tidy/checks/performance/move-const-arg>` check by avoiding false positives on trivially copyable types with a non-public copy constructor. +- Improved :doc:`performance-prefer-single-char-overloads + <clang-tidy/checks/performance/prefer-single-char-overloads>` check: + + - Now analyzes calls to the ``starts_with``, ``ends_with``, ``contains``, + and ``operator+=`` string member functions. + + - Fixes false negatives when using ``std::set`` from ``libstdc++``. + - Improved :doc:`readability-container-size-empty <clang-tidy/checks/readability/container-size-empty>` check: diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index ceab1e9414951..2b5be931271ec 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -364,6 +364,7 @@ Clang-Tidy Checks :doc:`performance-noexcept-destructor <performance/noexcept-destructor>`, "Yes" :doc:`performance-noexcept-move-constructor <performance/noexcept-move-constructor>`, "Yes" :doc:`performance-noexcept-swap <performance/noexcept-swap>`, "Yes" + :doc:`performance-prefer-single-char-overloads <performance/prefer-single-char-overloads>`, "Yes" :doc:`performance-string-view-conversions <performance/string-view-conversions>`, "Yes" :doc:`performance-trivially-destructible <performance/trivially-destructible>`, "Yes" :doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "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..bedac6fa78c55 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=prefer-single-char-overloads.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-prefer-single-char-overloads <prefer-single-char-overloads>` +for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst new file mode 100644 index 0000000000000..3e6a9d979497e --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst @@ -0,0 +1,35 @@ +.. title:: clang-tidy - performance-prefer-single-char-overloads + +performance-prefer-single-char-overloads +======================================== + +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/prefer-single-char-overloads.cpp similarity index 94% rename from clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp rename to clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.cpp index 79d6d23f47aa6..46bbf3c20f8fd 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.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-prefer-single-char-overloads %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-prefer-single-char-overloads %t -- \ // RUN: -config="{CheckOptions: \ -// RUN: {performance-faster-string-find.StringLikeClasses: \ +// RUN: {performance-prefer-single-char-overloads.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 efficient 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 efficient overload accepting a character [performance-prefer-single-char-overloads] // CHECK-FIXES: Str.find('a'); // Works with the pos argument. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
