From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11049 invoked by alias); 29 Sep 2006 01:17:26 -0000 Received: (qmail 11023 invoked by uid 48); 29 Sep 2006 01:17:16 -0000 Date: Fri, 29 Sep 2006 01:17:00 -0000 Subject: [Bug c/29280] New: misleading warning for assignment used as truth construct X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "elanthis at awesomeplay dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2006-09/txt/msg02717.txt.bz2 List-Id: 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