cpp:S6003 false positive

  • What language is this for?
    C++
  • Which rule?
    cpp:S6003
  • Why do you believe it’s a false-positive/false-negative?
    When push_back is used in conjunction with designated initializers, it is just not possible to replace the push_back call in an equivalent way with emplace_back due to C++ syntax limitations. Therefore it would make more sense not to flag this as a violation.
  • Are you using
    • SonarQube - which version?
      Enterprise Edition Version 9.9 (build 65466)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
#include <vector>

struct X {
    int a;
    int b;
    int pad[100];
};

int main()
{
    std::vector<X> vec;
    vec.push_back(X{.a{1}, .b{2}});
}

Compiler Explorer (here also the quick fix is wrong - if you apply it, it won’t compile any more)

Hi Phillip,

Yes, this is indeed a false positive, and S6003 should ignore insertions of objects constructed using designated initializers.

I have created a ticket on our Jira so you can track the progress of this false positive.

Cheers