...
This recommendation is related to MSC07-A. Detect and remove dead code.
Non-Compliant Code Example (
...
Assignment)
In this example, the comparison of a
to b
has no effect.
...
This is likely a case of the programmer mistakenly using the equals operator ==
instead of the assignment operator =
.
Compliant Solution (
...
Assignment)
The assignment of b
to a
is now properly performed.
Code Block | ||
---|---|---|
| ||
int a; int b; /* ... */ a = b; |
Non-Compliant Code Example (
...
Dereference)
In this example, p
is incremented and then dereferenced. However, *p
has no effect.
Code Block | ||
---|---|---|
| ||
int *p; /* ... */ *p++; |
Compliant Solution (
...
Dereference)
Correcting this example depends on the intent of the programmer. For instance, if dereferencing p
was a mistake, then p
should not be dereferenced.
...
If the intent was to increment the value referred to by p
, then parentheses can be used to ensure p
is dereferenced and then incremented (see EXP00-A. Use parentheses for precedence of operation).
Code Block | ||
---|---|---|
| ||
int *p; /* ... */ (*p)++ |
Compliant Solution (
...
Memory Mapped Devices)
Another possibility is that p
is being using used to reference a memory-mapped device. In this case, the variable p
should be declared as volatile
.
...
Wiki Markup |
---|
\[[Coverity 07|AA. C References#Coverity 07]\] Coverity Prevent User's Manual (3.3.0) (2007).
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "BRS Leveraging human experience," "BVQ Unspecified Functionality," "KOA Likely incorrect expressions," and "XYQ Dead and Deactivated Code"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 14.1 and Rule 14.2 |
...