llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.85k stars 11.47k forks source link

[Clang-Tidy] Create performance-proxy-object-copy check #80859

Open PiotrZSL opened 7 months ago

PiotrZSL commented 7 months ago

I'm adding this here so, i wouldn't forget.

Example:

struct A
{
    const std::string& getS() { return s; }
    std::string s;
};

struct AProxy
{
    AProxy(A& a) : a(a) {}
    std::string getS() { return s.getS(); }
    A& a;
};

Should detect copies of nontrivial heavy objects returned from a call as const reference, but returned as value. This can be common mistake when writing proxies, and then changing only 1 interface instead of two. This can be expected behaviour when writing proxies that add something (like mutex locks), in such case behavior can be expected, so best would be to detect only functions that have only returnStmt.

Check name is up to negotation.

llvmbot commented 7 months ago

@llvm/issue-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

I'm adding this here so, i wouldn't forget. Example: ``` struct A { const std::string& getS() { return s; } std::string s; }; struct AProxy { AProxy(A& a) : a(a) {} std::string getS() { return s.getS(); } A& a; }; ``` Should detect copies of nontrivial heavy objects returned from a call as const reference, but returned as value. This can be common mistake when writing proxies, and then changing only 1 interface instead of two. This can be expected behaviour when writing proxies that add something (like mutex locks), in such case behavior can be expected, so best would be to detect only functions that have only returnStmt. Check name is up to negotation.