Skip to content

Disallow #[must_use] for functions with no return value #55035

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
leonardo-m opened this issue Oct 13, 2018 · 2 comments
Closed

Disallow #[must_use] for functions with no return value #55035

leonardo-m opened this issue Oct 13, 2018 · 2 comments

Comments

@leonardo-m
Copy link

leonardo-m commented Oct 13, 2018

This code:

#[must_use]
fn foo() {}
fn main() {
    foo();
}

Gives (rustc 1.31.0-nightly 2c2e2c5 2018-10-12):

warning: unused return value of `foo` which must be used
 --> ...\test.rs:4:5
  |
4 |     foo();
  |     ^^^^^^
  |
  = note: #[warn(unused_must_use)] on by default

But what I expected is no warning on the foo call, and an error (or warning) for adding #[must_use] on a function without return value.

In (rare) code like this, where the unit (empty tuple) is returned in an explicit way, I expect no errors nor warnings:

#[must_use]
fn foo() -> () {}
fn main() {
    let _ = foo();
}
@varkor
Copy link
Member

varkor commented Oct 13, 2018

It was decided #[must_use] should act just as on any function in this case: #54828. The "explicit return syntax" is semantically no different from the implicit return type and making that different would be misleading.

@leonardo-m
Copy link
Author

I am not sure that decision is the best, but OK, I close this down.

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

No branches or pull requests

2 participants