On Mon, Oct 05, 2026 at 01:08:32PM +0100, Daniel P. Berrangé wrote:
> Functions annotated with 'warn_unused_result' cannot be cast
> to (void) to silence the compiler. Add the ignore_value()
> macro which tricks the compiler into thinking the result is
> genuinely used, avoiding the need for manual workarounds where
> the return value is intentionally ignored.
> 
> This clever macro is derived from one originating with the
> GNULIB project:
> 
>   
> https://gitweb.git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/ignore-value.h;hb=HEAD

Hey, I recognize that :) (See gnulib commit 939dedd9a2 where I
switched from an inline static function to use of __typeof__ 15 years
ago)

> 
> but simplified since we don't need compat with old GCC and
> optimizing on clang is overkill.
> 
> Reviewed-by: Marc-André Lureau <[email protected]>
> Signed-off-by: Daniel P. Berrangé <[email protected]>

Reviewed-by: Eric Blake <[email protected]>

> ---
>  include/qemu/osdep.h | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index 1ec5b42230b..c1ca2c127b6 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -251,6 +251,14 @@ extern "C" {
>  #endif
>  
>  
> +/*
> + * Merely casting to (void) is not sufficient for functions
> + * annotated with the "warn_unused_result" attribute
> + */
> +#define ignore_value(x) \
> +    (__extension__ ({ __typeof__(x) __x = (x); (void) __x; }))
> +
> +
>  /*
>   * For mingw, as of v6.0.0, the function implementing the assert macro is
>   * not marked as noreturn, so the compiler cannot delete code following an
> -- 
> 2.55.0
> 
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


Reply via email to