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

How do I get the lint level from a Visitor given a Block?

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

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

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

编辑:代码如下所示:

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
        }
    }
}