Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ jobs:
sweep-cache: true

- name: Run Clippy
env:
# Although we don't use any unstable options, this enables `rustc::internal` lints.
RUSTFLAGS: -Zunstable-options
run: cargo clippy --workspace --all-targets ${{ matrix.features }} -- --deny warnings

rustfmt:
Expand Down
9 changes: 8 additions & 1 deletion bevy_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
#![feature(rustc_private)]
// Allows chaining `if let` multiple times using `&&`.
#![feature(let_chains)]
// Warn on internal `rustc` lints that check for poor usage of internal compiler APIs.
// Warn on internal `rustc` lints that check for poor usage of internal compiler APIs. Note that
// you also need to pass `-Z unstable-options` to `rustc` for this to be enabled:
// `RUSTFLAGS="-Zunstable-options" cargo check`
#![warn(rustc::internal)]
#![allow(
rustc::usage_of_ty_tykind,
reason = "Many false positives without a valid replacement."
)]

// This is a list of every single `rustc` crate used within this library. If you need another, add
// it here!
extern crate rustc_abi;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_hir;
Expand Down
23 changes: 13 additions & 10 deletions bevy_lint/src/lints/duplicate_bevy_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@
//! leafwing-input-manager = "0.16"
//! ```

use std::{
collections::{BTreeMap, HashMap},
ops::Range,
path::Path,
str::FromStr,
sync::Arc,
};
use std::{collections::BTreeMap, ops::Range, path::Path, str::FromStr, sync::Arc};

use crate::declare_bevy_lint;
use cargo_metadata::{
Expand All @@ -77,6 +71,7 @@ use clippy_utils::{
diagnostics::{span_lint, span_lint_and_then},
find_crates,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_lint::LateContext;
use rustc_span::{BytePos, Pos, SourceFile, Span, Symbol, SyntaxContext};
Expand Down Expand Up @@ -121,7 +116,7 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, bevy_symbol: Symb
let local_name = cx.tcx.crate_name(LOCAL_CRATE);

// get the package name and the corresponding version of `bevy` that they depend on
let mut bevy_dependents = HashMap::new();
let mut bevy_dependents = FxHashMap::default();
for package in &metadata.packages {
for dependency in &package.dependencies {
if dependency.name.as_str() == "bevy"
Expand Down Expand Up @@ -155,7 +150,7 @@ fn lint_with_target_version(
cargo_toml: &CargoToml,
file: &Arc<SourceFile>,
bevy_cargo: &Spanned<toml::Value>,
bevy_dependents: &HashMap<&str, VersionReq>,
bevy_dependents: &FxHashMap<&str, VersionReq>,
) {
// Semver only supports checking if a given `VersionReq` matches a `Version` and not if two
// `VersionReq` can successfully resolve to one `Version`. Therefore we try to parse the
Expand All @@ -167,6 +162,10 @@ fn lint_with_target_version(

let bevy_cargo_toml_span = toml_span(bevy_cargo.span(), file);

#[allow(
rustc::potential_query_instability,
reason = "Iterating a hash map may lead to query instability, but the fix is not trivial."
Copy link
Collaborator

@DaAlbrecht DaAlbrecht Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we swap to a BTreeMap?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! I didn't even think of that! I switched it over to BTreeMap. Looks like rustc_data_structures has SortedMap<K, V>, which may be better, but I'll stick to BTreeMap for now.

)]
let mismatching_dependencies = bevy_dependents
.iter()
.filter(|dependency| !dependency.1.matches(&target_version));
Expand All @@ -191,7 +190,7 @@ fn lint_with_target_version(

fn minimal_lint(
cx: &LateContext<'_>,
bevy_dependents: &HashMap<&str, VersionReq>,
bevy_dependents: &FxHashMap<&str, VersionReq>,
resolved: &Resolve,
) {
// Examples of the underlying string representation of resolved crates
Expand All @@ -207,6 +206,10 @@ fn minimal_lint(
}
// Extract versions from external crates
if let Some((id, _)) = node.id.repr.split_once('@') {
#[allow(
rustc::potential_query_instability,
reason = "This is deterministic because we do not depend on the order of keys with `any()`."
)]
if bevy_dependents
.keys()
.any(|crate_name| id.ends_with(crate_name))
Expand Down
4 changes: 2 additions & 2 deletions bevy_lint/src/lints/zst_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'tcx> LateLintPass<'tcx> for ZstQuery {
hir_ty.span,
ZST_QUERY.lint.desc,
None,
query_kind.help(&peeled),
query_kind.help(peeled),
);
}
}
Expand All @@ -127,7 +127,7 @@ impl QueryKind {
}
}

fn help(&self, ty: &Ty<'_>) -> String {
fn help(&self, ty: Ty<'_>) -> String {
// It should be noted that `With<Foo>` is not always the best filter to suggest.
// While it's most often going to be what users want, there's also `Added<Foo>`
// and `Changed<Foo>` which might be more appropriate in some cases
Expand Down