This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: suggest parentheses around assignment used as truth value
- To: bgarcia at fore dot com (Brad M. Garcia)
- Subject: Re: suggest parentheses around assignment used as truth value
- From: Tim Hollebeek <tim at wagner dot princeton dot edu>
- Date: Wed, 1 Jul 1998 13:26:40 -0400 (EDT)
- Cc: rouaix at my-dejanews dot com, egcs at cygnus dot com
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."