AutosarC++17_03-A7.1.1

Constexpr or const specifiers shall be used for immutable data declaration

Required inputs: IR

Rationale

If data is declared to be const or constexpr then its value can not be changed by mistake. Also, such declaration can offer the compiler optimization opportunities.

Note that the constexpr specifier in an object declaration implies const as well.

Example
//% $Id: A7-1-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#include <cstdint>
#include <limits>
void fn() {
const std::int16_t x1 = 5;     // Compliant
constexpr std::int16_t x2 = 5; // Compliant
std::int16_t x3 = 5; // Non-compliant - x3 is not modified but not declared as
               // constant (const or constexpr)
}
See Also
  • C++ Core Guidelines [10]: ES.25: Declare objects const or constexpr unless you want to modify its value later on.

Possible Messages

Key

Text

Severity

Disabled

variable_missing_const

An immutable variable shall be const/constexpr qualified.

None

False

Options

ignore_pointer_variables

ignore_pointer_variables : bool = False

Whether variables of pointer type should be ignored.
 

ignore_smart_pointer_variables

ignore_smart_pointer_variables : bool = False

Whether smart pointer variables should be ignored.
 

only_check_unit_locals

only_check_unit_locals : bool = False

Whether only local variables and global static variables should be checked.

Note: this option is automatically activated during single-file analysis.