[PATCH] D87455: Restrict UnnecessaryCopyInitialization check to variables initialized from methods/functions without arguments
Felix Berger via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 10 07:56:19 PDT 2020
flx created this revision.
flx added reviewers: alexfh, sbenza.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
flx requested review of this revision.
This restriction avoids cases where an alias is returned to an argument and which could lead to to a false positive change. Example:
struct S {
S(const S&);
void modify();
};
const S& alias(const S& a) {
return a;
}
void f(S a) {
const S c = alias(a);
a.modify();
}
Taking a const reference of the return value of alias would not be safe since a is modified.
Still allow methods and functions with default arguments if they are called with their default arguments.
A more involved alternative approach would be to inspect each argument passed to these functions for its constness, but this can get complex quickly.
As I'm writing this I'm realizing though that this precludes catching repeated protobuff messages:
Message m;
const auto field = m.repeated_field(0);
Maybe this approach should be refined to allowing functions and methods that take all parameters by value?
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D87455
Files:
clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp
clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.h
clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87455.290971.patch
Type: text/x-patch
Size: 6680 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200910/25f6979e/attachment.bin>
More information about the cfe-commits
mailing list