[clang-tools-extra] [clang-tidy] Extend modernize-pass-by-value to handle function body local copies (PR #182024)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 09:19:40 PST 2026
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 origin/main HEAD --extensions cpp -- clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value-local-copy.cpp clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
index eb1e9aa97..c260bcc48 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
@@ -169,8 +169,7 @@ static bool hasRValueOverload(const FunctionDecl *Func,
const bool IsLValueRValuePair =
FuncParamType->isLValueReferenceType() &&
CandidateParamType->isRValueReferenceType() &&
- CandidateParamType->getPointeeType()
- ->getUnqualifiedDesugaredType() ==
+ CandidateParamType->getPointeeType()->getUnqualifiedDesugaredType() ==
FuncParamType->getPointeeType()->getUnqualifiedDesugaredType();
if (I == ParamIdx) {
// The parameter of interest must be paired.
@@ -285,8 +284,8 @@ void PassByValueCheck::registerMatchers(MatchFinder *Finder) {
hasArgument(
0,
ignoringImplicit(declRefExpr(to(
- parmVarDecl(hasType(
- notTemplateSpecConstRefType()))
+ parmVarDecl(
+ hasType(notTemplateSpecConstRefType()))
.bind("FuncParam")))))))))
.bind("LocalVar"))))
.bind("Func")),
@@ -303,10 +302,11 @@ void PassByValueCheck::registerPPCallbacks(const SourceManager &SM,
/// Attempts to rewrite the const-ref parameter declarations to pass-by-value
/// across all redeclarations. Returns true if fixits were added, false if
/// rewriting is not possible (e.g. type hidden behind a typedef).
-static bool rewriteParamDeclsToValue(
- const FunctionDecl *Func, const ParmVarDecl *ParamDecl,
- const SourceManager &SM, const LangOptions &LangOpts,
- DiagnosticBuilder &Diag) {
+static bool rewriteParamDeclsToValue(const FunctionDecl *Func,
+ const ParmVarDecl *ParamDecl,
+ const SourceManager &SM,
+ const LangOptions &LangOpts,
+ DiagnosticBuilder &Diag) {
if (!ParamDecl->getType()->isLValueReferenceType())
return true; // Already by value, nothing to rewrite.
@@ -328,11 +328,10 @@ static bool rewriteParamDeclsToValue(
const TypeLoc ValueTL = RefTL.getPointeeLoc();
const CharSourceRange TypeRange = CharSourceRange::getTokenRange(
ParmDecl->getBeginLoc(), ParamTL.getEndLoc());
- std::string ValueStr =
- Lexer::getSourceText(
- CharSourceRange::getTokenRange(ValueTL.getSourceRange()), SM,
- LangOpts)
- .str();
+ std::string ValueStr = Lexer::getSourceText(CharSourceRange::getTokenRange(
+ ValueTL.getSourceRange()),
+ SM, LangOpts)
+ .str();
ValueStr += ' ';
Diag << FixItHint::CreateReplacement(TypeRange, ValueStr);
}
@@ -412,8 +411,7 @@ void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {
if (!HasUsableMove && !Record->needsImplicitMoveConstructor())
return;
- auto Diag =
- diag(ParamDecl->getBeginLoc(), "pass by value and use std::move");
+ auto Diag = diag(ParamDecl->getBeginLoc(), "pass by value and use std::move");
if (!rewriteParamDeclsToValue(Func, ParamDecl, SM, getLangOpts(), Diag))
return;
``````````
</details>
https://github.com/llvm/llvm-project/pull/182024
More information about the cfe-commits
mailing list