Skip to content

Commit f6b0645

Browse files
committed
fix: use new PathLookup methods
1 parent 18eda6e commit f6b0645

File tree

12 files changed

+59
-75
lines changed

12 files changed

+59
-75
lines changed

bevy_lint/src/lints/cargo.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::declare_bevy_lint_pass;
55
use cargo_metadata::MetadataCommand;
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{config::Input, utils::was_invoked_from_cargo};
8-
use rustc_span::Symbol;
98

109
declare_bevy_lint_pass! {
1110
pub(crate) Cargo => [DUPLICATE_BEVY_DEPENDENCIES],

bevy_lint/src/lints/nursery/duplicate_bevy_dependencies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use clippy_utils::{
7373
};
7474
use rustc_hir::def_id::LOCAL_CRATE;
7575
use rustc_lint::LateContext;
76-
use rustc_span::{BytePos, Pos, SourceFile, Span, Symbol, SyntaxContext};
76+
use rustc_span::{BytePos, Pos, SourceFile, Span, SyntaxContext};
7777
use serde::Deserialize;
7878
use toml::Spanned;
7979

bevy_lint/src/lints/nursery/zst_query.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ use crate::{
5454
declare_bevy_lint, declare_bevy_lint_pass,
5555
utils::hir_parse::{detuple, generic_type_at},
5656
};
57-
use clippy_utils::{
58-
diagnostics::span_lint_and_help,
59-
ty::{match_type, ty_from_hir_ty},
60-
};
57+
use clippy_utils::{diagnostics::span_lint_and_help, ty::ty_from_hir_ty};
6158
use rustc_abi::Size;
6259
use rustc_hir::AmbigArg;
6360
use rustc_lint::{LateContext, LateLintPass};
@@ -123,7 +120,7 @@ enum QueryKind {
123120

124121
impl QueryKind {
125122
fn try_from_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
126-
if match_type(cx, ty, &crate::paths::QUERY) {
123+
if crate::paths::QUERY.matches_ty(cx, ty) {
127124
Some(Self::Query)
128125
} else {
129126
None

bevy_lint/src/lints/pedantic/borrowed_reborrowable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ use std::ops::ControlFlow;
101101
use crate::{declare_bevy_lint, declare_bevy_lint_pass};
102102
use clippy_utils::{
103103
diagnostics::span_lint_and_sugg,
104+
paths::PathLookup,
104105
source::{snippet, snippet_opt},
105-
ty::match_type,
106106
};
107107
use rustc_errors::Applicability;
108108
use rustc_hir::{Body, FnDecl, MutTy, Mutability, intravisit::FnKind};
@@ -255,7 +255,7 @@ impl Reborrowable {
255255
fn try_from_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
256256
use crate::paths::*;
257257

258-
const PATH_MAP: &[(&[&str], Reborrowable)] = &[
258+
static PATH_MAP: &[(&PathLookup, Reborrowable)] = &[
259259
(&COMMANDS, Reborrowable::Commands),
260260
(&DEFERRED, Reborrowable::Deferred),
261261
(&DEFERRED_WORLD, Reborrowable::DeferredWorld),
@@ -271,7 +271,7 @@ impl Reborrowable {
271271
];
272272

273273
for &(path, reborrowable) in PATH_MAP {
274-
if match_type(cx, ty, path) {
274+
if path.matches_ty(cx, ty) {
275275
return Some(reborrowable);
276276
}
277277
}

bevy_lint/src/lints/pedantic/main_return_without_appexit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
use crate::{declare_bevy_lint, declare_bevy_lint_pass, sym, utils::hir_parse::MethodCall};
3737
use clippy_utils::{
3838
diagnostics::span_lint_hir_and_then, is_entrypoint_fn, is_expr_used_or_unified,
39-
ty::match_type, visitors::for_each_expr,
39+
visitors::for_each_expr,
4040
};
4141
use rustc_errors::Applicability;
4242
use rustc_hir::{Body, FnDecl, FnRetTy, Ty, TyKind, def_id::LocalDefId, intravisit::FnKind};
4343
use rustc_lint::{LateContext, LateLintPass};
44-
use rustc_span::{Span, Symbol};
44+
use rustc_span::Span;
4545
use std::ops::ControlFlow;
4646

4747
declare_bevy_lint! {
@@ -94,7 +94,7 @@ impl<'tcx> LateLintPass<'tcx> for MainReturnWithoutAppExit {
9494
let ty = cx.typeck_results().expr_ty(receiver).peel_refs();
9595

9696
// If `src` is a Bevy `App` and the `AppExit` is unused, emit the lint.
97-
if match_type(cx, ty, &crate::paths::APP)
97+
if crate::paths::APP.matches_ty(cx, ty)
9898
&& !is_expr_used_or_unified(cx.tcx, expr)
9999
{
100100
span_lint_hir_and_then(

bevy_lint/src/lints/restriction/missing_reflect.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,15 @@
5656
5757
use crate::{declare_bevy_lint, declare_bevy_lint_pass};
5858
use clippy_utils::{
59-
def_path_res,
6059
diagnostics::span_lint_hir_and_then,
61-
get_trait_def_id,
60+
paths::PathLookup,
6261
sugg::DiagExt,
6362
ty::{implements_trait, ty_from_hir_ty},
6463
};
6564
use rustc_errors::Applicability;
66-
use rustc_hir::{
67-
HirId, Item, ItemKind, Node, OwnerId, QPath, TyKind,
68-
def::{DefKind, Res},
69-
};
65+
use rustc_hir::{HirId, Item, ItemKind, Node, OwnerId, QPath, TyKind, def::DefKind};
7066
use rustc_lint::{LateContext, LateLintPass};
71-
use rustc_middle::{span_bug, ty::TyCtxt};
67+
use rustc_middle::span_bug;
7268
use rustc_span::Span;
7369

7470
declare_bevy_lint! {
@@ -87,36 +83,27 @@ impl<'tcx> LateLintPass<'tcx> for MissingReflect {
8783
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
8884
// Finds all types that implement `Reflect` in this crate.
8985
let reflected: Vec<TraitType> =
90-
TraitType::from_local_crate(cx.tcx, &crate::paths::REFLECT).collect();
86+
TraitType::from_local_crate(cx, &crate::paths::REFLECT).collect();
9187

9288
// Finds all non-`Reflect` types that implement `Event` in this crate.
93-
let events: Vec<TraitType> = TraitType::from_local_crate(cx.tcx, &crate::paths::EVENT)
89+
let events: Vec<TraitType> = TraitType::from_local_crate(cx, &crate::paths::EVENT)
9490
.filter(|trait_type| !reflected.contains(trait_type))
9591
.collect();
9692

9793
// Finds all non-`Reflect` types that implement `Component` and *not* `Event` in this
9894
// crate. Because events are also components, we need to deduplicate the two to avoid
9995
// emitting multiple diagnostics for the same type.
100-
let components: Vec<TraitType> =
101-
TraitType::from_local_crate(cx.tcx, &crate::paths::COMPONENT)
102-
.filter(|trait_type| {
103-
!(reflected.contains(trait_type) || events.contains(trait_type))
104-
})
105-
.collect();
96+
let components: Vec<TraitType> = TraitType::from_local_crate(cx, &crate::paths::COMPONENT)
97+
.filter(|trait_type| !(reflected.contains(trait_type) || events.contains(trait_type)))
98+
.collect();
10699

107100
// Finds all non-`Reflect` types that implement `Resource` in this crate.
108-
let resources: Vec<TraitType> =
109-
TraitType::from_local_crate(cx.tcx, &crate::paths::RESOURCE)
110-
.filter(|trait_type| !reflected.contains(trait_type))
111-
.collect();
101+
let resources: Vec<TraitType> = TraitType::from_local_crate(cx, &crate::paths::RESOURCE)
102+
.filter(|trait_type| !reflected.contains(trait_type))
103+
.collect();
104+
105+
let reflect_trait_def_ids = crate::paths::PARTIAL_REFLECT.get(cx);
112106

113-
// This is an expensive function that is purposefully called outside of the `for` loop. Note
114-
// that this will only return `None` if `PartialReflect` does not exist (e.g. `bevy_reflect`
115-
// is not available.)
116-
let Some(reflect_trait_def_id) = get_trait_def_id(cx.tcx, &crate::paths::PARTIAL_REFLECT)
117-
else {
118-
return;
119-
};
120107
// Emit diagnostics for each of these types.
121108
for (checked_trait, trait_name, message_phrase) in [
122109
(events, "Event", "an event"),
@@ -173,7 +160,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingReflect {
173160
// Check if the field's type implements the `PartialReflect` trait. If it does
174161
// not, change the `Applicability` level to `MaybeIncorrect` because `Reflect`
175162
// cannot be automatically derived.
176-
if !implements_trait(cx, ty, reflect_trait_def_id, &[]) {
163+
if !reflect_trait_def_ids
164+
.iter()
165+
.any(|&trait_id| implements_trait(cx, ty, trait_id, &[]))
166+
{
177167
applicability = Applicability::MaybeIncorrect;
178168
break;
179169
}
@@ -220,34 +210,32 @@ struct TraitType {
220210
}
221211

222212
impl TraitType {
223-
fn from_local_crate<'tcx>(
224-
tcx: TyCtxt<'tcx>,
225-
trait_path: &[&str],
226-
) -> impl Iterator<Item = Self> + use<'tcx> {
213+
fn from_local_crate<'tcx, 'a>(
214+
cx: &'a LateContext<'tcx>,
215+
trait_path: &'a PathLookup,
216+
) -> impl Iterator<Item = Self> + use<'tcx, 'a> {
227217
// Find the `DefId` of the trait. There may be multiple if there are multiple versions of
228218
// the same crate.
229-
let trait_def_ids = def_path_res(tcx, trait_path)
219+
let trait_def_ids = trait_path
220+
.get(cx)
230221
.into_iter()
231-
.filter_map(|res| match res {
232-
Res::Def(DefKind::Trait, def_id) => Some(def_id),
233-
_ => None,
234-
});
222+
.filter(|&def_id| cx.tcx.def_kind(def_id) == DefKind::Trait);
235223

236224
// Find a map of all trait `impl` items within the current crate. The key is the `DefId` of
237225
// the trait, and the value is a `Vec<LocalDefId>` for all `impl` items.
238-
let all_trait_impls = tcx.all_local_trait_impls(());
226+
let all_trait_impls = cx.tcx.all_local_trait_impls(());
239227

240228
// Find all `impl` items for the specific trait.
241229
let trait_impls = trait_def_ids
242-
.filter_map(|def_id| all_trait_impls.get(&def_id))
230+
.filter_map(|def_id| all_trait_impls.get(def_id))
243231
.flatten()
244232
.copied();
245233

246234
// Map the `DefId`s of `impl` items to `TraitType`s. Sometimes this conversion can fail, so
247235
// we use `filter_map()` to skip errors.
248236
trait_impls.filter_map(move |local_def_id| {
249237
// Retrieve the node of the `impl` item from its `DefId`.
250-
let node = tcx.hir_node_by_def_id(local_def_id);
238+
let node = cx.tcx.hir_node_by_def_id(local_def_id);
251239

252240
// Verify that it's an `impl` item and not something else.
253241
let Node::Item(Item {
@@ -284,7 +272,7 @@ impl TraitType {
284272

285273
// Find the span where the type was declared. This is guaranteed to be an item, so we
286274
// can safely call `expect_item()` without it panicking.
287-
let item_span = tcx.hir_node(hir_id).expect_item().span;
275+
let item_span = cx.tcx.hir_node(hir_id).expect_item().span;
288276

289277
Some(TraitType {
290278
hir_id,

bevy_lint/src/lints/restriction/panicking_methods.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use crate::{
5555
use clippy_utils::{
5656
diagnostics::span_lint_and_help,
5757
source::{snippet, snippet_opt},
58-
ty::match_type,
5958
};
6059
use rustc_hir::Expr;
6160
use rustc_lint::{LateContext, LateLintPass};
@@ -215,9 +214,9 @@ enum PanickingType {
215214
impl PanickingType {
216215
/// Returns the corresponding variant for the given [`Ty`], if it is supported by this lint.
217216
fn try_from_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
218-
if match_type(cx, ty, &crate::paths::QUERY) {
217+
if crate::paths::QUERY.matches_ty(cx, ty) {
219218
Some(Self::Query)
220-
} else if match_type(cx, ty, &crate::paths::WORLD) {
219+
} else if crate::paths::WORLD.matches_ty(cx, ty) {
221220
Some(Self::World)
222221
} else {
223222
None

bevy_lint/src/lints/suspicious/insert_event_resource.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@
4040
//! ```
4141
4242
use crate::{
43-
declare_bevy_lint, declare_bevy_lint_pass, sym, utils::hir_parse::{generic_args_snippet, span_args, MethodCall}
43+
declare_bevy_lint, declare_bevy_lint_pass, sym,
44+
utils::hir_parse::{MethodCall, generic_args_snippet, span_args},
4445
};
4546
use clippy_utils::{
4647
diagnostics::span_lint_and_sugg,
4748
source::{snippet, snippet_with_applicability},
48-
ty::{match_type, ty_from_hir_ty},
49+
ty::ty_from_hir_ty,
4950
};
5051
use rustc_errors::Applicability;
5152
use rustc_hir::{Expr, GenericArg, GenericArgs, Path, PathSegment, QPath};
5253
use rustc_lint::{LateContext, LateLintPass};
5354
use rustc_middle::ty::{Ty, TyKind};
54-
use rustc_span::Symbol;
5555
use std::borrow::Cow;
5656

5757
declare_bevy_lint! {
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for InsertEventResource {
8383
.peel_refs();
8484

8585
// If `src` is not a Bevy `App`, exit.
86-
if !match_type(cx, src_ty, &crate::paths::APP) {
86+
if !crate::paths::APP.matches_ty(cx, src_ty) {
8787
return;
8888
}
8989

@@ -113,7 +113,7 @@ fn check_insert_resource(cx: &LateContext<'_>, method_call: &MethodCall) {
113113
let ty = cx.typeck_results().expr_ty(arg);
114114

115115
// If `arg` is `Events<T>`, emit the lint.
116-
if match_type(cx, ty, &crate::paths::EVENTS) {
116+
if crate::paths::EVENTS.matches_ty(cx, ty) {
117117
let mut applicability = Applicability::MachineApplicable;
118118

119119
let event_ty_snippet = extract_ty_event_snippet(ty, &mut applicability);
@@ -191,7 +191,7 @@ fn check_init_resource<'tcx>(cx: &LateContext<'tcx>, method_call: &MethodCall<'t
191191
let resource_ty = ty_from_hir_ty(cx, resource_hir_ty.as_unambig_ty());
192192

193193
// If the resource type is `Events<T>`, emit the lint.
194-
if match_type(cx, resource_ty, &crate::paths::EVENTS) {
194+
if crate::paths::EVENTS.matches_ty(cx, resource_ty) {
195195
let mut applicability = Applicability::MachineApplicable;
196196

197197
let event_ty_snippet =

bevy_lint/src/lints/suspicious/insert_unit_bundle.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@
5050
//! # bevy::ecs::system::assert_is_system(spawn);
5151
//! ```
5252
53-
use clippy_utils::{diagnostics::span_lint_hir_and_then, ty::match_type};
53+
use clippy_utils::diagnostics::span_lint_hir_and_then;
5454
use rustc_errors::Applicability;
5555
use rustc_hir::{Expr, ExprKind};
5656
use rustc_lint::{LateContext, LateLintPass};
5757
use rustc_middle::ty::{Ty, TyKind};
58-
use rustc_span::Symbol;
5958

6059
use crate::{declare_bevy_lint, declare_bevy_lint_pass, sym, utils::hir_parse::MethodCall};
6160

@@ -88,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for InsertUnitBundle {
8887
// If the method call was not to `Commands::spawn()` or originates from an external macro,
8988
// we skip it.
9089
if !(span.in_external_macro(cx.tcx.sess.source_map())
91-
|| match_type(cx, src_ty, &crate::paths::COMMANDS)
90+
|| crate::paths::COMMANDS.matches_ty(cx, src_ty)
9291
&& method_path.ident.name == sym::spawn)
9392
{
9493
return;

bevy_lint/src/lints/suspicious/iter_current_update_events.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
//! }
3939
//! ```
4040
41-
use clippy_utils::{diagnostics::span_lint_and_help, ty::match_type};
41+
use clippy_utils::diagnostics::span_lint_and_help;
4242
use rustc_hir::Expr;
4343
use rustc_lint::{LateContext, LateLintPass};
44-
use rustc_span::Symbol;
4544

4645
use crate::{declare_bevy_lint, declare_bevy_lint_pass, sym, utils::hir_parse::MethodCall};
4746

@@ -83,7 +82,7 @@ impl<'tcx> LateLintPass<'tcx> for IterCurrentUpdateEvents {
8382
.expr_ty_adjusted(method_call.receiver)
8483
.peel_refs();
8584

86-
if !match_type(cx, src_ty, &crate::paths::EVENTS) {
85+
if !crate::paths::EVENTS.matches_ty(cx, src_ty) {
8786
return;
8887
}
8988

0 commit comments

Comments
 (0)