如何从给定访问者的访客那里获得皮棉等级? [英] How do I get the lint level from a Visitor given a Block?
问题描述
出于各种原因,我将访问者用于HIR树遍历,而不是
依靠皮棉环境行走在树上.但是,这意味着我的皮棉
忽略源中的#[allow/warn/deny(..)]
批注.我怎么能得到这个
回来吗?
For various reasons I use a Visitor for the HIR tree traversal instead of
relying on the lint context to walk the tree. However, this means my lint
ignores #[allow/warn/deny(..)]
annotations in the source. How can I get this
back?
我知道ctxt.levels
,但是这些似乎没有帮助.其他功能
(例如with_lint_attrs(..)
是上下文专有的.
I know of ctxt.levels
, but those don't appear to help. The other functions
(like with_lint_attrs(..)
are private to the context.
推荐答案
Since there was no solution with the Rust we had, I created the necessary callbacks in Rustc: With tonight's nightly, our LateLintPass
has another check_block_post(..)
method. So we can pull the Visitor stuff into the lint, and add a new field of type Option<&Block>
that is set in the check_block(..)
method and unset in the check_block_post(..)
if the field is equal the current block, thus ignoring all contained blocks.
代码如下:
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
}
}
}
这篇关于如何从给定访问者的访客那里获得皮棉等级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!