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

佐手、 提交于 2019-12-12 02:53:54

问题


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:


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


来源:https://stackoverflow.com/questions/35284646/how-do-i-get-the-lint-level-from-a-visitor-given-a-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!