This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: suggest parentheses around assignment used as truth value


Brad M. Garcia writes ...
> 
> > I'd just like to register my opiniont that code like
> > 
> > if (x = p()) {...}
> > 
> > shouldn't generate a warning under gcc -Wall.  Even if
> > you feel that code like this is obscure, the recommended
> > 
> > if ((x = p())) {...}
> > 
> > is just bizarre.
> 
> In 80% (don't you love making up statistics?) of cases where "=" is
> used inside an "if" expression, the programmer meant to use "==".  So
> the warning is helpful.

Ah, that 80% statistic reminds me of something:

---
% cat >case.c
int main() {
    int x;
    
    switch (1) {
    case 0:
	x = 1;
    case 1:
	x = 2;
    }

    return x;
}
% gcc -Wall case.c
%
---

falling through a case is an error even more often (I've heard the
number 95% of the time tossed around).

I suggest a warning for it.  consecutive cases with no intervening
statements should not generate the warning.

Of course, we would need a syntactic way to specify when fallthrough
was desired, much like the /*FALLTHROUGH*/ lint comment.

I suggest:

    switch (1) {
    case 0:
	x = 1;
	;                  /* it is visually clear this one doesn't */
    case 1:
	x = 2;
	break;             /* this case has a break */
    }

I'm going to adopt this style myself, I think; I'd love it if someone
could add the warning for it to egcs.

---------------------------------------------------------------------------
Tim Hollebeek                           | "Everything above is a true
email: tim@wfn-shop.princeton.edu       |  statement, for sufficiently
URL: http://wfn-shop.princeton.edu/~tim |  false values of true."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]