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

2019-09-05 02:09发布

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?

I know of ctxt.levels, but those don't appear to help. The other functions (like with_lint_attrs(..) are private to the context.

1条回答
混吃等死
2楼-- · 2019-09-05 02:30

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.

Edit: The code looks as follows:

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
        }
    }
}
查看更多
登录 后发表回答