diff --git a/bevy_lint/src/callback.rs b/bevy_lint/src/callback.rs index 84b499af..19edd7a0 100644 --- a/bevy_lint/src/callback.rs +++ b/bevy_lint/src/callback.rs @@ -17,6 +17,7 @@ impl Callbacks for BevyLintCallback { crate::lints::register_lints(store); crate::lints::register_passes(store); + crate::groups::register_groups(store); })); } } diff --git a/bevy_lint/src/groups.rs b/bevy_lint/src/groups.rs index ce903a6d..7310d174 100644 --- a/bevy_lint/src/groups.rs +++ b/bevy_lint/src/groups.rs @@ -1,11 +1,14 @@ -use crate::LintGroup; -use rustc_lint::Level; +use crate::{ + lint::{BevyLint, LintGroup}, + lints::LINTS, +}; +use rustc_lint::{Level, LintStore}; /// A group of deny-by-default lints that check for outright wrong or useless code. /// /// These lints are carefully picked to be free of false positives. You should avoid /// `#[allow(...)]`-ing these lints without a _very_ good reason. -pub static CORRECTNESS: LintGroup = LintGroup { +pub static CORRECTNESS: &LintGroup = &LintGroup { name: "bevy::correctness", level: Level::Deny, }; @@ -13,19 +16,19 @@ pub static CORRECTNESS: LintGroup = LintGroup { /// A group similar to [`CORRECTNESS`] that checks for suspicious or usually wrong code. /// /// The linted code may have been written intentionally, but should probably still be fixed. -pub static SUSPICIOUS: LintGroup = LintGroup { +pub static SUSPICIOUS: &LintGroup = &LintGroup { name: "bevy::suspicious", level: Level::Warn, }; /// A group that offers suggestions on how to simplify your code. -pub static COMPLEXITY: LintGroup = LintGroup { +pub static COMPLEXITY: &LintGroup = &LintGroup { name: "bevy::complexity", level: Level::Warn, }; /// A group that suggests how to increase the performance of your code. -pub static PERFORMANCE: LintGroup = LintGroup { +pub static PERFORMANCE: &LintGroup = &LintGroup { name: "bevy::performance", level: Level::Warn, }; @@ -33,7 +36,7 @@ pub static PERFORMANCE: LintGroup = LintGroup { /// A group of lints that encourage idiomatic code. /// /// These lints are opinionated and may be freely disabled if you disagree with their suggestions. -pub static STYLE: LintGroup = LintGroup { +pub static STYLE: &LintGroup = &LintGroup { name: "bevy::style", level: Level::Warn, }; @@ -42,7 +45,7 @@ pub static STYLE: LintGroup = LintGroup { /// /// If you enable this group, expect to liberally apply `#[allow(...)]` attributes throughout your /// code. -pub static PEDANTIC: LintGroup = LintGroup { +pub static PEDANTIC: &LintGroup = &LintGroup { name: "bevy::pedantic", level: Level::Allow, }; @@ -52,13 +55,40 @@ pub static PEDANTIC: LintGroup = LintGroup { /// These are designed for scenarios where you want to increase the consistency of your code-base /// and reject certain patterns. They should not all be enabled at once, but instead specific lints /// should be individually enabled. -pub static RESTRICTION: LintGroup = LintGroup { +pub static RESTRICTION: &LintGroup = &LintGroup { name: "bevy::restriction", level: Level::Allow, }; /// A group of unstable lints that may be removed at any time for any reason. -pub static NURSERY: LintGroup = LintGroup { +pub static NURSERY: &LintGroup = &LintGroup { name: "bevy::nursery", level: Level::Allow, }; + +static GROUPS: &[&LintGroup] = &[ + CORRECTNESS, + SUSPICIOUS, + COMPLEXITY, + PERFORMANCE, + STYLE, + PEDANTIC, + RESTRICTION, + NURSERY, +]; + +pub(crate) fn register_groups(store: &mut LintStore) { + for &group in GROUPS { + let lints = LINTS + .iter() + .copied() + // Only select lints of this specified group. + .filter(|l| l.group == group) + // Convert the lints into their `LintId`s. + .map(BevyLint::id) + // Collect into a `Vec`. + .collect(); + + store.register_group(true, group.name, None, lints); + } +} diff --git a/bevy_lint/src/lib.rs b/bevy_lint/src/lib.rs index 8f7197c9..fa4a19f9 100644 --- a/bevy_lint/src/lib.rs +++ b/bevy_lint/src/lib.rs @@ -21,7 +21,4 @@ mod lint; pub mod lints; mod paths; -pub use self::{ - callback::BevyLintCallback, - lint::{BevyLint, LintGroup}, -}; +pub use self::callback::BevyLintCallback; diff --git a/bevy_lint/src/lint.rs b/bevy_lint/src/lint.rs index 48d9608f..2fa78c58 100644 --- a/bevy_lint/src/lint.rs +++ b/bevy_lint/src/lint.rs @@ -1,14 +1,22 @@ -use rustc_lint::{Level, Lint}; +use rustc_lint::{Level, Lint, LintId}; /// A Bevy lint definition and its associated group. /// /// The level of the lint must be the same as the level of the group. +#[derive(Debug)] pub struct BevyLint { pub lint: &'static Lint, pub group: &'static LintGroup, } +impl BevyLint { + pub fn id(&self) -> LintId { + LintId::of(self.lint) + } +} + /// Represents a lint group. +#[derive(PartialEq, Debug)] pub struct LintGroup { /// The name of the lint group. /// @@ -21,6 +29,7 @@ pub struct LintGroup { } #[macro_export] +#[doc(hidden)] macro_rules! declare_bevy_lint { { $(#[$attr:meta])* diff --git a/bevy_lint/src/lints/mod.rs b/bevy_lint/src/lints/mod.rs index e63e5ac6..d400c46a 100644 --- a/bevy_lint/src/lints/mod.rs +++ b/bevy_lint/src/lints/mod.rs @@ -1,10 +1,10 @@ -use crate::BevyLint; +use crate::lint::BevyLint; use rustc_lint::{Lint, LintStore}; pub mod insert_event_resource; pub mod main_return_without_appexit; -pub static LINTS: &[&BevyLint] = &[ +pub(crate) static LINTS: &[&BevyLint] = &[ insert_event_resource::INSERT_EVENT_RESOURCE, main_return_without_appexit::MAIN_RETURN_WITHOUT_APPEXIT, ];