There is a coverity warning type: UNUSED_VALUE. This is defined by tool under "Code maintainability issues"
UNUSED_VALUE: When a variable is assigned a pointer value returned from a function call and is never used anywhere else in the source code, it can not only cause inefficient use of resources but can also result in undetermined behavior. This checker identifies all variables that are never used anywhere else in the program after a value is assigned to them.
This checker seems to be picking up some of the good programming practice also as a warning.
My question is that is there some better way to do the things? Or should such a warning be ignored (and reported to Coverity team for any possible improvements)?
Example 1: default iniailization of local variables
int func()
{
int retval = SUCCESS; //COVERITY says: Unused value (UNUSED_VALUE)assigned_value: Value SUCCESS is assigned to retval here, but that stored value is not used before it is overwritten
retval = recvMessage(); //COVERITY says: value_overwrite: Value SUCCESS is overwritten with value fromrecvMessage
....
}
Example 2: setting pointer to NULL after memory is freed
void func2()
{
char *p = NULL;
....
p = alloc_mem_wrapper();
... do something
free_mem_wrapper(p);
p = NULL; //Coverity says: Unused value (UNUSED_VALUE)assigned_pointer: Value NULL is assigned to p here, but that stored value is not used
... do rest of the job (p is not used again)
}
In my case, 90% of all the warnings are of above nature only!
Why not do it like this:
and this
(Mostly) if you do not know how to initialise a variable you probably do not needed (where you defined it).