Skip to content

Commit

Permalink
Actually *register* the lint groups (#106)
Browse files Browse the repository at this point in the history
Looks like I forgot to register the lint groups introduced in #98.
Whoops!

While I'm at it, I also changed the visibility of some items so that
less is public that before.
  • Loading branch information
BD103 authored Sep 25, 2024
1 parent 23bb966 commit 1dd2304
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions bevy_lint/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Callbacks for BevyLintCallback {

crate::lints::register_lints(store);
crate::lints::register_passes(store);
crate::groups::register_groups(store);
}));
}
}
50 changes: 40 additions & 10 deletions bevy_lint/src/groups.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
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,
};

/// 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,
};

/// 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,
};
Expand All @@ -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,
};
Expand All @@ -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);
}
}
5 changes: 1 addition & 4 deletions bevy_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ mod lint;
pub mod lints;
mod paths;

pub use self::{
callback::BevyLintCallback,
lint::{BevyLint, LintGroup},
};
pub use self::callback::BevyLintCallback;
11 changes: 10 additions & 1 deletion bevy_lint/src/lint.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -21,6 +29,7 @@ pub struct LintGroup {
}

#[macro_export]
#[doc(hidden)]
macro_rules! declare_bevy_lint {
{
$(#[$attr:meta])*
Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/src/lints/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
];
Expand Down

0 comments on commit 1dd2304

Please sign in to comment.