Skip to main content

ryo_mutations/basic/stmt/
replace_expr.rs

1//! ReplaceExprMutation: Replace expressions with other expressions
2//!
3//! Converts:
4//! ```ignore
5//! let x = old_expr;
6//! foo(old_expr);
7//! ```
8//! Into:
9//! ```ignore
10//! let x = new_expr;
11//! foo(new_expr);
12//! ```
13
14use ryo_source::pure::PureExpr;
15use ryo_symbol::SymbolId;
16
17use crate::Mutation;
18
19/// Replace an expression with another expression
20#[derive(Debug, Clone)]
21pub struct ReplaceExprMutation {
22    /// The expression to find (matched by structure)
23    pub old_expr: PureExpr,
24    /// The replacement expression
25    pub new_expr: PureExpr,
26    /// Target function SymbolId
27    pub target_fn: SymbolId,
28    /// Replace all occurrences (true) or just first (false)
29    pub replace_all: bool,
30}
31
32impl ReplaceExprMutation {
33    pub fn new(old_expr: PureExpr, new_expr: PureExpr, target_fn: SymbolId) -> Self {
34        Self {
35            old_expr,
36            new_expr,
37            target_fn,
38            replace_all: true,
39        }
40    }
41
42    /// Replace only the first occurrence
43    pub fn first_only(mut self) -> Self {
44        self.replace_all = false;
45        self
46    }
47}
48
49impl Mutation for ReplaceExprMutation {
50    fn describe(&self) -> String {
51        "Replace expression with another expression".to_string()
52    }
53
54    fn mutation_type(&self) -> &'static str {
55        "ReplaceExpr"
56    }
57
58    fn box_clone(&self) -> Box<dyn Mutation> {
59        Box::new(self.clone())
60    }
61}