[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Enhance the check for the scenario with MemberExpr initialization. (PR #151936)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 7 08:11:30 PDT 2025
================
@@ -371,6 +392,21 @@ void UnnecessaryCopyInitialization::diagnoseCopyFromLocalVar(
maybeIssueFixes(Ctx, Diagnostic);
}
+void UnnecessaryCopyInitialization::diagnoseCopyFromConstVarMember(
+ const CheckContext &Ctx, const VarDecl &OldVar, const MemberExpr &ME) {
+ std::string MEStr(Lexer::getSourceText(
+ CharSourceRange::getTokenRange(ME.getSourceRange()),
+ Ctx.ASTCtx.getSourceManager(), Ctx.ASTCtx.getLangOpts()));
+ auto Diagnostic =
+ diag(Ctx.Var.getLocation(),
+ "local copy %0 of the subobject '%1' of type %2 is never "
+ "modified%select{"
+ "| and never used}4; consider %select{avoiding the copy|removing "
+ "the statement}4")
+ << &Ctx.Var << MEStr << Ctx.Var.getType() << &OldVar << Ctx.IsVarUnused;
----------------
localspook wrote:
```suggestion
"local copy %0 of the subobject '%1' of type %2 is never "
"modified%select{"
"| and never used}3; consider %select{avoiding the copy|removing "
"the statement}3")
<< &Ctx.Var << MEStr << Ctx.Var.getType() << Ctx.IsVarUnused;
```
Then, the `OldVar` parameter can be removed.
https://github.com/llvm/llvm-project/pull/151936
More information about the cfe-commits
mailing list