AutosarC++19_03-A2.7.2

Sections of code shall not be “commented out”

Required inputs: IR

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

doxygen_start

doxygen_start : set[str] = {'/**', '///'}

Start of a valid Doxygen comment. Note that ignore_doxygen_comments needs to be enabled to actually ignore the matching comments.
 

files_to_check

files_to_check : bauhaus.rules.axivion.lexical.comments.FileToCheckSet = {'Primary_File', 'System_Include_File', 'User_Include_File'}

Files to be checked, e.g. Primary_File or File.
 

forbidden_tokens

forbidden_tokens : set[str] = {'/*', '// '}

Tokens that indicate code in comments despite normal text.

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

If set, doxygen comments (starting with prefixes as configured with doxygen_start) are never considered violations.
 

normal_text

normal_text

Type: set[str]

Default: {' and ', ' are ', ' be ', ' because ', ' but ', ' has ', ' is ', ' now ', ' oder ', ' only ', ' or ', ' the ', ' to ', ' und ', ' when ', ' with ', '##', '++++', '----', '://', '====', 'AXIVION'}

Tokens that indicate non-code comments.

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'

Regular expression to match normal non-code text.

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

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('}

Tokens that indicate code in comments without normal text.

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.