Skip to content

performance-unnecessary-value-param suggests to preserve unnecessary copy instead of introducing move #57908

Open
@firewave

Description

@firewave
#include <string>

class A
{
public:
	A(std::string s)
	: s_(std::move(s))
	{}
private:
	std::string s_;
};

extern void f1(std::string s) {
	A a{s}; // performance-unnecessary-value-param
}

extern void f3(std::string s) {
    if (!s.empty()) // performance-unnecessary-value-param
    {
	    A a{s}; // no warning
    }
}
<source>:14:6: warning: parameter 's' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param]
        A a{s}; // performance-unnecessary-value-param
            ^
            std::move( )
<source>:17:28: warning: the parameter 's' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]
extern void f3(std::string s) {
                           ^
               const      &

https://godbolt.org/z/x936rcvTd

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions