-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Impossible to remove #![must_use] from a value #36675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Statement attributes will hopefully be stabilized soon, which should offer a nice way to do this (I say "should" because the current implementation doesn't work with lints). Anyway, how about a simple wrapper type? struct Unused<T>(T);
fn return_ok() -> Result<(), ()> {
Ok(())
}
fn main() {
Unused(return_ok()); // no warning
} |
Wrapping in a tuple works too: |
@Thiez though of course neither suggestion works if we ever make the lint smart enough to see through such things :) |
I suppose using a wrapper or tuple at the invocation would be equivalent to calling |
I don't think statement attributes work either, since even if they're on the macro, the problem is the invocation's warning happens outside the macro |
In my ideal world you would write #[allow(unused_must_use)] println_err!("whatever"); or #[allow(unused_must_use)] {
println_err!("one");
println_err!("two");
} or have a macro generate such code. Outside of that, or the wrapper struct/tuple trick, I chalk this up to working as designed -- the whole point of |
That's precisely what I don't want. And no macro can generate such code that also returns the value, because the value eventually escapes the attributed statement. It's not "working as designed". This isn't an unsafety issue, or a lifetime issue, even syntactic. The Type's author may want to remind the user with compiler warnings, but if the user knows these warnings are incorrect (like for a particular semantic case of I don't want to and shouldn't have to disable all extern crate bob;
unsafe fn not_safe() -> u8 { bob::s::unsafe_function(0, "foo") }
fn guaranteed_to_always_be_safe() -> u8 { unsafe { bob::s::unsafe_function(1, "foo") } } which is entirely permissible. |
Why would we want that, though? I thought the lint was there to prevent accidents, not to enforce some strange variation on checked exceptions. |
You'd can use the wrapper type in the macro, to keep the Result value silent but reachable on demand. (If you'd just want to silence each macro invocation manually, then the more popular |
@Thiez For the exact same reason @bluss I considered a wrapper type, but accessing it with a |
The point of On Mon, Sep 26, 2016 at 6:24 PM, Alex [email protected] wrote:
|
Triage: no changes here. Personal note: not sure if this is enough of a paint point to address. Not my call though. |
closing this as it feels like a niche request that hasn't got much attention in the last few years |
I want to write a macro wrapping something marked
#![must_use]
which doesn't spam the build output with "unused_must_use", but also doesn't destroy the result. I don't want to unwrap these values either, nor mark every single invocation with#![allow(unused_must_use)]
, nor allowunused_must_see
for the entire program (because with this macro's one exception, it is useful): I simply want to strip this return value's compiler-spammingmust_use
.This:
doesn't work because the result is unavailable if I need to check it, which I do on occasion.
Even this: fails to work:
because the final line can be unused in the calling context
stdout
/stderr
/write_ln!
are used as examples here, but I need to do this generically with certain things that spammust_use
.This isn't an unsafety issue. I simply shouldn't have to be bound ubiquitously by the particular compiler-warning policy preached by a module's author. I may (and do) have valid cases where
must_use
is semantically wrong.It should not be impossible to do this.
The text was updated successfully, but these errors were encountered: