Open
Description
Clang-Tidy should warn about the following cases of unnecessary copies:
#include <vector>
#include <string>
class A
{
public:
A(std::vector<std::string> v)
: v_(std::move(v))
{}
private:
std::vector<std::string> v_;
};
int main() {
std::vector<std::string> v;
for(int i = 0; i < 10; ++i)
{
v.emplace_back("str");
}
A a{v}; // missing std::move()
return 0;
}
#include <vector>
#include <string>
int main() {
std::vector<std::string> v;
std::string s;
for(int i = 0; i < 10; ++i)
{
s += " ";
}
v.emplace_back(s); // missing std::move()
return 0;
}
#include <vector>
struct A
{
std::vector<int> v;
};
int main() {
std::vector<int> v;
v.emplace_back(1);
A a;
a.v = v; // missing std::move()
return 0;
}
This was causing major performance issue on a project I once worked on. It required a lot of profiling and manual code review to find all occurrences so any pointers by a static code analysis tool would helpful.
This warning only makes sense on non-trivially-copyable types and types without side effects.
For objects with side effects it is not safe to judge if the copy was intended or not - even if the object is not used afterwards.