diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -69,8 +69,8 @@ struct NamingStyle { NamingStyle() = default; - NamingStyle(llvm::Optional Case, const std::string &Prefix, - const std::string &Suffix, const std::string &IgnoredRegexpStr, + NamingStyle(llvm::Optional Case, StringRef Prefix, + StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType); NamingStyle(const NamingStyle &O) = delete; NamingStyle &operator=(NamingStyle &&O) = default; diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -228,9 +228,8 @@ // clang-format on IdentifierNamingCheck::NamingStyle::NamingStyle( - llvm::Optional Case, - const std::string &Prefix, const std::string &Suffix, - const std::string &IgnoredRegexpStr, HungarianPrefixType HPType) + llvm::Optional Case, StringRef Prefix, + StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType) : Case(Case), Prefix(Prefix), Suffix(Suffix), IgnoredRegexpStr(IgnoredRegexpStr), HPType(HPType) { if (!IgnoredRegexpStr.empty()) { @@ -263,22 +262,21 @@ memcpy(&StyleString[StyleSize], "IgnoredRegexp", 13); StyleString.truncate(StyleSize + 13); - StringRef IgnoredRegexpStr = Options.get(StyleString, ""); + Optional IgnoredRegexpStr = Options.get(StyleString); memcpy(&StyleString[StyleSize], "Prefix", 6); StyleString.truncate(StyleSize + 6); - std::string Prefix(Options.get(StyleString, "")); + Optional Prefix(Options.get(StyleString)); // Fast replacement of [Pre]fix -> [Suf]fix. memcpy(&StyleString[StyleSize], "Suf", 3); - std::string Postfix(Options.get(StyleString, "")); + Optional Postfix(Options.get(StyleString)); memcpy(&StyleString[StyleSize], "Case", 4); StyleString.pop_back_n(2); - auto CaseOptional = + Optional CaseOptional = Options.get(StyleString); - if (CaseOptional || !Prefix.empty() || !Postfix.empty() || - !IgnoredRegexpStr.empty() || HPTOpt) - Styles[I].emplace(std::move(CaseOptional), std::move(Prefix), - std::move(Postfix), IgnoredRegexpStr.str(), + if (CaseOptional || Prefix || Postfix || IgnoredRegexpStr || HPTOpt) + Styles[I].emplace(std::move(CaseOptional), Prefix.getValueOr(""), + Postfix.getValueOr(""), IgnoredRegexpStr.getValueOr(""), HPTOpt.value_or(IdentifierNamingCheck::HPT_Off)); } bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -234,6 +234,11 @@ - Fixed incorrect suggestions for :doc:`readability-container-size-empty ` when smart pointers are involved. +- Fixed an issue in :doc:`readability-identifier-naming + ` when specifying an empty + string for ``Prefix`` or ``Suffix`` options could result in the style not + being used. + - Fixed a false positive in :doc:`readability-non-const-parameter ` when the parameter is referenced by an lvalue. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-empty-options.cpp @@ -0,0 +1,12 @@ +// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-identifier-naming.GlobalConstantPrefix: "", \ +// RUN: readability-identifier-naming.GlobalVariablePrefix: g_ }}' + +int BadGlobalVariable; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for global variable 'BadGlobalVariable' [readability-identifier-naming] +// CHECK-FIXES: int g_BadGlobalVariable; +int g_GoodGlobalVariable; + +const int GoodGlobalConstant = 0; +const int g_IgnoreGlobalConstant = 0;