From 0b28a95bf8e005c8c2457ae9f1797b2be6ae4844 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Sat, 2 Nov 2024 10:16:24 -0700 Subject: [PATCH] Apply suggestions from code review Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com> --- bevy_lint/src/lints/borrow_of_reborrowable.rs | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/bevy_lint/src/lints/borrow_of_reborrowable.rs b/bevy_lint/src/lints/borrow_of_reborrowable.rs index 70526255..47720364 100644 --- a/bevy_lint/src/lints/borrow_of_reborrowable.rs +++ b/bevy_lint/src/lints/borrow_of_reborrowable.rs @@ -76,12 +76,17 @@ impl<'tcx> LateLintPass<'tcx> for BorrowOfReborrowable { fn check_fn( &mut self, cx: &LateContext<'tcx>, - _: FnKind<'tcx>, + kind: FnKind<'tcx>, decl: &'tcx FnDecl<'tcx>, _: &'tcx Body<'tcx>, _: Span, def_id: LocalDefId, ) { + // Closures are not currently supported, as `tcx.fn_sig()` crashes for them. + if let FnKind::Closure = kind { + return; + } + // We are already inside of the function item, // so we can use `instantiate_identity` to discharge the binder let fn_sig = cx.tcx.fn_sig(def_id).instantiate_identity(); @@ -137,7 +142,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowOfReborrowable { } } -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] enum Reborrowable { Commands, // Deferred, @@ -158,19 +163,21 @@ enum Reborrowable { impl Reborrowable { fn try_from_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option { - if match_type(cx, ty, &crate::paths::COMMANDS) { - Some(Self::Commands) - } else if match_type(cx, ty, &crate::paths::ENTITY_COMMANDS) { - Some(Self::EntityCommands) - } else if match_type(cx, ty, &crate::paths::QUERY) { - Some(Self::Query) - } else if match_type(cx, ty, &crate::paths::RES_MUT) { - Some(Self::ResMut) - } else if match_type(cx, ty, &crate::paths::NON_SEND_MUT) { - Some(Self::NonSendMut) - } else { - None + const PATH_MAP: &[(&[&str], Reborrowable)] = &[ + (&crate::paths::COMMANDS, Reborrowable::Commands), + (&crate::paths::ENTITY_COMMANDS, Reborrowable::EntityCommands), + (&crate::paths::QUERY, Reborrowable::Query), + (&crate::paths::RES_MUT, Reborrowable::ResMut), + (&crate::paths::NON_SEND_MUT, Reborrowable::NonSendMut), + ]; + + for &(path, reborrowable) in PATH_MAP { + if match_type(cx, ty, path) { + return Some(reborrowable); + } } + + None } fn lint(&self) -> &'static Lint {