From 2aa7bda3cb21ce6c052b82e08bcab79a625d04f2 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Sun, 31 May 2026 02:42:52 +0800 Subject: Enhance code quality across the entire codebase --- mingling_macros/src/lib.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'mingling_macros/src/lib.rs') diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 8cae29f..7a93895 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -82,7 +82,7 @@ pub(crate) static CHAINS_EXIST: Registry = OnceLock::new(); pub(crate) static RENDERERS_EXIST: Registry = OnceLock::new(); pub(crate) static HELP_REQUESTS: Registry = OnceLock::new(); -/// Checks that a TypePath is a simple single-segment identifier (no `::` in the path). +/// Checks that a `TypePath` is a simple single-segment identifier (no `::` in the path). /// /// This is used by `#[renderer]`, `#[help]`, `#[chain]`, and `#[completion]` attribute macros /// to ensure that the type in the function signature is a bare identifier like `Empty`, @@ -307,7 +307,7 @@ pub fn empty_result(_input: TokenStream) -> TokenStream { /// /// When the `extra_macros` feature is enabled, the `CommandStruct => EntryStruct` /// portion can be omitted. The struct names are auto-derived from the command path -/// using PascalCase conversion: +/// using `PascalCase` conversion: /// /// ```rust,ignore /// // Auto-derives: "remote.add" → CMDRemoteAdd ⇒ EntryRemoteAdd @@ -1188,7 +1188,7 @@ pub fn program_comp_gen(input: TokenStream) -> TokenStream { /// Registers a type into the global packed types registry for inclusion in /// the program enum generated by `gen_program!`. /// -/// This macro is called internally by `pack!` and `#[derive(Groupped)]`(macro.derive_groupped.html) +/// This macro is called internally by `pack!` and `#[derive(Groupped)]`(`macro.derive_groupped.html`) /// and is generally not needed in user code. However, it can be used for manual /// registration if you are implementing custom type registration outside of /// the standard macros. @@ -1201,6 +1201,10 @@ pub fn program_comp_gen(input: TokenStream) -> TokenStream { /// /// Each call inserts the type's name into the `PACKED_TYPES` global set, which /// is later consumed by `program_final_gen!` to generate enum variants. +/// +/// # Panics +/// +/// Panics if the global `PACKED_TYPES` mutex is poisoned. #[proc_macro] pub fn register_type(input: TokenStream) -> TokenStream { let type_ident = parse_macro_input!(input as syn::Ident); @@ -1348,7 +1352,13 @@ pub fn program_fallback_gen(input: TokenStream) -> TokenStream { /// pub fn new() -> Program { Program::new() } /// } /// ``` +/// +/// # Panics +/// +/// Panics if any of the global registries (`PACKED_TYPES`, `RENDERERS`, `CHAINS`, etc.) +/// are poisoned. #[proc_macro] +#[allow(clippy::too_many_lines)] pub fn program_final_gen(input: TokenStream) -> TokenStream { let name = read_name(&input); @@ -1479,11 +1489,11 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { .collect(); let num_variants = packed_types.len(); - let repr_type = if num_variants <= u8::MAX as usize { + let repr_type = if u8::try_from(num_variants).is_ok() { quote! { u8 } - } else if num_variants <= u16::MAX as usize { + } else if u16::try_from(num_variants).is_ok() { quote! { u16 } - } else if num_variants <= u32::MAX as usize { + } else if u32::try_from(num_variants).is_ok() { quote! { u32 } } else { quote! { u128 } @@ -1612,7 +1622,7 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { /// /// # Related /// -/// - `suggest_enum!`(macro.suggest_enum.html) — Build suggestions from an `EnumTag` enum. +/// - `suggest_enum!`(`macro.suggest_enum.html`) — Build suggestions from an `EnumTag` enum. #[cfg(feature = "comp")] #[proc_macro] pub fn suggest(input: TokenStream) -> TokenStream { -- cgit