Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3780

Class members should not be const references to string

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Refactor this code to avoid using a const reference to a string.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Targeted languages:
      C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      It is unsafe to have const string reference members as they can be created from a temporary quite easily.

      struct Foo {
        Foo(const string &Str) : Str(Str) {}
        const string &Str;
      };
      Foo instance("string");
      

      When the constructor is called, a string temporary is created from const char * and destroyed immediately after. This results in a dangling reference.

      This rule applies for both std::string and ::string const reference members.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: