Issue 133734
Summary [clang-tidy] Check for uncessary copies as return value for getter functions
Labels clang-tidy
Assignees
Reporter ArnaudBienner
    The following code:
```
std::string GetValue() const { return mValue; }
```
(with `mValue` being a `std::string` member)

is not optimal since it might perform an unnecessary copy of the string object, which might not be needed.

The following code would be best:
```
const std::string& GetValue() const { return mValue; }
```

Similar to performance-unnecessary-value-param[ΒΆ](https://clang.llvm.org/extra/clang-tidy/checks/performance/unnecessary-value-param.html#performance-unnecessary-value-param) I think it would be nice to have a check for this.

What do you think?

Probably only if the method is not virtual, since I can imagine cases where children classes might not always be able to return a const reference.

I guess some people might still prefer to return a copy, to avoid callers to potentially mess up things using `const_cast`, but for most users, I think it would be nice to have this check.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to