AutosarC++17_10-A7.1.7¶
Each expression statement and identifier declaration shall be placed on a separate line
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 [8]: 7.1.1 Declare each identifier on a separate line in a separate declaration.
- JSF December 2005 [7]: AV Rule 42 Each expression-statement will be on a separate line.
- C++ Core Guidelines [10]: NL.20: Don’t place two statements on the same line.
- C++ Core Guidelines [10]: C.7: Don’t define a class or enum and declare a variable of its type in the same statement.
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_in_for_loop_init¶
allow_in_for_loop_init : bool = True
allow_uninitialized_simple_type¶
allow_uninitialized_simple_type : bool = False
ignore_stmts¶
ignore_stmts : set[bauhaus.ir.PIR_Class_Name] = {'Attributed_Statement', 'C_For_Loop', 'General_Statement_Sequence'}