obelix 0.2.0

Obélix is a tool to reduce Rust source files to produce MVEs
Documentation
use syn::spanned::Spanned;

#[derive(Default)]
pub struct RemoveStatementVisitor {
    done: bool,
    visited: std::collections::BTreeSet<(proc_macro2::LineColumn, proc_macro2::LineColumn)>,
}

impl super::Visiter for RemoveStatementVisitor {
    fn reset(&mut self) {
        self.done = false;
    }

    fn visited_count(&self) -> usize {
        self.visited.len()
    }
}

impl syn::fold::Fold for RemoveStatementVisitor {
    fn fold_block(&mut self, mut block: syn::Block) -> syn::Block {
        if self.done {
            return block;
        }

        let mut i = block.stmts.len();

        if i == 0 {
            return block;
        }

        while i != 0 {
            i = i.saturating_sub(1);
            let stmt = block.stmts[i].span();
            let stmt = (stmt.start(), stmt.end());

            if self.visited.insert(stmt) {
                block.stmts.remove(i).span();
                self.done = true;
                return block;
            }
        }

        syn::fold::fold_block(self, block)
    }
}