From 8d8b85158b76c14a4d6e586178bb82d043c1178f Mon Sep 17 00:00:00 2001 From: DaAlbrecht Date: Sun, 24 Nov 2024 16:25:01 +0100 Subject: [PATCH 1/2] feat: duplicated bevy version lint --- .../src/lints/duplicate_bevy_dependencies.rs | 38 +++++++++++++++++++ bevy_lint/src/lints/mod.rs | 3 ++ 2 files changed, 41 insertions(+) create mode 100644 bevy_lint/src/lints/duplicate_bevy_dependencies.rs diff --git a/bevy_lint/src/lints/duplicate_bevy_dependencies.rs b/bevy_lint/src/lints/duplicate_bevy_dependencies.rs new file mode 100644 index 0000000..54bb6a3 --- /dev/null +++ b/bevy_lint/src/lints/duplicate_bevy_dependencies.rs @@ -0,0 +1,38 @@ +//! Checks for multiple versions of `bevy` in the dependencies. +//! +//! # Motivation +//! +//! When different third party crates use incompatible versions of Bevy, it can lead to confusing +//! errors and type incompatibilities. + +use crate::declare_bevy_lint; +use clippy_utils::{diagnostics::span_lint, find_crates}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::declare_lint_pass; +use rustc_span::Symbol; + +declare_bevy_lint! { + pub DUPLICATE_BEVY_DEPENDENCIES, + CORRECTNESS, + "duplicate bevy dependencies", +} + +declare_lint_pass! { + DuplicateBevyDependencies => [DUPLICATE_BEVY_DEPENDENCIES.lint] +} + +impl<'tcx> LateLintPass<'tcx> for DuplicateBevyDependencies { + fn check_crate(&mut self, cx: &LateContext<'tcx>) { + let bevy_crates = find_crates(cx.tcx, Symbol::intern("bevy")); + + if bevy_crates.len() > 1 { + let span = cx.tcx.def_span(bevy_crates[1].def_id()); + span_lint( + cx, + DUPLICATE_BEVY_DEPENDENCIES.lint, + span, + "Multiple versions of `bevy` found", + ); + } + } +} diff --git a/bevy_lint/src/lints/mod.rs b/bevy_lint/src/lints/mod.rs index 22b9012..22ed4c9 100644 --- a/bevy_lint/src/lints/mod.rs +++ b/bevy_lint/src/lints/mod.rs @@ -7,6 +7,7 @@ use crate::lint::BevyLint; use rustc_lint::{Lint, LintStore}; +pub mod duplicate_bevy_dependencies; pub mod insert_event_resource; pub mod main_return_without_appexit; pub mod missing_reflect; @@ -22,6 +23,7 @@ pub(crate) static LINTS: &[&BevyLint] = &[ panicking_methods::PANICKING_WORLD_METHODS, plugin_not_ending_in_plugin::PLUGIN_NOT_ENDING_IN_PLUGIN, zst_query::ZST_QUERY, + duplicate_bevy_dependencies::DUPLICATE_BEVY_DEPENDENCIES, ]; pub(crate) fn register_lints(store: &mut LintStore) { @@ -36,4 +38,5 @@ pub(crate) fn register_passes(store: &mut LintStore) { store.register_late_pass(|_| Box::new(panicking_methods::PanickingMethods)); store.register_late_pass(|_| Box::new(plugin_not_ending_in_plugin::PluginNotEndingInPlugin)); store.register_late_pass(|_| Box::new(zst_query::ZstQuery)); + store.register_late_pass(|_| Box::new(duplicate_bevy_dependencies::DuplicateBevyDependencies)); } From 02c9e73ed8903e24ae1bbf297e864c86b6cd10f4 Mon Sep 17 00:00:00 2001 From: DaAlbrecht Date: Sun, 1 Dec 2024 16:54:32 +0100 Subject: [PATCH 2/2] feat: cache 'bevy' symbol --- .../src/lints/duplicate_bevy_dependencies.rs | 20 +++++++++++++++---- bevy_lint/src/lints/mod.rs | 4 +++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bevy_lint/src/lints/duplicate_bevy_dependencies.rs b/bevy_lint/src/lints/duplicate_bevy_dependencies.rs index 54bb6a3..d66ed3b 100644 --- a/bevy_lint/src/lints/duplicate_bevy_dependencies.rs +++ b/bevy_lint/src/lints/duplicate_bevy_dependencies.rs @@ -6,9 +6,9 @@ //! errors and type incompatibilities. use crate::declare_bevy_lint; -use clippy_utils::{diagnostics::span_lint, find_crates}; +use clippy_utils::{diagnostics::span_lint, find_crates, sym}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_session::declare_lint_pass; +use rustc_session::impl_lint_pass; use rustc_span::Symbol; declare_bevy_lint! { @@ -17,13 +17,25 @@ declare_bevy_lint! { "duplicate bevy dependencies", } -declare_lint_pass! { +pub(crate) struct DuplicateBevyDependencies { + bevy_symbol: Symbol, +} + +impl Default for DuplicateBevyDependencies { + fn default() -> Self { + Self { + bevy_symbol: sym!(bevy), + } + } +} + +impl_lint_pass! { DuplicateBevyDependencies => [DUPLICATE_BEVY_DEPENDENCIES.lint] } impl<'tcx> LateLintPass<'tcx> for DuplicateBevyDependencies { fn check_crate(&mut self, cx: &LateContext<'tcx>) { - let bevy_crates = find_crates(cx.tcx, Symbol::intern("bevy")); + let bevy_crates = find_crates(cx.tcx, self.bevy_symbol); if bevy_crates.len() > 1 { let span = cx.tcx.def_span(bevy_crates[1].def_id()); diff --git a/bevy_lint/src/lints/mod.rs b/bevy_lint/src/lints/mod.rs index 22ed4c9..4af1153 100644 --- a/bevy_lint/src/lints/mod.rs +++ b/bevy_lint/src/lints/mod.rs @@ -38,5 +38,7 @@ pub(crate) fn register_passes(store: &mut LintStore) { store.register_late_pass(|_| Box::new(panicking_methods::PanickingMethods)); store.register_late_pass(|_| Box::new(plugin_not_ending_in_plugin::PluginNotEndingInPlugin)); store.register_late_pass(|_| Box::new(zst_query::ZstQuery)); - store.register_late_pass(|_| Box::new(duplicate_bevy_dependencies::DuplicateBevyDependencies)); + store.register_late_pass(|_| { + Box::new(duplicate_bevy_dependencies::DuplicateBevyDependencies::default()) + }); }