Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!caip!clyde!watmath!rbutterworth From: rbutterworth@watmath.UUCP (Ray Butterworth) Newsgroups: net.lang.c Subject: Re: Equality vs Assignment Message-ID: <3234@watmath.UUCP> Date: Thu, 18-Sep-86 15:39:00 EDT Article-I.D.: watmath.3234 Posted: Thu Sep 18 15:39:00 1986 Date-Received: Fri, 19-Sep-86 23:57:24 EDT References: <2259@gitpyr.UUCP> Organization: U of Waterloo, Ontario Lines: 37 > 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: ...