Skip to content
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

Add lint: bevy::insert_event_resource #86

Merged
merged 39 commits into from
Sep 19, 2024
Merged

Add lint: bevy::insert_event_resource #86

merged 39 commits into from
Sep 19, 2024

Conversation

BD103
Copy link
Member

@BD103 BD103 commented Sep 18, 2024

This is rebased off of #84, so that will have to be merged first. Closes #40.

This checks for cases where App::init_resource::<Events<T>>() and friends are used instead of App::add_event::<T>(). I used the following code to test it:

use bevy::prelude::*;

#[derive(Event)]
struct MyEvent;

fn main() {
    App::new()
        .insert_resource(foo())
        .init_resource::<Events<MyEvent>>();
}

fn foo() -> Events<MyEvent> {
    Default::default()
}

Put it in bevy_lint/examples/lint_test.rs, cd into bevy_lint, then run cargo build && cargo run -- --example lint_test. The output should look like this:

error: called `App::init_resource::<Events<T>>()` instead of `App::add_event::<T>()`
 --> bevy_lint/examples/lint_test.rs:9:10
  |
9 |         .init_resource::<Events<MyEvent>>();
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(bevy::insert_event_resource)]` on by default
help: inserting an `Events` resource does not fully setup that event
  |
9 |         .add_event::<MyEvent>();
  |          ~~~~~~~~~~~~~~~~~~~~~~

error: called `App::insert_resource(Events<T>)` instead of `App::add_event::<T>()`
 --> bevy_lint/examples/lint_test.rs:8:10
  |
8 |         .insert_resource(foo())
  |          ^^^^^^^^^^^^^^^^^^^^^^
  |
help: inserting an `Events` resource does not fully setup that event
  |
8 |         .add_event::<MyEvent>()
  |          ~~~~~~~~~~~~~~~~~~~~~~

error: could not compile `bevy_lint` (example "lint_test") due to 2 previous errors

`rustfmt` doesn't format the lint modules because it can't find `mod name;` syntax for them. And honestly? This is fine until it becomes incredibly cumbersome.
This puts it all together, so you're less likely to forget to register the pass.
@BD103 BD103 added A-Linter Related to the linter and custom lints C-Feature Make something new possible S-Blocked This cannot move forward until something else changes labels Sep 18, 2024
@BD103 BD103 removed the S-Blocked This cannot move forward until something else changes label Sep 18, 2024
BD103 and others added 2 commits September 19, 2024 08:33
Co-authored-by: Christopher Biscardi <chris@christopherbiscardi.com>
Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

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

Lint is robust to my testing. Treat all of my comments on the PR as very minor opinions, I'm fine either way :)

@BD103
Copy link
Member Author

BD103 commented Sep 19, 2024

I'm going to let this sit for a few hours in case anyone has some last-minute feedback. After that, it's getting merged!

@BD103 BD103 merged commit 18acaab into main Sep 19, 2024
6 checks passed
@BD103 BD103 deleted the init-event-resource branch September 19, 2024 21:45
@BD103 BD103 mentioned this pull request Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Related to the linter and custom lints C-Feature Make something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add lint: .init_resource on Event resource (won't set up cleanup systems)
4 participants