charon_lib/transform/
remove_nops.rs

1//! Remove the useless no-ops.
2use crate::ast::*;
3use crate::transform::TransformCtx;
4
5use super::ctx::TransformPass;
6
7pub struct Transform;
8impl TransformPass for Transform {
9    fn transform_ctx(&self, ctx: &mut TransformCtx) {
10        ctx.for_each_fun_decl(|_ctx, fun| {
11            if let Ok(body) = &mut fun.body {
12                match body {
13                    Body::Unstructured(body) => {
14                        for blk in &mut body.body {
15                            if blk.statements.iter().any(|st| st.content.is_nop()) {
16                                blk.statements.retain(|st| !st.content.is_nop())
17                            }
18                        }
19                    }
20                    Body::Structured(body) => {
21                        body.body.visit_blocks_bwd(|blk: &mut llbc_ast::Block| {
22                            // Remove all the `Nop`s from this sequence.
23                            if blk.statements.iter().any(|st| st.content.is_nop()) {
24                                blk.statements.retain(|st| !st.content.is_nop())
25                            }
26                        });
27                    }
28                }
29            }
30        });
31    }
32}