@@ -911,18 +911,43 @@ void UseConstexprCheck::onEndOfTranslationUnit() {
911911 Diag << FixItHint::CreateInsertion (FDecl->getInnerLocStart (),
912912 FunctionReplacement);
913913 }
914- const std::string VariableReplacement = ConstexprString + " " ;
914+
915+ const std::string VariableReplacementWithStatic = StaticConstexprString + " " ;
916+ const auto VariableReplacement =
917+ [&FunctionReplacement, this , &VariableReplacementWithStatic](
918+ const VarDecl *Var, const FunctionDecl *FuncCtx,
919+ const bool IsAddingConstexprToFuncCtx) -> const std::string & {
920+ if (!FuncCtx)
921+ return FunctionReplacement;
922+
923+ if (!getLangOpts ().CPlusPlus23 )
924+ return FunctionReplacement;
925+
926+ // We'll prefer the function to be constexpr over the function not being
927+ // constexpr just for the var to be static constexpr instead of just
928+ // constexpr.
929+ if (IsAddingConstexprToFuncCtx)
930+ return FunctionReplacement;
931+
932+ if (Var->isStaticLocal ())
933+ return FunctionReplacement;
934+
935+ return VariableReplacementWithStatic;
936+ };
937+
915938 for (const auto &[Var, FuncCtx] : VariableMapping) {
939+ const auto IsAddingConstexprToFuncCtx = Functions.contains (FuncCtx);
916940 if (FuncCtx && getLangOpts ().CPlusPlus23 && Var->isStaticLocal () &&
917- Functions. contains (FuncCtx) )
941+ IsAddingConstexprToFuncCtx )
918942 continue ;
919943 const SourceRange R =
920944 SourceRange (Var->getInnerLocStart (), Var->getLocation ());
921945 auto Diag =
922946 diag (Var->getLocation (), " variable %0 can be declared 'constexpr'" )
923947 << Var << R
924- << FixItHint::CreateInsertion (Var->getInnerLocStart (),
925- VariableReplacement);
948+ << FixItHint::CreateInsertion (
949+ Var->getInnerLocStart (),
950+ VariableReplacement (Var, FuncCtx, IsAddingConstexprToFuncCtx));
926951 // Since either of the locs can be in a macro, use `makeFileCharRange` to be
927952 // sure that we have a consistent `CharSourceRange`, located entirely in the
928953 // source file.
@@ -947,11 +972,14 @@ UseConstexprCheck::UseConstexprCheck(StringRef Name, ClangTidyContext *Context)
947972 ConservativeLiteralType (Options.get (" ConservativeLiteralType" , true )),
948973 AddConstexprToMethodOfClassWithoutConstexprConstructor (Options.get (
949974 " AddConstexprToMethodOfClassWithoutConstexprConstructor" , false )),
950- ConstexprString (Options.get (" ConstexprString" , " constexpr" )) {}
975+ ConstexprString (Options.get (" ConstexprString" , " constexpr" )),
976+ StaticConstexprString (
977+ Options.get (" StaticConstexprString" , " static " + ConstexprString)) {}
951978void UseConstexprCheck::storeOptions (ClangTidyOptions::OptionMap &Opts) {
952979 Options.store (Opts, " ConservativeLiteralType" , ConservativeLiteralType);
953980 Options.store (Opts, " AddConstexprToMethodOfClassWithoutConstexprConstructor" ,
954981 AddConstexprToMethodOfClassWithoutConstexprConstructor);
955982 Options.store (Opts, " ConstexprString" , ConstexprString);
983+ Options.store (Opts, " StaticConstexprString" , StaticConstexprString);
956984}
957985} // namespace clang::tidy::modernize
0 commit comments