Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1659 Multiple variables should not be declared on the same line
  3. RSPEC-1004

C-Family: Init-declarator-lists and member-declarator-lists should consist of single init-declarators and member-declarators respectively

    Details

    • Type: Language-Specification
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Define each identifier in a dedicated statement.
    • Highlighting:
      • Primary: second identifier
      • Additional: additional identifiers
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Legacy Key:
      SingleDeclarationPerStatement
    • Constant Cost:
      5min

      Description

      Where multiple declarators appear in the same declaration the type of an identifier may not meet developer expectations.

      Noncompliant Code Example

      int i1; int j1; // Compliant, but not preferred
      int i2, *j2; // Noncompliant
      int *i3,                  
          &j3 = i2; // Noncompliant
      

      Compliant Solution

      int i1; 
      int j1;
      int i2;
      int *j2;
      int *i3;
      int &j3 = i2;
      

      See

      • MISRA C++:2008, 8-0-1 - An init-declarator-list or a member-declarator-list shall consist of a single init-declarator or member-declarator respectively
      • CERT, DCL52-J. - Do not declare more than one variable per declaration
      • CERT, DCL04-C. - Do not declare more than one variable per declaration
      • C++ Core Guidelines - ES.10 - Declare one name (only) per declaration

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: