Skip to content

must_use attribute #646

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

Closed
jorickert opened this issue Feb 21, 2020 · 4 comments
Closed

must_use attribute #646

jorickert opened this issue Feb 21, 2020 · 4 comments

Comments

@jorickert
Copy link
Contributor

Is there a reason why the must_use attribute is not used on functions that return a Boolean, for example "is_match" ? It could help to catch errors when the return value accidentally is unused

@BurntSushi
Copy link
Member

Is there a reason why

Because #[must_use] wasn't always available on Rust stable and because nobody has suggested it. And I certainly have not considered it.

Before I'd be willing to do this, I'd really need a more thorough proposal than this. In particular, what are the downsides of #[must_use]? In my experience it is not liberally used, so I wonder why not?

@BurntSushi
Copy link
Member

The most common place where I've seen #[must_use] used is for Result return types. It makes a lot of sense there because Result<(), Error> is so common and since it isn't always common to write tests for error cases (although of course one should). However, in the case of is_match, ignoring its return value is almost certainly going to produce a fairly noticeable bug somewhere in even lightly tested code.

I think the key missing piece of the equation for me is, "what are the down sides of using #[must_use]? why isn't it more broadly used?"

@jorickert
Copy link
Contributor Author

The most common place where I've seen #[must_use] used is for Result return types.

This is IMO a bit strange, because the Result type already has a #[must_use] attribute. As far as i know there are no downsides when adding a must_use tag. (Obviously its a bit of work to annotate functions with it). But its indeed strange that it is not used more widely. The tracking issue for annotating more functions in the standard library with it is rust-lang/rust#48926

@BurntSushi
Copy link
Member

This is IMO a bit strange, because the Result type already has a #[must_use] attribute.

Yes, that's what I meant?

Anyway, thank you for linking to that issue. It led me to this thread. In particular, I pretty much agree with what withoutboats said:

I don’t think we should put a must_use on every “pure” function; I think we should put them on ones that seem plausible to mistake as mutating methods (like round).

If std winds up adopting a liberal policy of using #[must_use], then I could get on board with following suit, but I don't see is_match as a strong contender for this attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants