This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Patch to Add -Wunused-returns
- From: Fergus Henderson <fjh at cs dot mu dot oz dot au>
- To: "David E. Weekly" <dweekly at legato dot com>
- Cc: <gcc at gnu dot org>
- Date: Fri, 7 Dec 2001 17:02:39 +1100
- Subject: Re: Patch to Add -Wunused-returns
- References: <0c4f01c17edd$5bbbefc0$5c044589@legato.com>
On 06-Dec-2001, David E. Weekly <dweekly@legato.com> 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 <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.