Skip to content

[clang-tidy] Check for uncessary copies as return value for getter functions #133734

@ArnaudBienner

Description

@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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions