Skip to content

Commit

Permalink
feat: panicking_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAlbrecht committed Feb 8, 2025
1 parent 09c726e commit 60d70dc
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 83 deletions.
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy::prelude::*;
extern crate proc_macros;
use proc_macros::external;

macro_rules! bad_macro {
macro_rules! local_macro {
($q:expr) => {
$q.single()
//~^ ERROR: called a `Query` method that can panic when a non-panicking alternative exists
Expand Down Expand Up @@ -65,5 +65,5 @@ fn my_system(mut query: Query<&mut Foo>) {
query.single();
}
});
bad_macro!(query);
local_macro!(query);
}
6 changes: 3 additions & 3 deletions bevy_lint/tests/ui/panicking_methods/query.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ error: called a `Query` method that can panic when a non-panicking alternative e
14 | $q.single()
| ^^^^^^^^
...
68 | bad_macro!(query);
| ----------------- in this macro invocation
68 | local_macro!(query);
| ------------------- in this macro invocation
|
= help: use `query.get_single()` and handle the `Option` or `Result`
= note: this error originates in the macro `bad_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `local_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 9 previous errors

23 changes: 23 additions & 0 deletions bevy_lint/tests/ui/panicking_methods/query_state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@aux-build:../auxiliary/proc_macros.rs
//! This tests the `panicking_query_methods` lint, specifically when triggered on the `QueryState`
//! type.
Expand All @@ -6,6 +7,21 @@
#![deny(bevy::panicking_query_methods)]

use bevy::prelude::*;
extern crate proc_macros;
use proc_macros::external;

macro_rules! local_macro {
() => {
let mut world = World::new();

let mut query_state = QueryState::<&mut Foo>::new(&mut world);

let _ = query_state.single(&world);
//~^ ERROR: called a `QueryState` method that can panic when a non-panicking alternative
// exists
//~^^^ HELP: use `query_state.get_single(&world)`
};
}

#[derive(Component)]
struct Foo;
Expand All @@ -30,4 +46,11 @@ fn main() {
QueryState::single_mut(&mut query_state, &mut world);
//~^ ERROR: called a `QueryState` method that can panic when a non-panicking alternative exists
//~| HELP: use `QueryState::get_single_mut(&mut query_state, &mut world)`

external!({
let mut world = World::new();
let mut query_state = QueryState::<&mut Foo>::new(&mut world);
let _ = query_state.single(&world);
});
local_macro!();
}
34 changes: 23 additions & 11 deletions bevy_lint/tests/ui/panicking_methods/query_state.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:18:25
--> tests/ui/panicking_methods/query_state.rs:34:25
|
18 | let _ = query_state.single(&world);
34 | let _ = query_state.single(&world);
| ^^^^^^^^^^^^^^
|
= help: use `query_state.get_single(&world)` and handle the `Option` or `Result`
note: the lint level is defined here
--> tests/ui/panicking_methods/query_state.rs:6:9
--> tests/ui/panicking_methods/query_state.rs:7:9
|
6 | #![deny(bevy::panicking_query_methods)]
7 | #![deny(bevy::panicking_query_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:22:13
--> tests/ui/panicking_methods/query_state.rs:38:13
|
22 | let _ = QueryState::single(&mut query_state, &world);
38 | let _ = QueryState::single(&mut query_state, &world);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `QueryState::get_single(&mut query_state, &world)` and handle the `Option` or `Result`

error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:26:17
--> tests/ui/panicking_methods/query_state.rs:42:17
|
26 | query_state.single_mut(&mut world);
42 | query_state.single_mut(&mut world);
| ^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `query_state.get_single_mut(&mut world)` and handle the `Option` or `Result`

error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:30:5
--> tests/ui/panicking_methods/query_state.rs:46:5
|
30 | QueryState::single_mut(&mut query_state, &mut world);
46 | QueryState::single_mut(&mut query_state, &mut world);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `QueryState::get_single_mut(&mut query_state, &mut world)` and handle the `Option` or `Result`

error: aborting due to 4 previous errors
error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:19:29
|
19 | let _ = query_state.single(&world);
| ^^^^^^^^^^^^^^
...
55 | local_macro!();
| -------------- in this macro invocation
|
= help: use `query_state.get_single(&world)` and handle the `Option` or `Result`
= note: this error originates in the macro `local_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 5 previous errors

25 changes: 25 additions & 0 deletions bevy_lint/tests/ui/panicking_methods/world.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//@aux-build:../auxiliary/proc_macros.rs
//! This tests the `panicking_query_methods` lint, specifically when triggered on the `World` type.
#![feature(register_tool)]
#![register_tool(bevy)]
#![deny(bevy::panicking_world_methods)]

use bevy::prelude::*;
extern crate proc_macros;
use proc_macros::external;

#[derive(Component)]
struct Bob;
Expand All @@ -15,6 +18,22 @@ struct Jeffrey;
// A non-send resource.
struct Patrick;

macro_rules! local_macro {
() => {
let mut world = World::new();

let bob = world.spawn(Bob).id();

world.entity(bob);
//~^ ERROR: called a `World` method that can panic when a non-panicking alternative exists
//~| HELP: use `world.get_entity(bob)`

World::entity(&world, bob);
//~^ ERROR: called a `World` method that can panic when a non-panicking alternative exists
//~| HELP: use `World::get_entity(&world, bob)`
};
}

fn main() {
let mut world = World::new();

Expand Down Expand Up @@ -123,4 +142,10 @@ fn main() {
World::schedule_scope(&mut world, Update, |_world, _schedule| {});
//~^ ERROR: called a `World` method that can panic when a non-panicking alternative exists
//~| HELP: use `World::try_schedule_scope(&mut world, Update, |_world, _schedule| {})`
external!({
let mut world = World::new();
let bob = world.spawn(Bob).id();
world.entity(bob);
});
local_macro!();
}
Loading

0 comments on commit 60d70dc

Please sign in to comment.