Marking a return value as inappropriate for _

A while back someone suggested I use the drop_guard crate to handle cleanup of partial initialization (in case of error mid-initialization of a function that performs a series of persistent system configuration changes). I made the following mistake:

let _ = drop_guard::guard(&status, |st| {
  // .. cleanup if st is false ..
});

It occurred to me that it would have been helpful if guard() could be marked with something akin to #[must_use], but which can take it one step further and warn if let _ = ... is used.

Is there already such an attribute? If not, what are the arguments against adding one?

1 Like

There isn't such attribute yet.

3 Likes

Wait,

So if let _ = ... is used for something implements Drop, it is not last to the end of current block. However, should this be the case if #[must_use] is also marked for the type?

I just couldn't think of any valid situation that the current behavior (drop as soon as possible), rather than the expected behavior (last as long as possible), would be more useful.

So I think we can alter it a bit such that if a type T

  • T: Drop
  • T is annotated with #[must_use]

Then it should last as long as possible when bind to _.

(If someone do wanted to drop early just write

{ let _ = ... }

That would be a breaking change. (Not particularly a fan of subtle yet sometimes very significant different behavior at a distance - and multiple distant locations at that - either.)

Right.

However

  1. There is a clear code migration path (introduce extra {})
  2. Or we can have a global flag, #![must_use_types_delays_drop] then make it default in the next edition.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.