Skip to content

Make App be #[must_use]#11947

Closed
doonv wants to merge 1 commit intobevyengine:mainfrom
doonv:app-must-use
Closed

Make App be #[must_use]#11947
doonv wants to merge 1 commit intobevyengine:mainfrom
doonv:app-must-use

Conversation

@doonv
Copy link
Copy Markdown
Contributor

@doonv doonv commented Feb 18, 2024

Objective

Make it harder to use App incorrectly.

Solution

Add the #[must_use] attribute to App, this prevents you from creating an App and not using it.


Changelog

  • Add #[must_use] attribute to App.

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh clever. Yeah, this will catch the "I forget to call run" problem that beginners hit sometimes!

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use A-App Bevy apps and plugins and removed A-ECS Entities, components, systems, and events labels Feb 18, 2024
@alice-i-cecile alice-i-cecile added this to the 0.14 milestone Feb 18, 2024
@tim-blackbird
Copy link
Copy Markdown
Contributor

Oh clever. Yeah, this will catch the "I forget to call run" problem that beginners hit sometimes!

It won't help with that, sadly :(
I think this only adds a warning if you do something silly like:

App::new();

Not worth adding in my opinion. Unless, there's some other example in which this helps, @doonv?

@doonv
Copy link
Copy Markdown
Contributor Author

doonv commented Feb 18, 2024

Oh clever. Yeah, this will catch the "I forget to call run" problem that beginners hit sometimes!

It won't help with that, sadly :( I think this only adds a warning if you do something silly like:

App::new();

Not worth adding in my opinion. Unless, there's some other example in which this helps, @doonv?

Yeah unfortunately there's not much you can do about that apart from making App's methods also #[must_use], but that comes at the cost of not allowing non-chained method calls.

Making App #[must_use] still helps in some rare situations though, there's no reason not to add it.

@alice-i-cecile alice-i-cecile removed this from the 0.14 milestone Feb 18, 2024
@pablo-lua
Copy link
Copy Markdown
Contributor

pablo-lua commented Feb 18, 2024

Would be good to add must_use in App::update. that would say to the user that they must use either update or run (as run call update internally)
But this can be a bit tricky, maybe the Runner isn't sure that it'll call update 🤔

@hymm
Copy link
Copy Markdown
Contributor

hymm commented Feb 18, 2024

prior work #5395

@alice-i-cecile alice-i-cecile added the X-Needs-SME This type of work requires an SME to approve it. label Feb 18, 2024
@amytimed
Copy link
Copy Markdown
Contributor

bors r+

@andriyDev
Copy link
Copy Markdown
Contributor

andriyDev commented Feb 18, 2024

Oh clever. Yeah, this will catch the "I forget to call run" problem that beginners hit sometimes!

It won't help with that, sadly :( I think this only adds a warning if you do something silly like:

App::new();

Not worth adding in my opinion. Unless, there's some other example in which this helps, @doonv?

I'm pretty sure this is wrong. Since nearly all App functions take an owned App and returns an App, the returned App will also be must_use. So even if you do: App::new().add_system(Update, system1).add_system(Update, system2);, it will still complain. It will only be satisfied if you call .run() or store it in a variable.

I tested this out with a very simple example:

my-bevy/src/lib.rs

#[must_use]
pub struct MyBuilder;

impl MyBuilder {
    pub fn new() -> Self {
        Self
    }

    pub fn a(self) -> Self {
        self
    }
    pub fn b(self) -> Self {
        self
    }
    pub fn c(self) -> Self {
        self
    }
}

my-bin/src/main.rs

use my_bevy::MyBuilder;

fn main() {
    MyBuilder::new().a().b().c();
}

This still gives me warnings about my unused MyBuilder.

Edit: Therefore, App should also give the same warnings with this PR.

@tim-blackbird
Copy link
Copy Markdown
Contributor

tim-blackbird commented Feb 18, 2024

The methods on App take and return &mut Self, not Self.
App::new().add_system(Update, system); won't trigger the warning from #[must_use] because of that, I did test this before I commented before :)

Adding #[must_use] to a type also adds this warning to any API that returns the type downstream.
I'd still err on not adding it to avoid any potential bother that might cause or any confusion about why App would be marked as #[must_use].

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

Labels

A-App Bevy apps and plugins C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants