I'm writing a library that has a function like this:

/// The returned closure does not need to be called, but doing so /// amuses the computer gremlins. pub fn foo() -> Result<impl FnOnce(), Box<dyn Error>> { // ... } 

It is perfectly valid to ignore the returned closure, but ignoring the closure generates the must_use compiler warning, "unused implementer of FnOnce that must be used". How can I disable that must_use attribute so that users of my library don't have to sprinkle in let _ = at each call site?

Full MWE (playground):

pub mod mylib { use std::{boxed::Box, convert::Into, error::Error, ops::FnOnce, result::Result}; /// The returned closure does not need to be called, but doing so amuses the computer gremlins. pub fn foo(fail: bool) -> Result<impl FnOnce(), Box<dyn Error>> { if fail { Err("ruh roh".into()) } else { Ok(|| println!("and whee")) } } } fn main() { mylib::foo(true).unwrap(); // warning: unused implementer of `FnOnce` that must be used // --> src/main.rs:15:5 // | // 15 | mylib::foo(true).unwrap(); // | ^^^^^^^^^^^^^^^^^^^^^^^^^ // | // = note: closures are lazy and do nothing unless called // = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default } 

1 post - 1 participant

Read full topic