AutosarC++17_10-A5.1.1¶
Literal values shall not be used apart from type initialization, otherwise symbolic names shall be used instead
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
Rationale
Avoid use of “magic” numbers and strings in expressions in preference to constant variables with meaningful names. Literal values are supposed to be used only in type initialization constructs, e.g. assignments and constructors.
The use of named constants improves both the readability and maintainability of the code.
Exceptions
It is allowed to use literal values in combination with logging mechanism.
Example
// $Id: A5-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <array>
#include <cstdint>
#include <iostream>
#include <stdexcept>
namespace {
const std::int32_t maxIterations = 10; // Compliant - assignment
const char *const loopIterStr = "iter "; // Compliant - assignment
const char separator = ’:’; // Compliant - assignment
} // namespace
void F1() noexcept {
for (std::int32_t i = 0; i < 10; ++i) // Non-compliant
{
std::cout << "iter " << i << ’:’ << ’\n’; // Compliant by exception
}
for (std::int32_t i = 0; i < maxIterations; ++i) // Compliant
{
std::cout << loopIterStr << i << separator << ’\n’; // Compliant
}
for (std::int32_t i = 0; i < maxIterations; ++i) // Compliant
{
std::cout << "iter " << i << ’:’ << ’\n’; // Compliant by exception
}
}
void F2() {
// ...
throw std::logic_error("Logic Error"); // Compliant
// initialization of exception object
}
class C {
public:
C()
: x(0), y(nullptr) // Compliant - initialization
{}
C(std::int8_t num, std::int32_t *ptr) : x(num), y(ptr) {}
private:
std::int8_t x;
std::int32_t *y;
};
static std::int32_t *globalPointer = nullptr; // Compliant - assignment
void F3() noexcept {
C c1;
// ...
C c2(0, globalPointer); // Compliant - initialization of C object
}
std::int32_t F4(std::int32_t x, std::int32_t y) noexcept { return x + y; }
void F5() noexcept {
std::int32_t ret = F4(2, 5); // Non-compliant
// ...
std::int32_t x = 2;
std::int32_t y = 5;
ret = F4(x, y); // Compliant
std::array<std::int8_t, 5> arr{{1, 2, 3, 4, 5}}; // Compliant
}
See Also
- HIC++ v4.0 [8]: 5.1.1 Use symbolic names instead of literal values in code.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
magic_number |
Use of magic literal. |
None |
False |
magic_number_without_token |
Use of magic literal. |
None |
False |
magic_string |
Use of magic string literal. |
None |
False |
possible_magic_number |
Potential use of magic literal. |
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_nonconst_variable_initialization¶
allow_nonconst_variable_initialization : bool = False
allowed¶
allowed : set[float] = {0.0, 1.0, 2.0}
allowed_contexts¶
allowed_contexts
Optional set of PIR classes or functions ((node) -> bool) for allowed contexts, e.g. Case_Label.Type: set[bauhaus.ir.PIR_Class_Name | typing.Callable[[bauhaus.ir.Node], bool]]
Default:
{'Template_Const_Argument', <function bauhaus.rules.autosar.cpp.rule_A5_1_1.allow_in_type_initialization(node)>}
allowed_logging_contexts¶
allowed_logging_contexts : set[bauhaus.analysis.config.QualifiedName] = {'std::basic_ostream'}
operator<<). If this is non-empty,
std::throw() is considered a valid logging context as well.
allowed_string_contexts¶
allowed_string_contexts
Optional set of PIR classes or functions ((node) -> bool) for allowed contexts.Type: set[bauhaus.ir.PIR_Class_Name | typing.Callable[[bauhaus.ir.Node], bool]]
Default:
{<function bauhaus.rules.autosar.cpp.rule_A5_1_1.allow_in_type_initialization(node)>}
allowed_strings¶
allowed_strings : set[str] = {' ', ''}
exceptions¶
exceptions : typing.Callable[[bauhaus.ir.Node], bool] | None = None
exclude_pp_literals¶
exclude_pp_literals : bool = True
exclude_single_uses¶
exclude_single_uses : bool = False