Skip to main content

ryo_executor/engine/impls/
comparison_to_method.rs

1//! V2 ASTRegApply implementation for ComparisonToMethodMutation
2//!
3//! Converts comparisons to idiomatic method calls:
4//! - `s == ""` → `s.is_empty()`
5//! - `s != ""` → `!s.is_empty()`
6//! - `v.len() == 0` → `v.is_empty()`
7//! - `v.len() != 0` → `!v.is_empty()`
8
9use ryo_analysis::SymbolKind;
10use ryo_mutations::idiom::ComparisonToMethodMutation;
11use ryo_mutations::{Mutation, MutationResult};
12use ryo_source::pure::{PureImplItem, PureItem};
13
14use crate::engine::{ASTMutationContext, ASTRegApply};
15
16impl ASTRegApply for ComparisonToMethodMutation {
17    fn apply_to_registry(&self, ctx: &mut ASTMutationContext) -> MutationResult {
18        let mut total_changes = 0;
19
20        if let Some(target_id) = self.target_fn {
21            // Target function specified: direct lookup
22            if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(target_id) {
23                total_changes += self.transform_block(&mut f.body);
24            }
25        } else {
26            // No target: process all functions
27            let fn_ids: Vec<_> = ctx
28                .symbol_registry
29                .iter()
30                .filter(|(id, _)| {
31                    matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Function))
32                })
33                .map(|(id, _)| id)
34                .collect();
35
36            for id in fn_ids {
37                if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(id) {
38                    total_changes += self.transform_block(&mut f.body);
39                }
40            }
41
42            // Process impl blocks (methods)
43            let impl_ids: Vec<_> = ctx
44                .symbol_registry
45                .iter()
46                .filter(|(id, _)| matches!(ctx.symbol_registry.kind(*id), Some(SymbolKind::Impl)))
47                .map(|(id, _)| id)
48                .collect();
49
50            for id in impl_ids {
51                if let Some(PureItem::Impl(imp)) = ctx.ast_registry.get_mut(id) {
52                    for impl_item in &mut imp.items {
53                        if let PureImplItem::Fn(f) = impl_item {
54                            total_changes += self.transform_block(&mut f.body);
55                        }
56                    }
57                }
58            }
59        }
60
61        MutationResult {
62            mutation_type: self.mutation_type().to_string(),
63            changes: total_changes,
64            description: if total_changes > 0 {
65                format!("Converted {} comparison(s) to method calls", total_changes)
66            } else {
67                "No comparisons converted".to_string()
68            },
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use crate::engine::ASTMutationEngine;
77    use ryo_analysis::testing::ContextBuilder;
78
79    #[test]
80    fn test_v2_comparison_empty_string() {
81        let mut ctx = ContextBuilder::new()
82            .with_file(
83                "src/lib.rs",
84                r#"
85fn check(s: &str) -> bool {
86    s == ""
87}
88"#,
89            )
90            .build();
91
92        let mutation = ComparisonToMethodMutation::new();
93        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
94
95        assert_eq!(result.result.changes, 1);
96    }
97
98    #[test]
99    fn test_v2_comparison_len_zero() {
100        let mut ctx = ContextBuilder::new()
101            .with_file(
102                "src/lib.rs",
103                r#"
104fn check(v: &Vec<i32>) -> bool {
105    v.len() == 0
106}
107"#,
108            )
109            .build();
110
111        let mutation = ComparisonToMethodMutation::new();
112        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
113
114        assert_eq!(result.result.changes, 1);
115    }
116
117    #[test]
118    fn test_v2_comparison_no_changes() {
119        let mut ctx = ContextBuilder::new()
120            .with_file(
121                "src/lib.rs",
122                r#"
123fn check(s: &str) -> bool {
124    s.is_empty()
125}
126"#,
127            )
128            .build();
129
130        let mutation = ComparisonToMethodMutation::new();
131        let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
132
133        assert_eq!(result.result.changes, 0);
134    }
135}