RED-unused-val
In this section:
Synopsis
A variable is assigned a value that is never used.
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
A variable is initialized or assigned a value, and then another assignment destroys that value before it is used. This is not unsafe as such, but might indicate a logical error. This check does not detect when a value is simply lost when the function ends. This check is identical to MISRAC++2008-0-1-6, MISRAC2012-Rule-2.2_c.
Coding standards
- CWE 563
Unused Variable
- MISRA C:2012 Rule-2.2
(Required) There shall be no dead code
- MISRA C++ 2008 0-1-6
(Required) A project shall not contain instances of non-volatile variables being given values that are never subsequently used.
Code examples
The following code example fails the check and will give a warning:
int example(void) {
int x;
x = 20;
x = 3;
return 0;
}
The following code example passes the check and will not give a warning about this issue:
int example(void) {
int x;
x = 20;
return x;
}