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
//! V2 ASTRegApply implementation for UnwrapToQuestionMutation
//!
//! Converts .unwrap()/.expect() to ? operator:
//! - `x.unwrap()` → `x?`
//! - `x.expect("msg")` → `x?`
//!
//! Only applies in functions that return Result/Option.
use ryo_analysis::SymbolKind;
use ryo_mutations::idiom::UnwrapToQuestionMutation;
use ryo_mutations::{Mutation, MutationResult};
use ryo_source::pure::{PureImplItem, PureItem, PureType};
use crate::engine::{ASTMutationContext, ASTRegApply, ModificationType};
/// Check if a return type is Option or Result (compatible with ? operator)
fn returns_option_or_result(ret: &Option<PureType>) -> bool {
match ret {
Some(PureType::Path(path)) => {
path.starts_with("Option<")
|| path.starts_with("Result<")
|| path == "Option"
|| path == "Result"
}
_ => false,
}
}
impl ASTRegApply for UnwrapToQuestionMutation {
fn apply_to_registry(&self, ctx: &mut ASTMutationContext) -> MutationResult {
let mut total_changes = 0;
if let Some(target_id) = self.target_fn {
// Target function specified: direct lookup, transform only that fn.
//
// Note: this matches the convention of every other idiom mutation
// (NoOpArm / CollapsibleIf / BoolSimplify / FilterNext /
// MapUnwrapOr / AssignOp / MatchToIfLet etc.): standalone Function
// only. Methods inside an Impl block are not handled in scoped
// mode — when the lint targets a method the spec falls through
// to "no change". This is a known pre-existing limitation
// shared across all mutations, not introduced here. Tracked as
// a separate ergonomic improvement.
if let Some(PureItem::Fn(f)) = ctx.ast_registry.get_mut(target_id) {
if returns_option_or_result(&f.ret) {
let changes = self.transform_block(&mut f.body);
if changes > 0 {
ctx.emit_modified(target_id, ModificationType::BodyModified);
total_changes += changes;
}
}
}
} else {
// No target: process all 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) {
// Only transform if function returns Option or Result
if returns_option_or_result(&f.ret) {
let changes = self.transform_block(&mut f.body);
if changes > 0 {
ctx.emit_modified(id, ModificationType::BodyModified);
total_changes += changes;
}
}
}
}
// 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 {
let mut impl_changes = 0;
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 {
// Only transform if method returns Option or Result
if returns_option_or_result(&f.ret) {
impl_changes += self.transform_block(&mut f.body);
}
}
}
}
if impl_changes > 0 {
ctx.emit_modified(id, ModificationType::BodyModified);
total_changes += impl_changes;
}
}
}
MutationResult {
mutation_type: self.mutation_type().to_string(),
changes: total_changes,
description: if total_changes > 0 {
format!("Converted {} .unwrap()/.expect() to ?", total_changes)
} else {
"No unwrap calls converted".to_string()
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::ASTMutationEngine;
use ryo_analysis::testing::ContextBuilder;
#[test]
fn test_v2_unwrap_to_question_basic() {
let mut ctx = ContextBuilder::new()
.with_file(
"src/lib.rs",
r#"
fn process(opt: Option<i32>) -> Option<i32> {
let x = opt.unwrap();
Some(x + 1)
}
"#,
)
.build();
let mutation = UnwrapToQuestionMutation::new();
let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
assert_eq!(result.result.changes, 1);
}
#[test]
fn test_v2_unwrap_to_question_non_option_return() {
let mut ctx = ContextBuilder::new()
.with_file(
"src/lib.rs",
r#"
fn process(opt: Option<i32>) -> i32 {
opt.unwrap()
}
"#,
)
.build();
let mutation = UnwrapToQuestionMutation::new();
let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
// Should not convert because function doesn't return Option/Result
assert_eq!(result.result.changes, 0);
}
#[test]
fn test_v2_unwrap_to_question_expect() {
let mut ctx = ContextBuilder::new()
.with_file(
"src/lib.rs",
r#"
fn process(opt: Option<i32>) -> Option<i32> {
let x = opt.expect("should not be none");
Some(x + 1)
}
"#,
)
.build();
let mutation = UnwrapToQuestionMutation::new();
let result = ASTMutationEngine::execute_ast_reg(&mutation, &mut ctx);
assert_eq!(result.result.changes, 1);
}
}