Repository: swc-project/swc
File: crates/swc_ecma_minifier/src/compress/optimize/inline.rs (Rust)

Code before:
            self.vars.inline_with_multi_replacer(decl);
            match decl {
                Decl::Fn(f) if self.options.inline >= 2 && f.ident.sym != *"arguments" => {
                    if let Some(body) = &f.function.body {
                        if !usage.flags.contains(VarUsageInfoFlags::USED_RECURSIVELY)
                            // only callees can be inlined multiple times
                            && usage.callee_count > 0
                            // prefer single inline
                            && usage.ref_count > 1
                            && self.is_fn_body_simple_enough_to_inline(
                                body,
                                f.function.params.len(),
                                usage,
                            )
                        {
                            if f.function
                                .params
                                .iter()
                                .any(|param| matches!(param.pat, Pat::Rest(..) | Pat::Assign(..)))
                            {
                                return;
                            }
                            report_change!(
                                "inline: Decided to inline function `{}{:?}` as it's very simple",
                                id.0,
                                id.1
                            );

                            for i in collect_infects_from(
                                &f.function,
                                AliasConfig::default()
                                    .marks(Some(self.marks))
                                    .need_all(true),
                            ) {
                                if let Some(usage) = self.data.vars.get_mut(&i.0) {
                                    usage.ref_count += 1;
                                }
                            }

                            self.vars.simple_functions.insert(
                                id,
                                FnExpr {
                                    ident: None,
                                    function: f.function.clone(),
                                }
                                .into(),
                            );

                            return;
                        }
                    }

Review comment (suggestion):
The variable name `param` shadows the outer loop variable `param` from line 750. Consider renaming the inner binding to `param_ident` or `param_id` to avoid confusion and improve code clarity.
```suggestion
                                        let Pat::Ident(param_ident) = &*assign.left else {
                                            return;
                                        };
                                        let Some(usage) = self.data.vars.get(&param_ident.id.to_id())
```

Code after:
            // Inline very simple functions.
            self.vars.inline_with_multi_replacer(decl);
            match decl {
                Decl::Fn(f) if self.options.inline >= 2 && f.ident.sym != *"arguments" => {
                    if let Some(body) = &f.function.body {
                        if !usage.flags.contains(VarUsageInfoFlags::USED_RECURSIVELY)
                            // only callees can be inlined multiple times
                            && usage.callee_count > 0
                            // prefer single inline
                            && usage.ref_count > 1
                            && self.is_fn_body_simple_enough_to_inline(
                                body,
                                f.function.params.len(),
                                usage,
                            )
                        {
                            for (idx, param) in f.function.params.iter().enumerate() {
                                match &param.pat {
                                    Pat::Rest(..) => return,
                                    Pat::Assign(assign) => {
                                        // Multi-use function inlining with default params is only
                                        // safe for defaults that are never observed directly.
                                        let Pat::Ident(param) = &*assign.left else {
                                            return;
                                        };
                                        let Some(usage) = self.data.vars.get(&param.id.to_id())
                                        else {
                                            return;
                                        };
                                        if usage.flags.contains(VarUsageInfoFlags::REASSIGNED)
                                            || usage.ref_count != 0
                                        {
                                            return;
                                        }

                                        if assign.right.may_have_side_effects(self.ctx.expr_ctx) {
                                            return;
                                        }

                                        for earlier_param in f.function.params.iter().take(idx) {
                                            let earlier_ident = match &earlier_param.pat {
                                                Pat::Ident(id) => Some(&id.id),
                                                Pat::Assign(assign) => match &*assign.left {
                                                    Pat::Ident(id) => Some(&id.id),
                                                    _ => None,
                                                },
                                                _ => None,
                                            };

                                            let Some(earlier_ident) = earlier_ident else {
                                                return;