"Daniel P. Berrange" <berra...@redhat.com> writes:

> On Wed, Sep 21, 2016 at 10:00:23AM +0000, Felipe Franciosi wrote:
>> 
>> > On 21 Sep 2016, at 07:24, Markus Armbruster <arm...@redhat.com> wrote:
>> > 
>> > "Pavel Dovgalyuk" <dovga...@ispras.ru> writes:
>> > 
>> >>> From: Felipe Franciosi [mailto:fel...@nutanix.com]
>> >>> If compiling with -Werror=unused-result, replay-internal.c won't build
>> >>> due to a call to fwrite() where the returned value is ignored. A simple
>> >>> cast to (void) is not sufficient on recent GCCs, so this fixes it.
>> >>> 
>> >>> Signed-off-by: Felipe Franciosi <fel...@nutanix.com>
>> >>> ---
>> >>> replay/replay-internal.c | 2 +-
>> >>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> >>> 
>> >>> diff --git a/replay/replay-internal.c b/replay/replay-internal.c
>> >>> index 5835e8d..6978d76 100644
>> >>> --- a/replay/replay-internal.c
>> >>> +++ b/replay/replay-internal.c
>> >>> @@ -65,7 +65,7 @@ void replay_put_array(const uint8_t *buf, size_t size)
>> >>> {
>> >>>     if (replay_file) {
>> >>>         replay_put_dword(size);
>> >>> -        fwrite(buf, 1, size, replay_file);
>> >>> +        (void)(fwrite(buf, 1, size, replay_file)+1);
>> >>>     }
>> >>> }
>> >> 
>> >> This looks very weird.
>> 
>> Oh I couldn't agree more. I hate this syntax. It is, however, the simplest 
>> way to get around this issue. See:
>> http://stackoverflow.com/questions/11888594/ignoring-return-values-in-c
>
> If we want to ignore return value reliably, lets just pull in the
> ignore_value macro from gnulib which is known to work across GCC
> versions
>
>
> /* Normally casting an expression to void discards its value, but GCC
>    versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
>    which may cause unwanted diagnostics in that case.  Use __typeof__
>    and __extension__ to work around the problem, if the workaround is
>    known to be needed.  */
> #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
> # define ignore_value(x) \
>     (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
> #else
> # define ignore_value(x) ((void) (x))
> #endif

Casting a value to void is the traditional and obvious way to say "yes,
I mean to ignore this value".  Now compilers start to reply "no, you
don't".  We can invent new (and less obvious) ways to say "yes, I do",
and compilers can then learn them so they can again reply "no, you
don't".  Why have compilers started to behave like two-year-olds?

[...]

Reply via email to