AutosarC++19_03-A2.7.2¶
Sections of code shall not be “commented out”
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | non-automated |
Rationale
Comments, using both C-style and C++ comments, should only be used to explain aspect of the source code. Code that is commented-out may become out of date, which may lead to confusion while maintaining the code.
Additionally, C-style comment markers do not support nesting, and for this purpose commenting out code is dangerous, see: M2-7-1.
Note that the code that is a part of a comment (e.g. for clarification of the usage of the function, for specifying function behavior) does not violate this rule. As it is not possible to determine if a commented block is a textual comment, a code example or a commented-out piece of code, this rule is not enforceable by static analysis tools.
Example
// $Id: A2-7-2.cpp 305382 2018-01-26 06:32:15Z michal.szczepankiewicz $
#include <cstdint>
void Fn1() noexcept
{
std::int32_t i = 0;
// /*
// * ++i; /* incrementing the variable i */
// */ // Non-compliant - C-style comments nesting is not supported,
// compilation error
for (; i < 10; ++i)
{
// ...
}
}
void Fn2() noexcept
{
std::int32_t i = 0;
// ++i; // Incrementing the variable i // Non-compliant - code should not
// be commented-out
for (; i < 10; ++i)
{
// ...
}
}
void Fn3() noexcept
{
std::int32_t i = 0;
++i; // Incrementing the variable i using ++i syntax // Compliant - code
// is not commented-out, but ++i occurs in a
// comment too
for (; i < 10; ++i)
{
// ...
}
}
See Also
- MISRA C++ 2008 [7]: Rule 2-7-2 Sections of code shall not be “commented out” using C-style comments.
- MISRA C++ 2008 [7]: Rule 2-7-3 Sections of code should not be “commented out” using C++ comments.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
code_in_c_comment |
Sections of code should not be commented out. |
None |
False |
code_in_cpp_comment |
Sections of code should not be commented out. |
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
doxygen_start¶
doxygen_start : set[str] = {'/**', '///'}
files_to_check¶
files_to_check : bauhaus.rules.axivion.lexical.comments.FileToCheckSet = {'Primary_File', 'System_Include_File', 'User_Include_File'}
forbidden_tokens¶
forbidden_tokens : set[str] = {'/*', '// '}
This is the first step of checking a comment. If any of these words exist in the comment text, it is regarded as containing code and no further steps are taken. By default these tokens are comment starters that can be removed if nested comments are acceptable.
ignore_doxygen_comments¶
ignore_doxygen_comments : bool = False
normal_text¶
normal_text
Tokens that indicate non-code comments.Type: set[str]
Default:
{' and ', ' are ', ' be ', ' because ', ' but ', ' has ', ' is ', ' now ', ' oder ', ' only ', ' or ', ' the ', ' to ', ' und ', ' when ', ' with ', '##', '++++', '----', '://', '====', 'AXIVION'}
This is the second step of checking a comment. If any of these words exist in the comment text, it is regarded as not containing code.
normal_text_regex¶
normal_text_regex : bauhaus.analysis.config.SearchPattern = '\b[\w\d_]+\s+[\w\d_]+\s+[\w\d_]+\b'
This is the third step of checking a comment. If this regex finds a match in the comment text, the comment is regarded as not containing code.
suspicious¶
suspicious
Tokens that indicate code in comments without normal text.Type: set[str]
Default:
{' <= ', ' = ', ' == ', ' >= ', '#define', '#elif', '#else', '#endif', '#error', '#if', '#ifdef', '#ifndef', '#import', '#include', '#include_next', '#line', '#pragma', '#undef', '#warning', '++', '--', '->', '->*', '::', '::*', ';', '[', '_Pragma', '__has_include', 'for (', 'for(', 'if (', 'if(', 'while (', 'while('}
This is the final step of checking a comment. If the steps before did not exclude the comment from further inspection, the comment text is searched for any of these suspicious tokens. The comment is regarded as containing code, if any suspicious token is found.