Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-3020

Aggregates can be constructed directly via emplace functions

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.26
    • Component/s: C++, Rules
    • Labels:

      Description

      With C++20 using X{args...} to construct aggregates before passing to emplace functions, like make_unique, make_shared, emplace is no longer necessary, and they can be constructed directly:

      struct A { int x; int y; };
      auto p = std::make_shared<A>(A{1, 2}); // non-compliant
      auto t = std::make_shared<A>(1, 2);  // non-compliant
      

      Update existing rules to also suggest replacement in when aggregate is involved:

      1. RSPEC-6011 - apply to aggregates
      2. RSPEC-5950 - replace std::unique_ptr<A> a(new A{1, 2}); with make_unique as it is now possible
      3. RSPEC-6003 - replace v.insert({1,2}) or v.insert(A{1,2}) on {{ std::vector<A> v; }} with emplace

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              tomasz.kaminski Tomasz Kamiński
              Reporter:
              tomasz.kaminski Tomasz Kamiński
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: