From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26362 invoked by alias); 7 Dec 2001 06:03:02 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 26333 invoked from network); 7 Dec 2001 06:02:59 -0000 Received: from unknown (HELO fencepost.gnu.org) (199.232.76.164) by sources.redhat.com with SMTP; 7 Dec 2001 06:02:59 -0000 Received: from mail-gate.cs.mu.oz.au ([198.142.254.221] helo=mumnunah.cs.mu.OZ.AU) by fencepost.gnu.org with esmtp (Exim 3.22 #1 (Debian)) id 16CE62-0006Vl-00 for ; Fri, 07 Dec 2001 01:02:56 -0500 Received: from hg.cs.mu.oz.au (root@hg.cs.mu.OZ.AU [128.250.25.19]) by mumnunah.cs.mu.OZ.AU with ESMTP id RAA06657; Fri, 7 Dec 2001 17:02:40 +1100 (EST) Received: (from fjh@localhost) by hg.cs.mu.oz.au (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id RAA02160; Fri, 7 Dec 2001 17:02:39 +1100 Date: Thu, 06 Dec 2001 22:14:00 -0000 From: Fergus Henderson To: "David E. Weekly" Cc: Subject: Re: Patch to Add -Wunused-returns Message-ID: <20011207170238.A2078@hg.cs.mu.oz.au> References: <0c4f01c17edd$5bbbefc0$5c044589@legato.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0pre3i In-Reply-To: <0c4f01c17edd$5bbbefc0$5c044589@legato.com> X-SW-Source: 2001-12/txt/msg00350.txt.bz2 On 06-Dec-2001, David E. Weekly wrote: > +++ c-common.c 2001/12/07 04:57:44 ... > +/* Check to see if a non-void function's return value is ignored. */ > + > +void > +check_for_unused_returns (expr) > + tree expr; > +{ > + tree function; > + > + /* Make sure our expression is a function. */ > + if (TREE_CODE (expr) != CALL_EXPR || > + TREE_TYPE (expr) == void_type_node) > + return; > + > + function = TREE_OPERAND (expr, 0); > + > + if ((TREE_CODE (function) == ADDR_EXPR) && > + (TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)) > + warning("return value from function ignored"); I don't see what the point of the second `if' is. Surely the same warning should be issued for calls via function pointers as is issued for ordinary calls. So I'd just write the body of this function like this: if (TREE_CODE (expr) == CALL_EXPR && TREE_TYPE (expr) == void_type_node) warning("return value from function call ignored"); On the other hand, if it is an ordinary function call, then it would be nice if the warning message included the name of the function. So maybe it would be better to keep the code you have above, and change the last three lines to something like this (completely untested): if ((TREE_CODE (function) == ADDR_EXPR) && (TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)) warning_with_decl(TREE_OPERAND (function, 0), "return value from function `%s' ignored"); else warning("return value from function call ignored"); > Index: gcc/doc/invoke.texi ... > +@item -Wunused-returns @r{(C only)} > +@opindex Wunused-returns > +Warn whenever the result of a non-void function is implicitly cast away. > +It is not included in the above, because it requires @code{void} casting > +returns of all sorts of regular functions, like @code{close} and > +@code{printf}, whose return value you usually don't care about. This makes > +GCC more like Lint. I'm not sure that close() is a good example here, since checking the return value from close() for files opened in output mode is very important, and it would be bad to encourage people to omit that. -- Fergus Henderson | "I have always known that the pursuit The University of Melbourne | of excellence is a lethal habit" WWW: | -- the last words of T. S. Garp.