gpt4 book ai didi

rust - 如何从给定 block 的访问者那里获得 lint 级别?

转载 作者:行者123 更新时间:2023-11-29 08:30:08 27 4
gpt4 key购买 nike

出于各种原因,我使用 Visitor 进行 HIR 树遍历,而不是依靠 lint 上下文来遍历树。但是,这意味着我的 Lint 忽略源中的 #[allow/warn/deny(..)] 注释。我怎样才能得到这个回来了吗?

我知道 ctxt.levels,但这些似乎没有帮助。其他功能(比如 with_lint_attrs(..) 是上下文私有(private)的。

最佳答案

由于我们的 Rust 没有解决方案,我 created Rustc 中必要的回调:今晚每晚,我们的 LateLintPass还有一个check_block_post(..)方法。所以我们可以将 Visitor 的东西拉到 lint 中,并添加一个类型为 Option<&Block> 的新字段在 check_block(..) 中设置check_block_post(..) 中的方法和取消设置如果该字段等于当前 block ,则忽略所有包含的 block 。

编辑:代码如下所示:

use syntax::ast::NodeId;

struct RegexLint { // some fields omitted
last: Option<NodeId>
}
// concentrating on the block functions here:
impl LateLintPass for RegexLint {
fn check_block(&mut self, cx: &LateContext, block: &Block) {
if !self.last.is_none() { return; }
// set self.last to Some(block.id) to ignore inner blocks
}

fn check_block_post(&mut self, _: &LateContext, block: &Block) {
if self.last.map_or(false, |id| block.id == id) {
self.last = None; // resume visiting blocks
}
}
}

关于rust - 如何从给定 block 的访问者那里获得 lint 级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com