Equality vs Assignment

Ray Butterworth rbutterworth at watmath.UUCP
Fri Sep 19 05:39:00 AEST 1986


> Who among us has not been bitten at least once by writing
> if (x = 1) ...
> when he/she meant
> if (x == 1)
> What I want to know is
> 
> Has anyone written a program to scan a C program and issue a warning message
> for those lines where an assignment has occured in a conditional statement
> (i.e. if, while, the third statement in a for loop)?

There is already such a program:  lint.
Here's the modified section you want from the BSD 4.2 version of
/usr/src/usr.bin/lint/lpass1.c.

contx( p, down, pl, pr ) register NODE *p; register *pl, *pr; {

    *pl = *pr = VAL;
    if (p->in.type==UNDEF) down=VAL; /* (void) cast */

    switch( p->in.op ){

    case NOT:
        *pl=down;
    case ANDAND:
    case OROR:
        if (hflag&&(p->in.right->in.op==ASSIGN))
            werror("Possible unintended assignment");
    case QUEST:
        *pr = down;
    case CBRANCH:
        if (hflag&&(p->in.left->in.op==ASSIGN))
            werror("possible unintended assignment");
        break;

    case SCONV:
    case PCONV:
    ...



More information about the Comp.lang.c mailing list