public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: suggest parentheses around assignment used as truth value
@ 1998-07-01  4:00 Brad M. Garcia
  1998-07-01 20:20 ` Tim Hollebeek
  1998-07-02  1:39 ` Harvey J. Stein
  0 siblings, 2 replies; 13+ messages in thread
From: Brad M. Garcia @ 1998-07-01  4:00 UTC (permalink / raw)
  To: rouaix; +Cc: egcs

> 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.

Now, it "suggests" using parentheses as a means to bypass the generation
of this warning.  You don't need to follow the suggestion, but the
suggestion results in the least amount of change to the source code.  
Other methods include moving the assignment outside of the "if"
expression, or using -Wno-parentheses in addition to -Wall.

Brad Garcia
   ___/  __ /  __ /  ___/ "Being the Linux of digital media
  __/   /  /  / _/  __/    would be a very good life."
_/    ____/ _/ _| ____/      - Jean-Louis Gassee, CEO of Be, Inc.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-01  4:00 suggest parentheses around assignment used as truth value Brad M. Garcia
@ 1998-07-01 20:20 ` Tim Hollebeek
  1998-07-02  1:39 ` Harvey J. Stein
  1 sibling, 0 replies; 13+ messages in thread
From: Tim Hollebeek @ 1998-07-01 20:20 UTC (permalink / raw)
  To: Brad M. Garcia; +Cc: rouaix, egcs

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."

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-01  4:00 suggest parentheses around assignment used as truth value Brad M. Garcia
  1998-07-01 20:20 ` Tim Hollebeek
@ 1998-07-02  1:39 ` Harvey J. Stein
  1 sibling, 0 replies; 13+ messages in thread
From: Harvey J. Stein @ 1998-07-02  1:39 UTC (permalink / raw)
  To: Brad M. Garcia; +Cc: hjstein

"Brad M. Garcia" <bgarcia@fore.com> 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.

I believe the original poster might be making a distinction btw

   if (x = p()) ...

and

   if (x = y) ...

The second case (where the lvalue and rvalue are variables) is the one
where comparison is typically meant instead of assignment, and which
the gcc warning should apply to.

In the first case (where the rvalue is a function call) it's not at
all clear to me that comparison

   if (x == p()) ...

is more common than assignment

   if (x = p()) ...

or even that assignment is a common mistake when the programmer means
comparison.

There are bad things about both constructs.  The first is doing a
comparision inside the if test instead of an assignment, but is also
losing the return value of the function.  The second is keeping the
return value, which is a good thing, but at the cost of doing an
assignment inside of the if test.

It's not clear that gcc should value one over the other.

Maybe there should be a different independently settable warning for
the case where the rvalue is a function return?

-- 
Harvey J. Stein
BFM Financial Research
hjstein@bfr.co.il

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-02  7:08 ` Carlo Wood
@ 1998-07-03 14:48   ` Kamil Iskra
  0 siblings, 0 replies; 13+ messages in thread
From: Kamil Iskra @ 1998-07-03 14:48 UTC (permalink / raw)
  To: Carlo Wood; +Cc: egcs

On Wed, 1 Jul 1998, Carlo Wood wrote:

> It is a very common mistake to type '=' where '==' was intended.
> Using extra parentheses is a very logical way to make the code
> more clear in what you mean.  Not only for the author itself,
> but also for others that read the code later and wonder if this
> is a typo/bug, or if it was intended (something not always clear
> from the context).
> 
> My personal opinion is that this warning is so useful that I'd
> even object to move it to -pedantic warnings.

I don't think that's a good idea. -pedantic serves completely different
pupose:


@item -pedantic
Issue all the warnings demanded by strict ANSI standard C; reject
all programs that use forbidden extensions.


AFAIK, ANSI doesn't require any diagnostic in this case.

/ Kamil Iskra    AmigaOS  Linux/i386  Linux/m68k               \
| GeekGadgets GCC maintainer   UNIX system administrator       |
| iskra@student.uci.agh.edu.pl  kiskra@ernie.icslab.agh.edu.pl |
\ kamil@dwd.interkom.pl   http://student.uci.agh.edu.pl/~iskra /


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-03  3:31         ` Nathan Sidwell
@ 1998-07-03  6:15           ` Richard Earnshaw
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Earnshaw @ 1998-07-03  6:15 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: rearnsha

> 
> It's rather hard to double the parentheses in error, and the form does
> visually stand out.
> 

It's very easy to double the parentheses in error.  You just have to have 
the erroneous code inside a macro.

Somewhat simple I know, but:

#define COMPARE(X,Y) ((X)=(Y)) /* Should be == */

...

if (COMPARE(p,q)) {...}  /* Should get a warning, but doesn't */

Richard.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-02  7:08       ` Richard Earnshaw
@ 1998-07-03  3:31         ` Nathan Sidwell
  1998-07-03  6:15           ` Richard Earnshaw
  0 siblings, 1 reply; 13+ messages in thread
From: Nathan Sidwell @ 1998-07-03  3:31 UTC (permalink / raw)
  To: richard.earnshaw; +Cc: egcs

Richard Earnshaw wrote:
> 
> Of course the parentheses are necessary in that case, but the meaning is
> obvious, and the compiler can tell that a warning is unnecessary.
> 
> In my opinion, it's a bug if gcc doesn't emit a warning for
> 
>   if ((x = p())) {...}
> 
> but does for
> 
>   if (x = p()) {...}
> 
> since apart from an extra set of parentheses they are identical.  Perhaps
> the warning should be changed to "suggest explicit comparison of
> assignment used as truth value".
I think it'd be a mistake to change the behaviour of this warning and
its inhibiting syntax. It's been that way on gcc for too long. I for one
would be annoyed -- and you can tell how picky I can be about warnings
from some of my other posts :-)

It's rather hard to double the parentheses in error, and the form does
visually stand out.

nathan

-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-06-30 19:49 Marc Rouaix
  1998-07-01  2:51 ` Andreas Schwab
  1998-07-01  3:42 ` Branko Cibej
@ 1998-07-02  7:08 ` Carlo Wood
  1998-07-03 14:48   ` Kamil Iskra
  2 siblings, 1 reply; 13+ messages in thread
From: Carlo Wood @ 1998-07-02  7:08 UTC (permalink / raw)
  To: Marc Rouaix; +Cc: egcs

| 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.
| 
| ---
| Marc

It is a very common mistake to type '=' where '==' was intended.
Using extra parentheses is a very logical way to make the code
more clear in what you mean.  Not only for the author itself,
but also for others that read the code later and wonder if this
is a typo/bug, or if it was intended (something not always clear
from the context).

My personal opinion is that this warning is so useful that I'd
even object to move it to -pedantic warnings.

-- 
 Carlo Wood  <carlo@runaway.xs4all.nl>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-01 21:20     ` Bill Currie
@ 1998-07-02  7:08       ` Richard Earnshaw
  1998-07-03  3:31         ` Nathan Sidwell
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Earnshaw @ 1998-07-02  7:08 UTC (permalink / raw)
  To: Bill Currie; +Cc: rearnsha

> Richard Earnshaw wrote:
> >         if ((x = p()) != 0) {...}
> > 
> > Then it becomes clear why the extra parentheses are needed.
> 
> Yes, because without the parentheses, it will be evaluated as:
> 
> 	if (x = (p() != 0)) {...}
> 
> And you're right back where you started from with a `suggest parentheses
> around assignment used as truth value' warning.
> 

Of course the parentheses are necessary in that case, but the meaning is 
obvious, and the compiler can tell that a warning is unnecessary.

In my opinion, it's a bug if gcc doesn't emit a warning for 

  if ((x = p())) {...}

but does for

  if (x = p()) {...}

since apart from an extra set of parentheses they are identical.  Perhaps 
the warning should be changed to "suggest explicit comparison of 
assignment used as truth value".


Richard.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-01  9:20   ` Richard Earnshaw
@ 1998-07-01 21:20     ` Bill Currie
  1998-07-02  7:08       ` Richard Earnshaw
  0 siblings, 1 reply; 13+ messages in thread
From: Bill Currie @ 1998-07-01 21:20 UTC (permalink / raw)
  To: richard.earnshaw; +Cc: Andreas Schwab, Marc Rouaix, egcs, rearnsha

Richard Earnshaw wrote:
>         if ((x = p()) != 0) {...}
> 
> Then it becomes clear why the extra parentheses are needed.

Yes, because without the parentheses, it will be evaluated as:

	if (x = (p() != 0)) {...}

And you're right back where you started from with a `suggest parentheses
around assignment used as truth value' warning.

Bill
-- 
Leave others their otherness

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-07-01  2:51 ` Andreas Schwab
@ 1998-07-01  9:20   ` Richard Earnshaw
  1998-07-01 21:20     ` Bill Currie
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Earnshaw @ 1998-07-01  9:20 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: rearnsha

> "Marc Rouaix" <rouaix@my-dejanews.com> writes:
> 
> |> I'd just like to register my opiniont that code like
> |> 
> |> if (x = p()) {...}
> |> 
> |> shouldn't generate a warning under gcc -Wall.
> 
> What if you have a typo here?
> 
> |>  Even if
> |> you feel that code like this is obscure, the recommended
> |> 
> |> if ((x = p())) {...}
> |> 
> |> is just bizarre.
> 
> Then write it as:
> 
> 	x = p();
> 	if (x) {...}
> 

Or,

	if ((x = p()) != 0) {...}

Then it becomes clear why the extra parentheses are needed.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-06-30 19:49 Marc Rouaix
  1998-07-01  2:51 ` Andreas Schwab
@ 1998-07-01  3:42 ` Branko Cibej
  1998-07-02  7:08 ` Carlo Wood
  2 siblings, 0 replies; 13+ messages in thread
From: Branko Cibej @ 1998-07-01  3:42 UTC (permalink / raw)
  To: Marc Rouaix; +Cc: egcs

Marc Rouaix wrote:

> I'd just like to register my opiniont that code like
>
> if (x = p()) {...}
>
> shouldn't generate a warning under gcc -Wall.

What if you meant to write "if (x == p()) {...}"? Such bugs are
usually very hard to find.

> Even if you feel that code like this is obscure,

It is _not_ obscure, it is in fact used quite often; therefore the
possibility of a typo is large enough to warrant a warning.

> the recommended
>
> if ((x = p())) {...}
>
> is just bizarre.

Can you recommend a better way of telling the compiler that you know
what you're doing? I mean, apart from the obvious

     x = p(); if (x) {...}

    Brane

--
Branko Cibej   <branko.cibej@hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
phone: (++386 61) 186 53 49  fax: (++386 61) 186 52 70



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: suggest parentheses around assignment used as truth value
  1998-06-30 19:49 Marc Rouaix
@ 1998-07-01  2:51 ` Andreas Schwab
  1998-07-01  9:20   ` Richard Earnshaw
  1998-07-01  3:42 ` Branko Cibej
  1998-07-02  7:08 ` Carlo Wood
  2 siblings, 1 reply; 13+ messages in thread
From: Andreas Schwab @ 1998-07-01  2:51 UTC (permalink / raw)
  To: Marc Rouaix; +Cc: egcs

"Marc Rouaix" <rouaix@my-dejanews.com> writes:

|> I'd just like to register my opiniont that code like
|> 
|> if (x = p()) {...}
|> 
|> shouldn't generate a warning under gcc -Wall.

What if you have a typo here?

|>  Even if
|> you feel that code like this is obscure, the recommended
|> 
|> if ((x = p())) {...}
|> 
|> is just bizarre.

Then write it as:

	x = p();
	if (x) {...}

-- 
Andreas Schwab                                      "And now for something
schwab@issan.informatik.uni-dortmund.de              completely different"
schwab@gnu.org

^ permalink raw reply	[flat|nested] 13+ messages in thread

* suggest parentheses around assignment used as truth value
@ 1998-06-30 19:49 Marc Rouaix
  1998-07-01  2:51 ` Andreas Schwab
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Marc Rouaix @ 1998-06-30 19:49 UTC (permalink / raw)
  To: egcs

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.

---
Marc



-----== Sent via Deja News, The Discussion Network ==-----
http://www.dejanews.com/  Easy access to 50,000+ discussion forums

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~1998-07-03 14:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-07-01  4:00 suggest parentheses around assignment used as truth value Brad M. Garcia
1998-07-01 20:20 ` Tim Hollebeek
1998-07-02  1:39 ` Harvey J. Stein
  -- strict thread matches above, loose matches on Subject: below --
1998-06-30 19:49 Marc Rouaix
1998-07-01  2:51 ` Andreas Schwab
1998-07-01  9:20   ` Richard Earnshaw
1998-07-01 21:20     ` Bill Currie
1998-07-02  7:08       ` Richard Earnshaw
1998-07-03  3:31         ` Nathan Sidwell
1998-07-03  6:15           ` Richard Earnshaw
1998-07-01  3:42 ` Branko Cibej
1998-07-02  7:08 ` Carlo Wood
1998-07-03 14:48   ` Kamil Iskra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).