This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
Programs should not contain dead code, the removal of which does not affect the program behavior.
The presence of dead code can indicate an error in the program logic.
Exceptions:
void when the result is not used, since it points to a value that is intentionally not used;Note. Compilers can remove dead code, as this does not affect the program behavior.
The example of redundant operations:
int func( int input )
{
short var;
char *ptr;
var = 11; // <=
(int)var; // <=
var + input; // <=
*ptr++; // <=
return input + input;
}
There are several operations: assignment (=), type casting ((int)), addition (+), the ptr pointer increment and dereference (*). They are redundant because the result of these operations is not used and does not affect the program execution.
The fixed code:
int func( int input )
{
short var;
int *ptr;
ptr = &input;
var = 11;
input += (int)var;
(*ptr)++;
return input;
}
The example of a redundant function call:
void funcToCall ( int input )
{
return;
for(size_t i = 0; i < 13; ++i)
{
input += i;
}
}
int funcBase ( int input )
{
funcToCall(input);
return input + input;
}
The funcToCall function is not dead code, since it contains operations in unreachable code. However, calling the function is dead code, since its removal does not change the program behavior.
The fixed code:
unsigned int funcToCall ( unsigned int x )
{
for(size_t i = 0; i < 13; ++i)
{
x += i;
}
return x;
}
unsigned int funcBase ( unsigned int x )
{
return funcToCall(x);
}
This diagnostic is classified as:
|