From 95eb97ef9e57c7991b81fbdd8f8e0dea37024b30 Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:19:20 -0400 Subject: [PATCH 1/4] fix: register the created lint groups --- bevy_lint/src/callback.rs | 1 + bevy_lint/src/groups.rs | 47 ++++++++++++++++++++++++++++++--------- bevy_lint/src/lint.rs | 9 +++++++- 3 files changed, 46 insertions(+), 11 deletions(-) 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..e6b28785 100644 --- a/bevy_lint/src/groups.rs +++ b/bevy_lint/src/groups.rs @@ -1,11 +1,11 @@ -use crate::LintGroup; -use rustc_lint::Level; +use crate::{lints::LINTS, BevyLint, LintGroup}; +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 +13,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 +33,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 +42,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 +52,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 + .into_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/lint.rs b/bevy_lint/src/lint.rs index 48d9608f..77320185 100644 --- a/bevy_lint/src/lint.rs +++ b/bevy_lint/src/lint.rs @@ -1,4 +1,4 @@ -use rustc_lint::{Level, Lint}; +use rustc_lint::{Level, Lint, LintId}; /// A Bevy lint definition and its associated group. /// @@ -8,7 +8,14 @@ pub struct BevyLint { pub group: &'static LintGroup, } +impl BevyLint { + pub fn id(&self) -> LintId { + LintId::of(self.lint) + } +} + /// Represents a lint group. +#[derive(PartialEq)] pub struct LintGroup { /// The name of the lint group. /// From a2f6c864138aec3ea777921fb097a8da38607f3a Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:21:11 -0400 Subject: [PATCH 2/4] feat: implement `Debug` for `BevyLint` and `LintGroup` --- bevy_lint/src/lint.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bevy_lint/src/lint.rs b/bevy_lint/src/lint.rs index 77320185..f2015e37 100644 --- a/bevy_lint/src/lint.rs +++ b/bevy_lint/src/lint.rs @@ -3,6 +3,7 @@ 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, @@ -15,7 +16,7 @@ impl BevyLint { } /// Represents a lint group. -#[derive(PartialEq)] +#[derive(PartialEq, Debug)] pub struct LintGroup { /// The name of the lint group. /// From 45bc49c400199e296c90f8fe068f59652f390424 Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:29:06 -0400 Subject: [PATCH 3/4] chore!: restrict what is public --- bevy_lint/src/groups.rs | 5 ++++- bevy_lint/src/lib.rs | 5 +---- bevy_lint/src/lint.rs | 1 + bevy_lint/src/lints/mod.rs | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bevy_lint/src/groups.rs b/bevy_lint/src/groups.rs index e6b28785..714ca437 100644 --- a/bevy_lint/src/groups.rs +++ b/bevy_lint/src/groups.rs @@ -1,4 +1,7 @@ -use crate::{lints::LINTS, BevyLint, LintGroup}; +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. 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 f2015e37..2fa78c58 100644 --- a/bevy_lint/src/lint.rs +++ b/bevy_lint/src/lint.rs @@ -29,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, ]; From 5547453059bf0f711ce90707c3dd06c5111d146c Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:29:33 -0400 Subject: [PATCH 4/4] fix: clippy --- bevy_lint/src/groups.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bevy_lint/src/groups.rs b/bevy_lint/src/groups.rs index 714ca437..7310d174 100644 --- a/bevy_lint/src/groups.rs +++ b/bevy_lint/src/groups.rs @@ -80,7 +80,7 @@ static GROUPS: &[&LintGroup] = &[ pub(crate) fn register_groups(store: &mut LintStore) { for &group in GROUPS { let lints = LINTS - .into_iter() + .iter() .copied() // Only select lints of this specified group. .filter(|l| l.group == group)