Sun Studio 12: C User's Guide

4.6.1 Diagnostics Performed by lint

lint-specific diagnostics are issued for three broad categories of conditions: inconsistent use, nonportable code, and questionable constructs. In this section, we review examples of lint’s behavior in each of these areas, and suggest possible responses to the issues they raise. Consistency Checks

Inconsistent use of variables, arguments, and functions is checked within files as well as across them. Generally speaking, the same checks are performed for prototype uses, declarations, and parameters as lint checks for old-style functions. If your program does not use function prototypes, lint checks the number and types of parameters in each call to a function more strictly than the compiler. lint also identifies mismatches of conversion specifications and arguments in [fs]printf() and [fs]scanf() control strings.

Examples: Portability Checks

Some nonportable code is flagged by lint in its default behavior, and a few more cases are diagnosed when lint is invoked with -p or -Xc. The latter causes lint to check for constructs that do not conform to the ISO C standard. For the messages issued under -p and -Xc, see 4.6.2 lint Libraries.


char c;
c = getchar();
if (c == EOF) ...

where EOF has the value -1, always fails on machines where character variables take on nonnegative values. lint invoked with -p checks all comparisons that imply a plain char may have a negative value. However, declaring c as a signed char in the above example eliminates the diagnostic, not the problem. That’s because getchar() must return all possible characters and a distinct EOF value, so a char cannot store its value. We cite this example, perhaps the most common one arising from implementation-defined sign-extension, to show how a thoughtful application of lint’s portability option can help you discover bugs not related to portability. In any case, declare c as an int.

short s;
long l;
s = l;

lint flags all such assignments by default; the diagnostic can be suppressed by invoking the -a option. Bear in mind that you may be suppressing other diagnostics when you invoke lint with this or any other option. Check the list in 4.6.2 lint Libraries for the options that suppress more than one diagnostic.

int *fun(y)
char *y;
    return(int *)y;

because, on most machines, an int cannot start on an arbitrary byte boundary, whereas a char can. You can suppress the diagnostic by invoking lint with -h, although, again, you may be disabling other messages. Better still, eliminate the problem by using the generic pointer void *.

int a[10];
    int i = 1;
    a[i++] = i;

In this example, the value of a[1] may be 1 if one compiler is used, 2 if another. The bitwise logical operator & can give rise to this diagnostic when it is mistakenly used in place of the logical operator &&:

if ((c = getchar()) != EOF & c != ’0’) Questionable Constructs

lint flags a miscellany of legal constructs that may not represent what the programmer intended. Examples:

unsigned x;
if (x < 0) ...

always fails. The test:

unsigned x;
if (x > 0) ...

is equivalent to:

if (x != 0) ...

This may not be the intended action. lint flags questionable comparisons of unsigned variables with negative constants or 0. To compare an unsigned variable to the bit pattern of a negative number, cast it to unsigned:

if (u == (unsigned) -1) ...

Or use the U suffix:

if (u == -1U) ...

int fun()
    int a, b, x, y;
    (a = x) && (b == y);

if (x & a == 0) ...

is evaluated as:

if (x & (a == 0)) ...

which is most likely not what you intended. Invoking lint with -h disables the diagnostic.