1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! ComparisonToMethodMutation: Convert comparisons to idiomatic method calls
//!
//! Transforms:
//! - `s == ""` → `s.is_empty()`
//! - `s != ""` → `!s.is_empty()`
//! - `v.len() == 0` → `v.is_empty()`
//! - `v.len() != 0` → `!v.is_empty()`
//! - `v.len() > 0` → `!v.is_empty()`
//! - `ptr == std::ptr::null()` → `ptr.is_null()` (planned)
//!
//! Corresponds to Clippy lints: `clippy::comparison_to_empty`, `clippy::len_zero`
use ryo_source::pure::{PureBlock, PureExpr, PureStmt};
use ryo_symbol::SymbolId;
use crate::Mutation;
/// Convert comparisons to idiomatic method calls
///
/// # Example
///
/// ```rust,ignore
/// use ryo_mutations::idiom::ComparisonToMethodMutation;
///
/// let mutation = ComparisonToMethodMutation::new();
/// // Transforms: if s == "" { ... }
/// // Into: if s.is_empty() { ... }
/// ```
#[derive(Debug, Clone, Default)]
pub struct ComparisonToMethodMutation {
/// Target function SymbolId. If None, applies to all functions.
pub target_fn: Option<SymbolId>,
}
impl ComparisonToMethodMutation {
pub fn new() -> Self {
Self::default()
}
/// Only apply in a specific function
pub fn in_function(mut self, id: SymbolId) -> Self {
self.target_fn = Some(id);
self
}
/// Check if expression is an empty string literal
fn is_empty_string(expr: &PureExpr) -> bool {
match expr {
PureExpr::Lit(lit) => lit == "\"\"",
_ => false,
}
}
/// Check if expression is a zero literal
fn is_zero(expr: &PureExpr) -> bool {
match expr {
PureExpr::Lit(lit) => lit == "0" || lit == "0usize" || lit == "0_usize",
_ => false,
}
}
/// Check if expression is a .len() call
fn is_len_call(expr: &PureExpr) -> Option<&PureExpr> {
match expr {
PureExpr::MethodCall {
receiver,
method,
args,
..
} if method == "len" && args.is_empty() => Some(receiver.as_ref()),
_ => None,
}
}
/// Transform an expression, returns changes count
fn transform_expr(&self, expr: &mut PureExpr) -> usize {
let mut changes = 0;
// Pattern: x == "" or "" == x
if let PureExpr::Binary { op, left, right } = expr {
let is_eq = op == "==";
let is_neq = op == "!=";
let is_gt = op == ">";
let is_lt = op == "<";
if is_eq || is_neq {
// Check for empty string comparison
let (target, is_empty_check) = if Self::is_empty_string(left) {
(right.as_ref(), true)
} else if Self::is_empty_string(right) {
(left.as_ref(), true)
} else {
(left.as_ref(), false)
};
if is_empty_check {
let target = target.clone();
let is_empty_call = PureExpr::MethodCall {
receiver: Box::new(target),
method: "is_empty".to_string(),
turbofish: None,
args: vec![],
};
*expr = if is_eq {
is_empty_call
} else {
PureExpr::Unary {
op: "!".to_string(),
expr: Box::new(is_empty_call),
}
};
return 1;
}
// Check for len() == 0 or len() != 0
if let Some(receiver) = Self::is_len_call(left) {
if Self::is_zero(right) {
let is_empty_call = PureExpr::MethodCall {
receiver: Box::new(receiver.clone()),
method: "is_empty".to_string(),
turbofish: None,
args: vec![],
};
*expr = if is_eq {
is_empty_call
} else {
PureExpr::Unary {
op: "!".to_string(),
expr: Box::new(is_empty_call),
}
};
return 1;
}
}
// Check for 0 == len() or 0 != len()
if let Some(receiver) = Self::is_len_call(right) {
if Self::is_zero(left) {
let is_empty_call = PureExpr::MethodCall {
receiver: Box::new(receiver.clone()),
method: "is_empty".to_string(),
turbofish: None,
args: vec![],
};
*expr = if is_eq {
is_empty_call
} else {
PureExpr::Unary {
op: "!".to_string(),
expr: Box::new(is_empty_call),
}
};
return 1;
}
}
}
// Check for len() > 0 (not empty)
if is_gt {
if let Some(receiver) = Self::is_len_call(left) {
if Self::is_zero(right) {
let is_empty_call = PureExpr::MethodCall {
receiver: Box::new(receiver.clone()),
method: "is_empty".to_string(),
turbofish: None,
args: vec![],
};
*expr = PureExpr::Unary {
op: "!".to_string(),
expr: Box::new(is_empty_call),
};
return 1;
}
}
}
// Check for 0 < len() (not empty)
if is_lt {
if let Some(receiver) = Self::is_len_call(right) {
if Self::is_zero(left) {
let is_empty_call = PureExpr::MethodCall {
receiver: Box::new(receiver.clone()),
method: "is_empty".to_string(),
turbofish: None,
args: vec![],
};
*expr = PureExpr::Unary {
op: "!".to_string(),
expr: Box::new(is_empty_call),
};
return 1;
}
}
}
}
// Recursively transform sub-expressions
match expr {
PureExpr::Binary { left, right, .. } => {
changes += self.transform_expr(left);
changes += self.transform_expr(right);
}
PureExpr::Unary { expr: inner, .. } => {
changes += self.transform_expr(inner);
}
PureExpr::Call { func, args } => {
changes += self.transform_expr(func);
for arg in args {
changes += self.transform_expr(arg);
}
}
PureExpr::MethodCall { receiver, args, .. } => {
changes += self.transform_expr(receiver);
for arg in args {
changes += self.transform_expr(arg);
}
}
PureExpr::Block { block, .. } => {
changes += self.transform_block(block);
}
PureExpr::If {
cond,
then_branch,
else_branch,
} => {
changes += self.transform_expr(cond);
changes += self.transform_block(then_branch);
if let Some(else_expr) = else_branch {
changes += self.transform_expr(else_expr);
}
}
PureExpr::Match { expr: e, arms } => {
changes += self.transform_expr(e);
for arm in arms {
changes += self.transform_expr(&mut arm.body);
}
}
PureExpr::Loop { body: block, .. } | PureExpr::While { body: block, .. } => {
changes += self.transform_block(block);
}
PureExpr::For {
expr: iter_expr,
body,
..
} => {
changes += self.transform_expr(iter_expr);
changes += self.transform_block(body);
}
PureExpr::Closure { body, .. } => {
changes += self.transform_expr(body);
}
_ => {}
}
changes
}
pub fn transform_block(&self, block: &mut PureBlock) -> usize {
let mut changes = 0;
for stmt in &mut block.stmts {
changes += self.transform_stmt(stmt);
}
changes
}
fn transform_stmt(&self, stmt: &mut PureStmt) -> usize {
match stmt {
PureStmt::Local { init: Some(e), .. } => self.transform_expr(e),
PureStmt::Semi(e) | PureStmt::Expr(e) => self.transform_expr(e),
_ => 0,
}
}
}
impl Mutation for ComparisonToMethodMutation {
fn describe(&self) -> String {
"Convert comparisons to method calls (s == \"\" → s.is_empty())".to_string()
}
fn mutation_type(&self) -> &'static str {
"ComparisonToMethod"
}
fn box_clone(&self) -> Box<dyn Mutation> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_empty_string() {
assert!(ComparisonToMethodMutation::is_empty_string(&PureExpr::Lit(
"\"\"".to_string()
)));
assert!(!ComparisonToMethodMutation::is_empty_string(
&PureExpr::Lit("\"hello\"".to_string())
));
}
#[test]
fn test_is_zero() {
assert!(ComparisonToMethodMutation::is_zero(&PureExpr::Lit(
"0".to_string()
)));
assert!(ComparisonToMethodMutation::is_zero(&PureExpr::Lit(
"0usize".to_string()
)));
assert!(!ComparisonToMethodMutation::is_zero(&PureExpr::Lit(
"1".to_string()
)));
}
#[test]
fn test_is_len_call() {
let len_call = PureExpr::MethodCall {
receiver: Box::new(PureExpr::Path("v".to_string())),
method: "len".to_string(),
turbofish: None,
args: vec![],
};
assert!(ComparisonToMethodMutation::is_len_call(&len_call).is_some());
let not_len = PureExpr::MethodCall {
receiver: Box::new(PureExpr::Path("v".to_string())),
method: "size".to_string(),
turbofish: None,
args: vec![],
};
assert!(ComparisonToMethodMutation::is_len_call(¬_len).is_none());
}
}