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

Reply via email to