Skip to content

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

Closed
@leonardo-m

Description

@leonardo-m

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions