Skip to content

#[must_use] is permitted on functions without return type #54828

@abonander

Description

@abonander
Contributor

The following produces no warnings or errors:

#[must_use = "function with no return type"]
fn foo() {}

fn main() {
    foo();
}

Is this intended? Shouldn't it produce a lint warning on the attribute? This is a good mentoring issue, I can bang out instructions once I get an answer (fortunately this doesn't seem to involve hygiene this time so hopefully I should get it right... @petrochenkov)

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Oct 5, 2018
zackmdavis

zackmdavis commented on Oct 5, 2018

@zackmdavis
Member

The fact that unused-must-use doesn't fire on the foo(); is a consequence of the lint pass returning early for () and !.

I agree that it's useless to put #[must_use] on a function that returns (), but I don't think anyone thought ahead and decided it should (or shouldn't) be an error. (I wouldn't want to write a dedicated lint for such an edge-case, and I think these days we tend to frown on non-lint warnings on account of their unsilenceability.)

#48486 is a similar issue with trait implementations.

zackmdavis

zackmdavis commented on Oct 5, 2018

@zackmdavis
Member

I wouldn't want to write a dedicated lint for such an edge-case

... well, come to think of it, I see a good case that this should trigger unused_attributes, as @petrochenkov recently argued with respect to empty #[derive()]s.

Havvy

Havvy commented on Oct 5, 2018

@Havvy
Contributor

I concur. This should definitely cause unused_attributes to fire.

Centril

Centril commented on Oct 6, 2018

@Centril
Contributor

Should the lint unused_attributes also fire for an arbitrary unit type in that case?
There should be some consistency in the behavior.

hanna-kruppe

hanna-kruppe commented on Oct 6, 2018

@hanna-kruppe
Contributor

By "unit type" do you mean struct Foo;? I'd say no, must_use on user-defined types is always potentially sensible, e.g. ZSTs can be used as tokens for something that the caller should not usually forget to do.

Centril

Centril commented on Oct 6, 2018

@Centril
Contributor

@rkruppe I mean any terminal object in the category of Rust types, so struct Foo;, ((), ()), enum Foo { Bar }, and so on.

abonander

abonander commented on Oct 6, 2018

@abonander
ContributorAuthor

I would say an explicit () and any arity of tuple that's all ().

Centril

Centril commented on Oct 6, 2018

@Centril
Contributor

@abonander including recursively? e.g ((), ((), ()))

abonander

abonander commented on Oct 7, 2018

@abonander
ContributorAuthor

Probably, because those types typically don't have any meaning or semantics that can be added to them by the user. I guess it's possible with extension traits but that's going to be a very weird corner case.

Centril

Centril commented on Oct 7, 2018

@Centril
Contributor

I'm down with that. It seems to me not too arbitrary a rule.

On the other hand, if we could get #[must_use] on all types to emit a warning that could work as well.

abonander

abonander commented on Oct 7, 2018

@abonander
ContributorAuthor

Like @rkruppe said, custom ZSTs can have semantics attached to them so I don't think it's good to warn on #[must_use] for those.

hanna-kruppe

hanna-kruppe commented on Oct 7, 2018

@hanna-kruppe
Contributor

I was skeptical about linting even () but forgetting to fill in the return type is at least a plausible scenario where such a lint could help. Going any further does not seem likely to help anyone in any scenario. Types like ((), ()) are rarely even constructed much less written in function signatures.

26 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pnkfelix@Havvy@Centril@zackmdavis@hanna-kruppe

        Issue actions

          #[must_use] is permitted on functions without return type · Issue #54828 · rust-lang/rust