Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add
must-use-output
attribute #3773New 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
base: master
Are you sure you want to change the base?
Add
must-use-output
attribute #3773Changes from 4 commits
d51e8a8
0b18cb1
17253d1
49b5c10
073b089
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like much of a problem to me. We already lint on function parameters that are unused within the callee1, so if I saw
#[must_use]
on a&mut
parameter, I'd expect this would refer to an obligation on the caller, especially as that's the existing connotation of#[must_use]
.Barring better proposals (and hopefully more concise ones than
#[must_use_output]
), my own preference is to just use#[must_use]
for this.Footnotes
Except for
self
, unfortunately, due to not having a good way to suppress it, but realistically we would never use#[must_use] &self
in this way, to require something of the callee. ↩There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I also feel like it wouldn't be that big deal and people would figure it out quickly. But at the same time I'm not bothered by the name too much and I respect whatever decision will be made by the appropriate team.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer
#[must_use]
. When applied to a function, it also means "the output at the call site must be used", and people don't have a problem understanding it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the attribute could be named differently, something along the lines of the function having side effect that needs to be observed
#[must_use(side_effect)]
or#[is_a_setter]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a couple of parallel discussions occurring about the name, one here and one at #3773 (comment) .
From that discussion:
caller_must_use
seems like it removes the ambiguity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just be the default behaviour when a function has multiple outputs (e.g. an output parameter as well as a return type) that should be used. You have to use at least one, not all of them.
P.S. This is another reason why I prefer the name
#[must_use]
instead of#[must_use_output]
, because it is conceptually the same thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we would need a way to override it in the other direction, because there are absolutely functions for which you need to use all the output parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to give a specific example:
core::mem::swap
.But also for
write
andread
you should use the returned value specifically. Just using the writer is not enough.