-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
e919320
30f9c79
04f6d42
e9a49a2
32cb572
5c02c19
d7813b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -172,7 +172,8 @@ Changes in existing checks | |||||
|
||||||
- Improved :doc:`performance-unnecessary-copy-initialization | ||||||
<clang-tidy/checks/performance/unnecessary-copy-initialization>` by printing | ||||||
the type of the diagnosed variable. | ||||||
the type of the diagnosed variable and adding detection for local variables | ||||||
initialized with a member variable of a const object. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
- Improved :doc:`performance-unnecessary-value-param | ||||||
<clang-tidy/checks/performance/unnecessary-value-param>` by printing | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -938,3 +938,59 @@ template<typename T> bool OperatorWithNoDirectCallee(T t) { | |||||
ExpensiveToCopyType a2 = a1; | ||||||
return a1 == t; | ||||||
} | ||||||
|
||||||
bool CopiedFromParmVarField(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 subobject 'crs.Member' of type 'const ExpensiveToCopyType' 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 subobject 'cs.Member' of type 'const ExpensiveToCopyType' 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 CopiedFromVarField() { | ||||||
const Struct crs; | ||||||
Struct s; | ||||||
const auto m1 = crs.Member; | ||||||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'crs.Member' of type 'const ExpensiveToCopyType' 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 subobject 'GlobalS.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy | ||||||
// CHECK-FIXES: const auto& m3 = GlobalS.Member; | ||||||
return m1 == m2 || m2 == m3; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add tests for chained member expressions: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your tips. I have added support for chain member expressions. |
||||||
|
||||||
struct NestedStruct { | ||||||
Struct s; | ||||||
}; | ||||||
|
||||||
bool CopiedFromParmVarNestedField(const NestedStruct &ncrs, const NestedStruct ncs, NestedStruct &nrs, NestedStruct ns) { | ||||||
const auto m1 = ncrs.s.Member; | ||||||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'ncrs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// CHECK-FIXES: const auto& m1 = ncrs.s.Member; | ||||||
const auto m2 = ncs.s.Member; | ||||||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the subobject 'ncs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy | ||||||
// CHECK-FIXES: const auto& m2 = ncs.s.Member; | ||||||
const auto m3 = nrs.s.Member; | ||||||
const auto m4 = ns.s.Member; | ||||||
return m1 == m2 || m3 == m4; | ||||||
} | ||||||
|
||||||
const NestedStruct GlobalNS; | ||||||
bool CopiedFromVarNestedField() { | ||||||
const NestedStruct ncrs; | ||||||
NestedStruct ns; | ||||||
const auto m1 = ncrs.s.Member; | ||||||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'ncrs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy | ||||||
// CHECK-FIXES: const auto& m1 = ncrs.s.Member; | ||||||
const auto m2 = ns.s.Member; | ||||||
const auto m3 = GlobalNS.s.Member; | ||||||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the subobject 'GlobalNS.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy | ||||||
// CHECK-FIXES: const auto& m3 = GlobalNS.s.Member; | ||||||
return m1 == m2 || m2 == m3; | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, the
OldVar
parameter can be removed from this function and fromhandleCopyFromConstVarMember
.