HP C Programmer's Guide (92434-90009)

172 Chapter7
Using C Programming Tools
Using lint
The lint command's detection of unreachable code is by no means exhaustive. Warning
messages can be issued about valid code, and conversely lint may overlook code that
cannot be reached.
Programs that are generated by yacc or lex can have many unreachable break
statements. Normally, each one causes a complaint from lint. The -b option can be used
to force lint to ignore unreachable break statements.
Function Value
The C compiler allows a function containing both the statement
return();
and the statement
return
(expression);
to pass through without complaint. The lint command, however, detects this
inconsistency and responds with the message:
warning: function
'name'
has 'return(expression)' and 'return'
The most serious difficulty with this is detecting when a function return is implied by flow
of control reaching the end of the function. This can be seen with a simple example:
f(a)
{
if (a) return (3);
g();
}
Notice that if a tests false, f will call g and then return with no defined value. This will
trigger a message for lint. If g (like exit) never returns, the message will still be
produced when in fact nothing is wrong. In practice, some potentially serious bugs have
been discovered by this feature.
On a global scale, lint detects cases where a function returns a value that is sometimes or
never used. When the value is never used, it may constitute an inefficiency in the function
definition. When the value is sometimes used, it may represent bad style (e.g., not testing
for error conditions).
The lint command will not issue a diagnostic message if that function call is cast as void.
For example,
(void) printf("%d\n",i);
tells lint to not warn about the ignored return value.
The dual problem — using a function value when the function does not return one — is
also detected. This is a serious problem.
The lint command
does not
have an option for suppressing the display of warning for
inconsistent return functions and functions that return no value.
Portability
The -p option of lint aids the programmer is writing portable code in four areas: