Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I continue navigating into expressions inside a statement? #65

Closed
shepmaster opened this issue Nov 10, 2016 · 3 comments
Closed

Comments

@shepmaster
Copy link

Here's a piece of code rudely extracted from my playing-around:

syn::ItemKind::Fn(_, _, _, _, _, block) => {
    for stmt in block.stmts {
        match stmt {
            syn::Stmt::Expr(i) => {
                // i.node; // `node` is private
                println!("e {:?}", i); // but I can see the interesting block under here
            }
            _ => unimplemented!(),
        }
    }
    // Descend
}

I am trying to parse this code:

pub fn unsafe_block_inside() {
    unsafe {}
}

My goal is to be able to answer a "simple" true/false question: does a crate use unsafe code? If you have pointers for a better way I should be doing that, I'd much appreciate it!

Thanks for making such a useful library! ❤️

@dtolnay
Copy link
Owner

dtolnay commented Nov 10, 2016

Those definitely should have been public. I fixed it in 12dcd62 and released it as 0.10.2. You're on the right track!

I also went through and checked that every other AST struct has public fields.

@dtolnay
Copy link
Owner

dtolnay commented Nov 10, 2016

One thing that would be helpful here is #44, some syn equivalent to libsyntax's visit module. Then implementing something like "does a crate use unsafe code" is a matter of writing a single Visitor with just a few methods implemented (visit_expr to check for an unsafe block, visit_item to check for an unsafe fn, maybe a couple more). Sounds like you'll be doing most of that work anyway to recursively walk through the entire tree of expressions. I would be delighted if you have the extra time to factor that into a syn visitor crate, even if it isn't fully complete and I can follow up from there to add the rest of the syntax when I have time.

@shepmaster
Copy link
Author

shepmaster commented Nov 11, 2016

Thanks for the quick fix! I'll skim over visit and see how close I am. One thing is that I don't care at all about visitation order, so I can be very slapdash about which nodes I visit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants