public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Fergus Henderson <fjh@cs.mu.oz.au>
To: "David E. Weekly" <dweekly@legato.com>
Cc: <gcc@gnu.org>
Subject: Re: Patch to Add -Wunused-returns
Date: Thu, 06 Dec 2001 22:14:00 -0000	[thread overview]
Message-ID: <20011207170238.A2078@hg.cs.mu.oz.au> (raw)
In-Reply-To: <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.

  reply	other threads:[~2001-12-07  6:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-06 22:03 David E. Weekly
2001-12-06 22:14 ` Fergus Henderson [this message]
2001-12-06 23:32   ` David E. Weekly
2001-12-07  2:03 ` Neil Booth
2001-12-07  4:15 ` Joseph S. Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20011207170238.A2078@hg.cs.mu.oz.au \
    --to=fjh@cs.mu.oz.au \
    --cc=dweekly@legato.com \
    --cc=gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).