Skip to content

Commit

Permalink
feat: plugin_not_ending_in_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAlbrecht committed Feb 8, 2025
1 parent 60d70dc commit 9aaf92a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
10 changes: 9 additions & 1 deletion bevy_lint/src/lints/plugin_not_ending_in_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
//! ```
use crate::{declare_bevy_lint, declare_bevy_lint_pass};
use clippy_utils::{diagnostics::span_lint_hir_and_then, match_def_path, path_res};
use clippy_utils::{
diagnostics::span_lint_hir_and_then, match_def_path, path_res, source::HasSession,
};
use rustc_errors::Applicability;
use rustc_hir::{def::Res, HirId, Item, ItemKind, OwnerId};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_span::symbol::Ident;

declare_bevy_lint! {
Expand Down Expand Up @@ -95,6 +98,11 @@ impl<'tcx> LateLintPass<'tcx> for PluginNotEndingInPlugin {
return;
};

// skip lint if the struct was defined in an external macro
if in_external_macro(cx.sess(), struct_span) {
return;
}

// If the type's name ends in "Plugin", exit.
if struct_name.as_str().ends_with("Plugin") {
return;
Expand Down
29 changes: 28 additions & 1 deletion bevy_lint/tests/ui/plugin_not_ending_in_plugin/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//@aux-build:../auxiliary/proc_macros.rs
#![feature(register_tool)]
#![register_tool(bevy)]
#![deny(bevy::plugin_not_ending_in_plugin)]
//~^ NOTE: the lint level is defined here

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

// This should raise an error, since it does not end in "Plugin".
struct Foo;
Expand All @@ -30,6 +33,30 @@ impl Plugin for Baz {
fn build(&self, _app: &mut App) {}
}

external! {
// This should _not_ raise an error, since it is defined in an external crate.
struct ExternalBaz;
}

impl Plugin for ExternalBaz {
fn build(&self, _app: &mut App) {}
}

macro_rules! local_macro {
() => {
struct InternalBaz;
//~^ ERROR: implemented `Plugin` for a structure whose name does not end in "Plugin"
//~| HELP: rename the plugin
};
}

local_macro!();

//~v NOTE: `Plugin` implemented here
impl Plugin for InternalBaz {
fn build(&self, _app: &mut App) {}
}

fn main() {
App::new().add_plugins((Foo, BarPlugin, Baz));
App::new().add_plugins((Foo, BarPlugin, Baz, InternalBaz, ExternalBaz));
}
36 changes: 27 additions & 9 deletions bevy_lint/tests/ui/plugin_not_ending_in_plugin/main.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
error: implemented `Plugin` for a structure whose name does not end in "Plugin"
--> tests/ui/plugin_not_ending_in_plugin/main.rs:9:8
--> tests/ui/plugin_not_ending_in_plugin/main.rs:12:8
|
9 | struct Foo;
12 | struct Foo;
| ^^^ help: rename the plugin: `FooPlugin`
|
note: `Plugin` implemented here
--> tests/ui/plugin_not_ending_in_plugin/main.rs:14:1
--> tests/ui/plugin_not_ending_in_plugin/main.rs:17:1
|
14 | / impl Plugin for Foo {
15 | | fn build(&self, _app: &mut App) {}
16 | | }
17 | / impl Plugin for Foo {
18 | | fn build(&self, _app: &mut App) {}
19 | | }
| |_^
note: the lint level is defined here
--> tests/ui/plugin_not_ending_in_plugin/main.rs:3:9
--> tests/ui/plugin_not_ending_in_plugin/main.rs:4:9
|
3 | #![deny(bevy::plugin_not_ending_in_plugin)]
4 | #![deny(bevy::plugin_not_ending_in_plugin)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error
error: implemented `Plugin` for a structure whose name does not end in "Plugin"
--> tests/ui/plugin_not_ending_in_plugin/main.rs:47:16
|
47 | struct InternalBaz;
| ^^^^^^^^^^^ help: rename the plugin: `InternalBazPlugin`
...
53 | local_macro!();
| -------------- in this macro invocation
|
note: `Plugin` implemented here
--> tests/ui/plugin_not_ending_in_plugin/main.rs:56:1
|
56 | / impl Plugin for InternalBaz {
57 | | fn build(&self, _app: &mut App) {}
58 | | }
| |_^
= note: this error originates in the macro `local_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

0 comments on commit 9aaf92a

Please sign in to comment.