【问题标题】:How do I get the lint level from a Visitor given a Block?如何从给定块的访客那里获得棉绒级别?
【发布时间】:2016-02-09 05:36:27
【问题描述】:
出于各种原因,我使用访问者来遍历 HIR 树而不是
依靠 lint 上下文来遍历树。然而,这意味着我的皮棉
忽略源代码中的 #[allow/warn/deny(..)] 注释。我怎样才能得到这个
回来了吗?
我知道ctxt.levels,但这些似乎没有帮助。其他功能
(比如 with_lint_attrs(..) 是上下文私有的。
【问题讨论】:
标签:
rust
lint
internals
visitor-pattern
【解决方案1】:
由于我们的 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
}
}
}