[clang-tools-extra] Add clang tidy check performance constexpr non static in scope (PR #147809)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 12:47:58 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.cpp clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.h clang-tools-extra/test/clang-tidy/checkers/performance/constexpr-non-static-in-scope.cpp clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.cpp b/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.cpp
index fa4ac9de7..f785a7d86 100644
--- a/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.cpp
@@ -14,22 +14,24 @@ using namespace clang::ast_matchers;
namespace clang::tidy::performance {
void ConstexprNonStaticInScopeCheck::registerMatchers(MatchFinder *Finder) {
- Finder->addMatcher(varDecl(
- hasLocalStorage(),
- isConstexpr(),
- unless(isStaticLocal())
- ).bind("constexprVar"), this);
+ Finder->addMatcher(
+ varDecl(hasLocalStorage(), isConstexpr(), unless(isStaticLocal()))
+ .bind("constexprVar"),
+ this);
}
-void ConstexprNonStaticInScopeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+void ConstexprNonStaticInScopeCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "WarnInConstexprFuncCpp23", WarnInConstexprFuncCpp23);
}
-void ConstexprNonStaticInScopeCheck::check(const MatchFinder::MatchResult &Result) {
+void ConstexprNonStaticInScopeCheck::check(
+ const MatchFinder::MatchResult &Result) {
const auto *Var = Result.Nodes.getNodeAs<clang::VarDecl>("constexprVar");
if (!Var)
return;
- const auto *EnclosingFunc = llvm::dyn_cast_or_null<clang::FunctionDecl>(Var->getDeclContext());
+ const auto *EnclosingFunc =
+ llvm::dyn_cast_or_null<clang::FunctionDecl>(Var->getDeclContext());
if (EnclosingFunc && EnclosingFunc->isConstexpr()) {
// If the function is constexpr, only warn in C++23 and above
@@ -37,9 +39,10 @@ void ConstexprNonStaticInScopeCheck::check(const MatchFinder::MatchResult &Resul
return; // Don't warn unless in C++23+ AND the option is enabled
}
- diag(Var->getLocation(),
- "constexpr variable in function scope should be static to ensure static lifetime")
- << FixItHint::CreateInsertion(Var->getSourceRange().getBegin(), "static ");
+ diag(Var->getLocation(), "constexpr variable in function scope should be "
+ "static to ensure static lifetime")
+ << FixItHint::CreateInsertion(Var->getSourceRange().getBegin(),
+ "static ");
}
} // namespace clang::tidy::performance
diff --git a/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.h b/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.h
index 8bc353519..ce6ec1a23 100644
--- a/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.h
+++ b/clang-tools-extra/clang-tidy/performance/ConstexprNonStaticInScopeCheck.h
@@ -21,13 +21,15 @@ class ConstexprNonStaticInScopeCheck : public ClangTidyCheck {
public:
ConstexprNonStaticInScopeCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- WarnInConstexprFuncCpp23(Options.get("WarnInConstexprFuncCpp23", /*Default=*/true)) {}
+ WarnInConstexprFuncCpp23(
+ Options.get("WarnInConstexprFuncCpp23", /*Default=*/true)) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
}
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
private:
const bool WarnInConstexprFuncCpp23;
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/147809
More information about the cfe-commits
mailing list