False positive with cppcheck:passedByValue and std::move in constructor

  • What language is this for?
    C++
  • Which rule?
    cppcheck:passedByValue
  • Why do you believe it’s a false-positive/false-negative?
    std::move should be more efficient than passing by const reference as suggested
  • Are you using
    • SonarCloud?
    • SonarQube - which version?
      SonarQube 9.9
    • SonarLint - which IDE/version?
      • in connected mode with SonarQube or SonarCloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
#include <string> // Include the <string> header for std::string

class CheckActionAggregator
{
public:
    // Constructor with a string parameter
    explicit CheckActionAggregator(std::string pName)
        : mName(std::move(pName)) // Initialize mName using std::move
    {
        // No additional code here
    }

    // Add any other member functions or variables as needed

private:
    std::string mName;
    // You might want to declare mLogger here if it's a member of CheckActionAggregator
};

int main()
{
    // Create an instance of CheckActionAggregator with a name
    CheckActionAggregator myAggregator("My Check Action");

    // You can now use myAggregator as needed

    return 0;
}

The rule will be triggered on the constructor even though there is the std::move which is more efficient than passing by const reference

I found the same false positive in cppcheck
https://trac.cppcheck.net/ticket/8570

Can you confirm it’s a false positive?
Thanks

Hey there.

This issue is entirely being raised by cppcheck and imported into SonarQube via the community-supported GitHub - SonarOpenCommunity/sonar-cxx: SonarQube C++ Community plugin (cxx plugin): This plugin adds C++ support to SonarQube with the focus on integration of existing C++ tools.. There’s not much we can comment on!

Once it’s fixed in cppcheck, it will be fixed in your instance (or whenever sonar-cxx updates cppcheck, if that’s how the plugin works)

@Colin Oh ok, sorry for the wrong report then, thanks for the explanation.

Hi @Lusheez,

Please note that our own analyzer comes with a rule similar to the rule you mention in cppcheck.

I tried it on your code and, as expected, it is not triggered.

Our analyzer for C++ is available in SonarQube starting with the developer edition.

2 Likes