Skip to main content

ryo_executor/engine/impls/
match_to_if_let.rs

1//! V2 ASTRegApply implementation for MatchToIfLetMutation
2//!
3//! Converts match expressions to if let:
4//! - `match opt { Some(x) => body, None => {} }` → `if let Some(x) = opt { body }`
5
6use ryo_analysis::SymbolKind;
7use ryo_mutations::idiom::MatchToIfLetMutation;
8use ryo_mutations::{Mutation, MutationResult};
9use ryo_source::pure::{PureImplItem, PureItem};
10
11use crate::engine::{ASTMutationContext, ASTRegApply};
12
13impl ASTRegApply for MatchToIfLetMutation {
14    fn apply_to_registry(&self, ctx: &mut ASTMutationContext) -> MutationResult {
15        let mut total_changes = 0;
16
17        if let Some(target_id) = self.target_fn {
18            // Target function specified: direct lookup
19            if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(target_id) {
20                total_changes += self.transform_block(&mut f.body);
21            }
22        } else {
23            // No target: process all functions
24            let fn_ids: Vec<_> = ctx
25                .symbol_registry
26                .iter()
27                .filter(|(id, _)| {
28                    matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Function))
29                })
30                .map(|(id, _)| id)
31                .collect();
32
33            for id in fn_ids {
34                if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(id) {
35                    total_changes += self.transform_block(&mut f.body);
36                }
37            }
38
39            // Process impl blocks (methods)
40            let impl_ids: Vec<_> = ctx
41                .symbol_registry
42                .iter()
43                .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Impl)))
44                .map(|(id, _)| id)
45                .collect();
46
47            for id in impl_ids {
48                if let Some(PureItem::Impl(imp)) = ctx.ast_registry.get_mut(id) {
49                    for impl_item in &mut imp.items {
50                        if let PureImplItem::Fn(f) = impl_item {
51                            total_changes += self.transform_block(&mut f.body);
52                        }
53                    }
54                }
55            }
56        }
57
58        MutationResult {
59            mutation_type: self.mutation_type().to_string(),
60            changes: total_changes,
61            description: if total_changes > 0 {
62                format!("Converted {} match(es) to if let", total_changes)
63            } else {
64                "No matches converted to if let".to_string()
65            },
66        }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::engine::ASTMutationEngine;
74    use ryo_analysis::testing::ContextBuilder;
75
76    #[test]
77    fn test_v2_match_to_if_let_no_changes() {
78        // Already using if let
79        let mut ctx = ContextBuilder::new()
80            .with_file(
81                "src/lib.rs",
82                r#"
83fn process(opt: Option<i32>) {
84    if let Some(x) = opt {
85        println!("{}", x);
86    }
87}
88"#,
89            )
90            .build();
91
92        let mutation = MatchToIfLetMutation::new();
93        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
94
95        assert_eq!(result.result.changes, 0);
96    }
97}