Skip to main content

charon_lib/transform/simplify_output/
remove_nops.rs

1//! Remove the useless no-ops.
2use crate::ast::*;
3use crate::transform::TransformCtx;
4
5use crate::transform::ctx::{LlbcPass, TransformPass, UllbcPass};
6
7pub struct Transform;
8impl UllbcPass for Transform {
9    fn transform_body(&self, _ctx: &mut TransformCtx, body: &mut ullbc_ast::ExprBody) {
10        for blk in &mut body.body {
11            blk.statements
12                .retain(|st| !(st.kind.is_nop() && st.comments_before.is_empty()))
13        }
14    }
15}
16impl LlbcPass for Transform {
17    fn transform_body(&self, _ctx: &mut TransformCtx, body: &mut llbc_ast::ExprBody) {
18        body.body.visit_blocks_bwd(|blk: &mut llbc_ast::Block| {
19            blk.statements
20                .retain(|st| !(st.kind.is_nop() && st.comments_before.is_empty()))
21        });
22    }
23}
24
25impl TransformPass for Transform {
26    fn transform_ctx(&self, ctx: &mut TransformCtx) {
27        ctx.for_each_fun_decl(|ctx, fun| match &mut fun.body {
28            Body::Unstructured(body) => {
29                <Self as UllbcPass>::transform_body(self, ctx, body);
30            }
31            Body::Structured(body) => {
32                <Self as LlbcPass>::transform_body(self, ctx, body);
33            }
34            _ => {}
35        });
36    }
37}