Skip to repository content99 lines · 3.6 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T02:24:51.311Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
render_helpers.rs
1use rustc_hir::def_id::DefId;
2use rustc_hir::{ExprKind, HirId, Node};
3use rustc_lint::LateContext;
4use rustc_middle::ty::Ty;
5
6/// Returns `true` when `hir_id` sits directly inside a `fn render` that
7/// implements `gpui::Render` or `gpui::RenderOnce`, without an intervening
8/// closure. If a closure sits between `hir_id` and the `render` method, the
9/// expression executes later (e.g. in an event handler) and is not flagged.
10pub(crate) fn is_directly_in_render_method(cx: &LateContext<'_>, hir_id: HirId) -> bool {
11 for (parent_id, node) in cx.tcx.hir_parent_iter(hir_id) {
12 match node {
13 Node::Expr(expr) if matches!(expr.kind, ExprKind::Closure(_)) => {
14 return false;
15 }
16 Node::ImplItem(impl_item) if impl_item.ident.name.as_str() == "render" => {
17 return is_render_trait_impl(cx, parent_id);
18 }
19 _ => {}
20 }
21 }
22 false
23}
24
25/// Returns `true` when the `impl` block that owns `impl_item_hir_id` is an
26/// implementation of `gpui::Render` or `gpui::RenderOnce`.
27fn is_render_trait_impl(cx: &LateContext<'_>, impl_item_hir_id: HirId) -> bool {
28 let parent_owner = cx.tcx.hir_get_parent_item(impl_item_hir_id);
29 let node = cx.tcx.hir_node(parent_owner.into());
30 if let Node::Item(item) = node {
31 if let rustc_hir::ItemKind::Impl(impl_block) = &item.kind {
32 if let Some(trait_ref) = &impl_block.of_trait {
33 if let rustc_hir::def::Res::Def(_, trait_def_id) = trait_ref.trait_ref.path.res {
34 return is_gpui_render_trait(cx, trait_def_id);
35 }
36 }
37 }
38 }
39 false
40}
41
42fn is_gpui_render_trait(cx: &LateContext<'_>, trait_def_id: DefId) -> bool {
43 let crate_name = cx.tcx.crate_name(trait_def_id.krate);
44 if crate_name.as_str() != "gpui" {
45 return false;
46 }
47 let name = cx.tcx.item_name(trait_def_id);
48 name.as_str() == "Render" || name.as_str() == "RenderOnce"
49}
50
51/// Returns `true` when `ty` is `gpui::Entity<T>` or `gpui::WeakEntity<T>`.
52pub(crate) fn is_gpui_entity_or_weak(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
53 let peeled = ty.peel_refs();
54 let Some(adt) = peeled.ty_adt_def() else {
55 return false;
56 };
57 let did = adt.did();
58 let crate_name = cx.tcx.crate_name(did.krate);
59 if crate_name.as_str() != "gpui" {
60 return false;
61 }
62 let name = cx.tcx.item_name(did);
63 name.as_str() == "Entity" || name.as_str() == "WeakEntity"
64}
65
66/// Returns `true` when `ty` is `gpui::Context<T>`.
67pub(crate) fn is_gpui_context(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
68 let peeled = ty.peel_refs();
69 let Some(adt) = peeled.ty_adt_def() else {
70 return false;
71 };
72 let did = adt.did();
73 let crate_name = cx.tcx.crate_name(did.krate);
74 if crate_name.as_str() != "gpui" {
75 return false;
76 }
77 cx.tcx.item_name(did).as_str() == "Context"
78}
79
80/// Returns `true` when the expression type indicates a unit-returning update
81/// call — either `()` (from `Entity::update`) or `Result<(), _>` (from
82/// `WeakEntity::update`).
83pub(crate) fn is_unit_or_result_unit(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
84 if ty.is_unit() {
85 return true;
86 }
87 if let Some(adt) = ty.ty_adt_def() {
88 let path = cx.tcx.def_path_str(adt.did());
89 if path == "core::result::Result" || path == "std::result::Result" {
90 if let Some(substs) = ty.walk().nth(1) {
91 if let Some(inner_ty) = substs.as_type() {
92 return inner_ty.is_unit();
93 }
94 }
95 }
96 }
97 false
98}
99