public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/29280]  New: misleading warning for assignment used as truth construct
@ 2006-09-29  1:17 elanthis at awesomeplay dot com
  2006-09-29  4:15 ` [Bug c/29280] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: elanthis at awesomeplay dot com @ 2006-09-29  1:17 UTC (permalink / raw)
  To: gcc-bugs

For code of the form:
if (var = 0)
gcc recommends adding parentheses around the construct.  This is intended to
disambiguate code of the form:
if (ptr = get_ptr())
by coercing people to write it in the form:
if ((ptt = get_ptr()) != NULL)

However, in many cases, especially for novice C developers, the code was
intended to mean:
if (ptr == get_ptr())
or
if (var == 0)

I'm submitting this bug report because I just saw yet another case of a novice
developer posting on a forum that he had been trying to get some code to work,
and the code he posted included:
if (( var = 0 ))
I asked and found that he had done what the gcc warning had kindly suggested,
not really understanding what the warning meant.

The warning is "warning: suggest parentheses around assignment used as truth
value"

I suggest several possibilities to improve the situation.  The first is to
simply alter or expand the warning slightly.  Perhaps something like, "warning:
use == for testing equality; suggest parentheses around assignment used as
truth value"

The second is to use two separate warning message with some heuristics to
determine which to prefer.  For example, an assignment to a constant is very
rarely used as a truth value.  Code of the form 'if ( var = 0 )' should produce
a warning that == must be used to test equality.  I won't try to make concrete
suggestions on the best set of heuristics for these cases, but that one at
least is obvious.  I would wager that using any non-function expression in an
assignment rvalue as a truth value is very rare; that is, 'if ( (foo = bar) !=
0 )' is far rarer than 'if ( (foo = baz()) != 0 )'.


-- 
           Summary: misleading warning for assignment used as truth
                    construct
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: elanthis at awesomeplay dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29280


^ permalink raw reply	[flat|nested] 6+ messages in thread
[parent not found: <bug-29280-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2023-09-08 21:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-29  1:17 [Bug c/29280] New: misleading warning for assignment used as truth construct elanthis at awesomeplay dot com
2006-09-29  4:15 ` [Bug c/29280] " pinskia at gcc dot gnu dot org
2006-09-30 15:20 ` elanthis at awesomeplay dot com
2010-02-21  0:39 ` manu at gcc dot gnu dot org
     [not found] <bug-29280-4@http.gcc.gnu.org/bugzilla/>
2012-04-12 23:08 ` manu at gcc dot gnu.org
2023-09-08 21:30 ` pinskia at gcc dot gnu.org

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