Skip to content

Commit 1e3f6a6

Browse files
authored
[clang-tidy][NFC] Prefer constexpr llvm::StringLiteral over const char * (#147301)
Some of these are even global mutable state — probably not what was intended! ```cpp static const char *AnalyzerCheckNamePrefix = "clang-analyzer-"; ```
1 parent e976eaf commit 1e3f6a6

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ namespace clang::tidy {
5555

5656
namespace {
5757
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
58-
static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
58+
static constexpr llvm::StringLiteral AnalyzerCheckNamePrefix =
59+
"clang-analyzer-";
5960

6061
class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
6162
public:
@@ -351,10 +352,9 @@ ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
351352
static void
352353
setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
353354
clang::AnalyzerOptions &AnalyzerOptions) {
354-
StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
355355
for (const auto &Opt : Opts.CheckOptions) {
356356
StringRef OptName(Opt.getKey());
357-
if (!OptName.consume_front(AnalyzerPrefix))
357+
if (!OptName.consume_front(AnalyzerCheckNamePrefix))
358358
continue;
359359
// Analyzer options are always local options so we can ignore priority.
360360
AnalyzerOptions.Config[OptName] = Opt.getValue().Value;
@@ -476,7 +476,8 @@ std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
476476
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
477477
for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
478478
Context, Context.canEnableAnalyzerAlphaCheckers()))
479-
CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
479+
CheckNames.emplace_back(
480+
(AnalyzerCheckNamePrefix + AnalyzerCheck.first).str());
480481
#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
481482

482483
llvm::sort(CheckNames);

clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ AST_POLYMORPHIC_MATCHER(
4646
if (PrefixPosition == StringRef::npos)
4747
return false;
4848
Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
49-
static const char *AbseilLibraries[] = {
49+
static constexpr llvm::StringLiteral AbseilLibraries[] = {
5050
"algorithm", "base", "container", "debugging", "flags",
5151
"hash", "iterator", "memory", "meta", "numeric",
5252
"profiling", "random", "status", "strings", "synchronization",
5353
"time", "types", "utility"};
54-
return llvm::any_of(AbseilLibraries, [&](const char *Library) {
54+
return llvm::any_of(AbseilLibraries, [&](llvm::StringLiteral Library) {
5555
return Path.starts_with(Library);
5656
});
5757
}

clang-tools-extra/clang-tidy/bugprone/ComparePointerToMemberVirtualFunctionCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace {
2525

2626
AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }
2727

28-
static const char *const ErrorMsg =
28+
static constexpr llvm::StringLiteral ErrorMsg =
2929
"comparing a pointer to member virtual function with other pointer is "
3030
"unspecified behavior, only compare it with a null-pointer constant for "
3131
"equality.";

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ static bool isEmpty(ASTContext &Context, const QualType &Type) {
373373
return isIncompleteOrZeroLengthArrayType(Context, Type);
374374
}
375375

376-
static const char *getInitializer(QualType QT, bool UseAssignment) {
377-
const char *DefaultInitializer = "{}";
376+
static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
377+
static constexpr llvm::StringLiteral DefaultInitializer = "{}";
378378
if (!UseAssignment)
379379
return DefaultInitializer;
380380

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
119119
if (StartLoc.isMacroID() && IgnoreMacros)
120120
return;
121121

122-
static const char *UseUsingWarning = "use 'using' instead of 'typedef'";
122+
static constexpr llvm::StringLiteral UseUsingWarning =
123+
"use 'using' instead of 'typedef'";
123124

124125
// Warn at StartLoc but do not fix if there is macro or array.
125126
if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) {

0 commit comments

Comments
 (0)