You need to enable JavaScript to run this app. 如何隐藏使用Substrate的decl_event时Clippy报的unneeded unit expression警告
优惠活动
大模型
产品
解决方案
定价
更多

如何隐藏使用Substrate的decl_event时Clippy报的unneeded unit expression警告

问题描述

我在Substrate框架中编写了如下事件定义代码:

decl_event!(
    pub enum Event<T>
    where
        AccountId = <T as frame_system::Config>::AccountId,
        ChipBalance = <T as Config>::ChipBalance,
    {
        /// 购买筹码事件
        BuyChips(AccountId, ChipBalance),
        /// 筹码兑换事件
        Redemption(AccountId, ChipBalance),
        /// 质押筹码
        Reserve(AccountId, ChipBalance),
        /// 取消质押筹码
        Unreserve(AccountId, ChipBalance),
        /// 将质押中的筹码转给他人
        RepatriateReserved(AccountId, AccountId, ChipBalance),
    }
);

运行cargo clippy时收到如下警告:

warning: unneeded unit expression
  --> pallets/gamecenter/src/lib.rs:58:1
   |
58 | / decl_event!(
59 | |     pub enum Event<T>
60 | |     where
61 | |         AccountId = <T as frame_system::Config>::AccountId,
...  |
66 | |     }
67 | | );
   | |__^
   |
   = note: `#[warn(clippy::unused_unit)]` on by default
   = note: 该警告由`$crate::__decl_generic_event`宏展开后生成的代码触发

请问除了全局自定义Clippy lint规则外,有没有更好的方法可以隐藏该警告?


解决方法

有两种更精准的方案,不会影响其他代码的Clippy检查:

  • 局部关闭对应lint,仅作用于当前这段事件定义代码,在宏调用上方添加allow属性即可,原有代码不需要修改:
    #[allow(clippy::unused_unit)]
    decl_event!(
        // 原有事件定义代码保持不变
    );
    
  • 升级到Substrate新版的Pallet编写规范,用#[pallet::event]属性宏替代旧的decl_event!声明宏,新版宏已经修复了展开后生成多余单元表达式的问题,不会再触发该警告,同时写法更简洁易维护。

内容的提问来源于stack exchange,提问作者Tomasz Waszczyk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 11:54:05