Skip to content

[clang-tidy] performance-unnecessary-copy-initialization: Enhance the check for the scenario with MemberExpr initialization. #151936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(LocalVarCopiedFrom(declRefExpr(
to(varDecl(hasLocalStorage()).bind(OldVarDeclId)))),
this);

Finder->addMatcher(
LocalVarCopiedFrom(memberExpr(
hasObjectExpression(expr(ignoringParenImpCasts(declRefExpr(
to(varDecl(anyOf(hasType(isConstQualified()),
hasType(references(isConstQualified()))))
.bind(OldVarDeclId)))))),
member(fieldDecl().bind("fieldDecl")))),
this);
}

void UnnecessaryCopyInitialization::check(
Expand All @@ -294,6 +303,7 @@ void UnnecessaryCopyInitialization::check(
IssueFix, IsVarUnused, IsVarOnlyUsedAsConst};
const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>(OldVarDeclId);
const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>(ObjectArgId);
const auto *FD = Result.Nodes.getNodeAs<FieldDecl>("fieldDecl");
const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");

TraversalKindScope RAII(*Result.Context, TK_AsIs);
Expand All @@ -318,9 +328,12 @@ void UnnecessaryCopyInitialization::check(
if (OldVar == nullptr) {
// `auto NewVar = functionCall();`
handleCopyFromMethodReturn(Context, ObjectArg);
} else {
} else if (FD == nullptr) {
// `auto NewVar = OldVar;`
handleCopyFromLocalVar(Context, *OldVar);
} else {
// `auto NewVar = OldVar.FD;`
handleCopyFromConstLocalVarMember(Context, *OldVar);
}
}

Expand All @@ -345,6 +358,11 @@ void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
diagnoseCopyFromLocalVar(Ctx, OldVar);
}

void UnnecessaryCopyInitialization::handleCopyFromConstLocalVarMember(
const CheckContext &Ctx, const VarDecl &OldVar) {
diagnoseCopyFromConstLocalVarMember(Ctx, OldVar);
}

void UnnecessaryCopyInitialization::diagnoseCopyFromMethodReturn(
const CheckContext &Ctx) {
auto Diagnostic =
Expand All @@ -369,6 +387,18 @@ void UnnecessaryCopyInitialization::diagnoseCopyFromLocalVar(
maybeIssueFixes(Ctx, Diagnostic);
}

void UnnecessaryCopyInitialization::diagnoseCopyFromConstLocalVarMember(
const CheckContext &Ctx, const VarDecl &OldVar) {
auto Diagnostic =
diag(Ctx.Var.getLocation(),
"local copy %1 of the field of the variable %0 is never "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message would be more understandable if it mentioned the name of the field. Something like:

local copy 'foo' of the field 'bar' of the variable 'baz' is never modified [...]
                              ^^^^^

or:

local copy 'foo' of the subobject 'baz.bar' is never modified [...]
                        ^^^^^^^^^^^^^^^^^^^

"modified%select{"
"| and never used}2; consider %select{avoiding the copy|removing "
"the statement}2")
<< &OldVar << &Ctx.Var << Ctx.IsVarUnused;
maybeIssueFixes(Ctx, Diagnostic);
}

void UnnecessaryCopyInitialization::maybeIssueFixes(
const CheckContext &Ctx, DiagnosticBuilder &Diagnostic) {
if (Ctx.IssueFix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ class UnnecessaryCopyInitialization : public ClangTidyCheck {
virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx);
virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx,
const VarDecl &OldVar);
virtual void diagnoseCopyFromConstLocalVarMember(const CheckContext &Ctx,
const VarDecl &OldVar);

private:
void handleCopyFromMethodReturn(const CheckContext &Ctx,
const VarDecl *ObjectArg);
void handleCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar);

void handleCopyFromConstLocalVarMember(const CheckContext &Ctx,
const VarDecl &OldVar);

void maybeIssueFixes(const CheckContext &Ctx, DiagnosticBuilder &Diagnostic);

const std::vector<StringRef> AllowedTypes;
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Changes in existing checks
when the format string is converted to a different type by an implicit
constructor call.

- Improved :doc:`performance-unnecessary-copy-initialization
<clang-tidy/checks/performance/unnecessary-copy-initialization>` check by
adding detection for the local variables initialized with the member variable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
adding detection for the local variables initialized with the member variable
adding detection for local variables initialized with a member variable

of a const object.

- Improved :doc:`portability-template-virtual-member-function
<clang-tidy/checks/portability/template-virtual-member-function>` check to
avoid false positives on pure virtual member functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,28 @@ template<typename T> bool OperatorWithNoDirectCallee(T t) {
return a1 == t;
}

bool CopiedFromConstRefParmVar(const Struct &crs, const Struct cs, Struct &rs, Struct s) {
const auto m1 = crs.Member;
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field of the variable 'crs' is never modified; consider avoiding the copy
// CHECK-FIXES: const auto& m1 = crs.Member;
const auto m2 = cs.Member;
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the field of the variable 'cs' is never modified; consider avoiding the copy
// CHECK-FIXES: const auto& m2 = cs.Member;
const auto m3 = rs.Member;
const auto m4 = s.Member;
return m1 == m2 || m3 == m4;
}

const Struct GlobalS;
bool CopiedFromConstLocalVar() {
const Struct crs;
Struct s;
const auto m1 = crs.Member;
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field of the variable 'crs' is never modified; consider avoiding the copy
// CHECK-FIXES: const auto& m1 = crs.Member;
const auto m2 = s.Member;
const auto m3 = GlobalS.Member;
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the field of the variable 'GlobalS' is never modified; consider avoiding the copy
// CHECK-FIXES: const auto& m3 = GlobalS.Member;
return m1 == m2 || m2 == m3;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add tests for chained member expressions: const auto copy = Var.Member1.Member2;