AutosarC++18_10-A0.1.1¶
A project shall not contain instances of non-volatile variables being given values that are not subsequently used
Required inputs: IR, StaticSemanticAnalysis
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Known as a DU dataflow anomaly, this is a process whereby there is a data flow in which a variable is given a value that is not subsequently used. At best this is inefficient, but may indicate a genuine problem. Often the presence of these constructs is due to the wrong choice of statement aggregates such as loops.
See: DU-Anomaly.
Exceptions
Loop control variables (see Section 6.6.5) are exempt from this rule.
Example
//% $Id: A0-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <array>
#include <cstdint>
std::uint8_t Fn1(std::uint8_t param) noexcept
{
std::int32_t x{
0}; // Non-compliant - DU data flow anomaly; Variable defined,
// but not used
if (param > 0)
{
return 1;
}
else
{
return 0;
}
}
std::int32_t Fn2() noexcept
{
std::int8_t x{10U}; // Compliant - variable defined and will be used
std::int8_t y{20U}; // Compliant - variable defined and will be used
std::int16_t result = x + y; // x and y variables used
x = 0; // Non-compliant - DU data flow anomaly; Variable defined, but x is
// not subsequently used and goes out of scope
y = 0; // Non-compliant - DU data flow anomaly; Variable defined, but y is
// not subsequently used and goes out of scope
return result;
}
std::int32_t Fn3(std::int32_t param) noexcept
{
std::int32_t x{param +
1}; // Compliant - variable defined, and will be used in
// one of the branches
// However, scope of x variable could be reduced
if (param > 20)
{
return x;
}
return 0;
}
std::int32_t Fn4(std::int32_t param) noexcept
{
std::int32_t x{param +
1}; // Compliant - variable defined, and will be used in
// some of the branches
if (param > 20)
{
return x + 1;
}
else if (param > 10)
{
return x;
}
else
{
return 0;
}
}
void Fn5() noexcept
{
std::array<std::int32_t, 100> arr{};
arr.fill(1);
constexpr std::uint8_t limit{100U};
std::int8_t x{0};
for (std::uint8_t i{0U}; i < limit; ++i) // Compliant by exception - on the
// final loop, value of i defined will
// not be used
{
arr[i] = arr[x];
++x; // Non-compliant - DU data flow anomaly on the final loop, value
// defined and not used
}
}
See Also
- MISRAC++2008: 0-1-6 A project shall not contain instances of non-volatile variables being given values that are never subsequently used.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
conditional_unused_def |
Result of assignment is not used along some path(s) |
None |
False |
init_used_in_other_isr |
Initialization is not used except within code reached from an entry point that might be triggered by an interrupt |
None |
False |
unused_catch_parameter |
Unused implicit initialization of catch clause parameter (you can use an unnamed one instead) |
None |
False |
unused_def |
Result of assignment is not used |
None |
False |
unused_init |
Unused initialization |
None |
False |
used_in_other_isr |
Result of assignment is not used except within code reached from an entry point that might be triggered by an interrupt |
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
report_dead_initializations¶
report_dead_initializations : bool = True