Skip to content

Commit

Permalink
feat: merge panicking method lints
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAlbrecht committed Feb 13, 2025
1 parent 8d58625 commit 6ea9231
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 39 deletions.
3 changes: 1 addition & 2 deletions bevy_lint/src/lints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub(crate) static LINTS: &[&BevyLint] = &[
insert_unit_bundle::INSERT_UNIT_BUNDLE,
main_return_without_appexit::MAIN_RETURN_WITHOUT_APPEXIT,
missing_reflect::MISSING_REFLECT,
panicking_methods::PANICKING_QUERY_METHODS,
panicking_methods::PANICKING_WORLD_METHODS,
panicking_methods::PANICKING_METHODS,
plugin_not_ending_in_plugin::PLUGIN_NOT_ENDING_IN_PLUGIN,
zst_query::ZST_QUERY,
];
Expand Down
30 changes: 5 additions & 25 deletions bevy_lint/src/lints/panicking_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
//! For instance, this will lint against `Query::single()`, recommending that `Query::get_single()`
//! should be used instead.
//!
//! This lint is actually two: [`PANICKING_QUERY_METHODS`] and [`PANICKING_WORLD_METHODS`]. Each
//! can be toggled separately. The query variant lints for `Query` and `QueryState`, while the
//! world variant lints for `World`.
//!
//! # Motivation
//!
//! Panicking is the nuclear option of error handling in Rust: it is meant for cases where recovery
Expand Down Expand Up @@ -86,24 +82,18 @@ use clippy_utils::{
ty::match_type,
};
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass, Lint};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_span::Symbol;

declare_bevy_lint! {
pub PANICKING_QUERY_METHODS,
RESTRICTION,
"called a `Query` or `QueryState` method that can panic when a non-panicking alternative exists",
}

declare_bevy_lint! {
pub PANICKING_WORLD_METHODS,
pub PANICKING_METHODS,
RESTRICTION,
"called a `World` method that can panic when a non-panicking alternative exists",
"called a method that can panic when a non-panicking alternative exists",
}

declare_bevy_lint_pass! {
pub PanickingMethods => [PANICKING_QUERY_METHODS.lint, PANICKING_WORLD_METHODS.lint],
pub PanickingMethods => [PANICKING_METHODS.lint],
}

impl<'tcx> LateLintPass<'tcx> for PanickingMethods {
Expand Down Expand Up @@ -220,7 +210,7 @@ impl<'tcx> LateLintPass<'tcx> for PanickingMethods {

span_lint_and_help(
cx,
panicking_type.lint(),
PANICKING_METHODS.lint,
span,
format!(
"called a `{}` method that can panic when a non-panicking alternative exists",
Expand Down Expand Up @@ -295,14 +285,4 @@ impl PanickingType {
Self::World => "World",
}
}

/// Returns the [`Lint`] associated with this panicking type.
///
/// This can either return [`PANICKING_QUERY_METHODS`] or [`PANICKING_WORLD_METHODS`].
fn lint(&self) -> &'static Lint {
match self {
Self::Query | Self::QueryState => PANICKING_QUERY_METHODS.lint,
Self::World => PANICKING_WORLD_METHODS.lint,
}
}
}
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This tests the `panicking_query_methods` lint, specifically when triggered on the `Query` type.
//! This tests the `panicking_methods` lint, specifically when triggered on the `Query` type.
#![feature(register_tool)]
#![register_tool(bevy)]
#![deny(bevy::panicking_query_methods)]
#![deny(bevy::panicking_methods)]

use bevy::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/query.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ error: called a `Query` method that can panic when a non-panicking alternative e
note: the lint level is defined here
--> tests/ui/panicking_methods/query.rs:5:9
|
5 | #![deny(bevy::panicking_query_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | #![deny(bevy::panicking_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: called a `Query` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query.rs:21:5
Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/query_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! This tests the `panicking_query_methods` lint, specifically when triggered on the `QueryState`
//! This tests the `panicking_methods` lint, specifically when triggered on the `QueryState`
//! type.
#![feature(register_tool)]
#![register_tool(bevy)]
#![deny(bevy::panicking_query_methods)]
#![deny(bevy::panicking_methods)]

use bevy::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/query_state.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ error: called a `QueryState` method that can panic when a non-panicking alternat
note: the lint level is defined here
--> tests/ui/panicking_methods/query_state.rs:6:9
|
6 | #![deny(bevy::panicking_query_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 | #![deny(bevy::panicking_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: called a `QueryState` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/query_state.rs:22:13
Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/world.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This tests the `panicking_query_methods` lint, specifically when triggered on the `World` type.
//! This tests the `panicking_methods` lint, specifically when triggered on the `World` type.
#![feature(register_tool)]
#![register_tool(bevy)]
#![deny(bevy::panicking_world_methods)]
#![deny(bevy::panicking_methods)]

use bevy::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/tests/ui/panicking_methods/world.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ error: called a `World` method that can panic when a non-panicking alternative e
note: the lint level is defined here
--> tests/ui/panicking_methods/world.rs:5:9
|
5 | #![deny(bevy::panicking_world_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | #![deny(bevy::panicking_methods)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: called a `World` method that can panic when a non-panicking alternative exists
--> tests/ui/panicking_methods/world.rs:27:5
Expand Down

0 comments on commit 6ea9231

Please sign in to comment.