AutosarC++19_03-A7.1.7

Each expression statement and identifier declaration shall be placed on a separate line

Required inputs: IR

Rationale

Declaring an identifier on a separate line makes the identifier declaration easier to find and the source code more readable. Also, combining objects, references and pointers declarations with assignments and function calls on the same line may become confusing.

Exceptions

It is permitted to declare identifiers in initialization statement of a for loop.

Example
// $Id: A7-1-7.cpp 292454 2017-10-23 13:14:23Z michal.szczepankiewicz $
    #include <cstdint>
    #include <vector>

    typedef std::int32_t* ptr;                   // Compliant
    typedef std::int32_t *pointer, value;        // Non-compliant

    void Fn1() noexcept
    {
        std::int32_t x = 0;                       // Compliant
        std::int32_t y = 7, *p1 = nullptr;        // Non-compliant
        std::int32_t const *p2, z = 1;            // Non-compliant
    }

    void Fn2()
    {
        std::vector<std::int32_t> v{1, 2, 3, 4, 5};
        for (auto iter{v.begin()}, end{v.end()}; iter != end;
             ++iter) // Compliant by exception
        {
            // ...
        }
    }

    void Fn3() noexcept
    {
          std::int32_t x{5};
          std::int32_t y{15}; // Non-compliant
          x++;
          ++y; // Non-compliant
          for (std::int32_t i{0}; i < 100; ++i)
          {
               Fn2(); // Compliant
          }
    }
See Also
  • HIC++ v4.0 [9]: 7.1.1 Declare each identifier on a separate line in a separate declaration.
  • JSF December 2005 [8]: AV Rule 42 Each expression-statement will be on a separate line.
  • JSF December 2005 [8]: AV Rule 152: Multiple variable declarations shall not be allowed on the same line.
  • C++ Core Guidelines [11]: NL.20: Don’t place two statements on the same line.

Possible Messages

Key

Text

Severity

Disabled

multi_declaration

Multiple declarators in single declaration

None

False

multiple_statements_per_line

Multiple statements per line.

None

False

Options

allow_in_for_loop_init

allow_in_for_loop_init : bool = True

Whether a multi-declaration is allowed in the for-init statement
 

allow_uninitialized_simple_type

allow_uninitialized_simple_type : bool = False

Whether a multi-declaration of simple type is allowed when there is no initialization
 

ignore_stmts

ignore_stmts : set[bauhaus.ir.PIR_Class_Name] = {'Attributed_Statement', 'C_For_Loop', 'General_Statement_Sequence'}

Statements to be ignored when counting statements.