Re: warn on unused result without attribute warn_unused_result

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14 October 2016 at 20:34, Martin Sebor <msebor@xxxxxxxxx> wrote:
> On 10/13/2016 12:32 PM, Ben Burns wrote:
>>
>> Hello gcc contributors.
>>
>> I had an issue which was ultimately caused by not using the return
>> value of a function and I'm trying to find a compile-time warning that
>> could have alerted me to the issue.
>>
>> I've been looking through the documentation but I'm not seeing a
>> compile-time warning flag about ignoring function return values. Sure,
>> I can add the attribute "warn_unused_result" but I'm wondering if the
>> same affect is possible (i.e., for all functions) without changing any
>> code. I feel like this must have come up before and there's a reason
>> this warning doesn't exist -- perhaps this is better handled by a
>> static analysis tool, or perhaps it generates too much noise -- but if
>> there was a previous discussion about this I'd appreciate being
>> pointed in that direction. Or perhaps I'm just not searching for the
>> right terms, in which case I apologize for taking your time.
>
>
> I don't know if the idea has ever been discussed but because
I had submitted a patch to warn for unused return value of pure/const functions,
but it didn't make it into trunk because it gave false positives for
conditionally defined macros:
https://gcc.gnu.org/ml/gcc-patches/2016-07/msg01672.html

Thanks,
Prathamesh
> the return value of many common functions is rarely used (all
> the string manipulation functions in the C library and many stdio
> functions, for instance) issuing a warning for it would generate
> far too much noise to be useful.  It's quite easy to add this
> warning as an experiment.  With the patch below GCC emits the
> following warnings for your example:
>
> a.c: In function ‘main’:
> a.c:16:5: warning: ignoring return value of ‘scanf’ [-Wany-unused-result]
>      scanf("%d", &b);
>      ^~~~~~~~~~~~~~~
> a.c:17:5: warning: ignoring return value of ‘func’ [-Wany-unused-result]
>      func(b);
>      ^~~~~~~
> a.c:18:5: warning: ignoring return value of ‘inline_func’
> [-Wany-unused-result]
>      inline_func(b);
>      ^~~~~~~~~~~~~~
>
> Martin
>
>
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index e146781..21e0daa 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -998,6 +998,10 @@ Wunused-result
>  C ObjC C++ ObjC++ Var(warn_unused_result) Init(1) Warning
>  Warn if a caller of a function, marked with attribute warn_unused_result,
> does not use its return value.
>
> +Wany-unused-result
> +C ObjC C++ ObjC++ Var(warn_any_unused_result) Init(0) Warning
> +Warn if a caller of a function does not use its return value.
> +
>  Wunused-variable
>  C ObjC C++ ObjC++ LangEnabledBy(C ObjC C++ ObjC++,Wunused)
>  ; documented in common.opt
> diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
> index 2ed450c..462c440 100644
> --- a/gcc/tree-cfg.c
> +++ b/gcc/tree-cfg.c
> @@ -8920,6 +8920,17 @@ do_warn_unused_result (gimple_seq seq)
>                             "ignoring return value of function "
>                             "declared with attribute warn_unused_result");
>             }
> +         else if (warn_any_unused_result)
> +           {
> +             location_t loc = gimple_location (g);
> +
> +             if (fdecl)
> +               warning_at (loc, OPT_Wany_unused_result,
> +                           "ignoring return value of %qD", fdecl);
> +             else
> +               warning_at (loc, OPT_Wany_unused_result,
> +                           "ignoring return value of function");
> +           }
>           break;
>
>         default:
>




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux