Function must_use
const fn must_use<T>(value: T) -> T
An identity function that causes an unused_must_use warning to be
triggered if the given value is not used (returned, stored in a variable,
etc) by the caller.
This is primarily intended for use in macro-generated code, in which a
#[must_use] attribute either on a type or a function would not
be convenient.
Example
use fmt;
;
// Implementation detail of make_error! macro.
#
# // Make rustdoc not wrap the whole snippet in fn main, so that $crate::make_error works
#
In the above example, we'd like an unused_must_use lint to apply to the
value created by make_error!. However, neither #[must_use] on a struct
nor #[must_use] on a function is appropriate here, so the macro expands
using core::hint::must_use instead.
-
We wouldn't want
#[must_use]on thestruct Errorbecause that would make the following unproblematic code trigger a warning:# ; # # -
Using
#[must_use]onfn make_errorcan't help because the return value is used, as the right-hand side of aletstatement. Theletstatement looks useless but is in fact necessary for ensuring that temporaries within theformat_argsexpansion are not kept alive past the creation of theError, as keeping them alive past that point can cause autotrait issues in async code:# # # ; # # # # # async async // Returns something without a Sync impl. *const # #