ryo-executor 0.1.0

[experimental] Mutation execution engine for RYO - parallel execution, conflict detection, workspace management
Documentation
//! V2 ASTRegApply implementation for RedundantClosureMutation
//!
//! Simplifies redundant closures to function references:
//! - `|x| foo(x)` → `foo`
//! - `|a, b| func(a, b)` → `func`

use ryo_analysis::SymbolKind;
use ryo_mutations::idiom::RedundantClosureMutation;
use ryo_mutations::{Mutation, MutationResult};
use ryo_source::pure::{PureImplItem, PureItem};

use crate::engine::{ASTMutationContext, ASTRegApply};

impl ASTRegApply for RedundantClosureMutation {
    fn apply_to_registry(&self, ctx: &mut ASTMutationContext) -> MutationResult {
        let mut total_changes = 0;

        // Process standalone functions
        let fn_ids: Vec<_> = ctx
            .symbol_registry
            .iter()
            .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Function)))
            .map(|(id, _)| id)
            .collect();

        for id in fn_ids {
            if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(id) {
                if let Some(target) = self.target_fn {
                    if id != target {
                        continue;
                    }
                }
                total_changes += self.transform_block(&mut f.body);
            }
        }

        // Process impl blocks (methods)
        let impl_ids: Vec<_> = ctx
            .symbol_registry
            .iter()
            .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Impl)))
            .map(|(id, _)| id)
            .collect();

        for id in impl_ids {
            if let Some(PureItem::Impl(imp)) = ctx.ast_registry.get_mut(id) {
                for impl_item in &mut imp.items {
                    if let PureImplItem::Fn(f) = impl_item {
                        // Note: target_fn filtering not implemented for impl methods
                        // as we don't have direct SymbolId access for individual methods
                        // TODO: Add method SymbolId resolution for proper targeting
                        if self.target_fn.is_some() {
                            continue; // Skip impl methods when target_fn is specified
                        }
                        total_changes += self.transform_block(&mut f.body);
                    }
                }
            }
        }

        MutationResult {
            mutation_type: self.mutation_type().to_string(),
            changes: total_changes,
            description: if total_changes > 0 {
                format!("Simplified {} redundant closure(s)", total_changes)
            } else {
                "No redundant closures simplified".to_string()
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::ASTMutationEngine;
    use ryo_analysis::testing::ContextBuilder;

    #[test]
    fn test_v2_redundant_closure_single_param() {
        let mut ctx = ContextBuilder::new()
            .with_file(
                "src/lib.rs",
                r#"
fn process(items: Vec<i32>) -> Vec<i32> {
    items.into_iter().map(|x| foo(x)).collect()
}

fn foo(x: i32) -> i32 { x + 1 }
"#,
            )
            .build();

        let mutation = RedundantClosureMutation::new();
        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);

        assert_eq!(result.result.changes, 1);
    }

    #[test]
    fn test_v2_redundant_closure_no_changes() {
        let mut ctx = ContextBuilder::new()
            .with_file(
                "src/lib.rs",
                r#"
fn process(items: Vec<i32>) -> Vec<i32> {
    items.into_iter().map(foo).collect()
}

fn foo(x: i32) -> i32 { x + 1 }
"#,
            )
            .build();

        let mutation = RedundantClosureMutation::new();
        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);

        assert_eq!(result.result.changes, 0);
    }

    #[test]
    fn test_v2_redundant_closure_not_redundant() {
        let mut ctx = ContextBuilder::new()
            .with_file(
                "src/lib.rs",
                r#"
fn process(items: Vec<i32>) -> Vec<i32> {
    items.into_iter().map(|x| x + 1).collect()
}
"#,
            )
            .build();

        let mutation = RedundantClosureMutation::new();
        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);

        // |x| x + 1 is not a redundant closure (not a simple function call)
        assert_eq!(result.result.changes, 0);
    }
}