Skip to content

Commit 862693f

Browse files
committed
Auto merge of rust-lang#15752 - Young-Flash:replace_is_method_with_if_let_method, r=HKalbasi
feat: add replace_is_ok_with_if_let_ok assist following rust-lang/rust-analyzer#15743
2 parents 84e1d27 + 5bbca22 commit 862693f

File tree

3 files changed

+126
-92
lines changed

3 files changed

+126
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use syntax::ast::{self, AstNode};
2+
3+
use crate::{AssistContext, AssistId, AssistKind, Assists};
4+
5+
// Assist: replace_is_some_with_if_let_some
6+
//
7+
// Replace `if x.is_some()` with `if let Some(_tmp) = x` or `if x.is_ok()` with `if let Ok(_tmp) = x`.
8+
//
9+
// ```
10+
// fn main() {
11+
// let x = Some(1);
12+
// if x.is_som$0e() {}
13+
// }
14+
// ```
15+
// ->
16+
// ```
17+
// fn main() {
18+
// let x = Some(1);
19+
// if let Some(_tmp) = x {}
20+
// }
21+
// ```
22+
pub(crate) fn replace_is_method_with_if_let_method(
23+
acc: &mut Assists,
24+
ctx: &AssistContext<'_>,
25+
) -> Option<()> {
26+
let if_expr = ctx.find_node_at_offset::<ast::IfExpr>()?;
27+
28+
let cond = if_expr.condition()?;
29+
let call_expr = match cond {
30+
ast::Expr::MethodCallExpr(call) => call,
31+
_ => return None,
32+
};
33+
34+
let name_ref = call_expr.name_ref()?;
35+
match name_ref.text().as_str() {
36+
"is_some" | "is_ok" => {
37+
let receiver = call_expr.receiver()?;
38+
let target = call_expr.syntax().text_range();
39+
40+
let (assist_id, message, text) = if name_ref.text() == "is_some" {
41+
("replace_is_some_with_if_let_some", "Replace `is_some` with `if let Some`", "Some")
42+
} else {
43+
("replace_is_ok_with_if_let_ok", "Replace `is_ok` with `if let Ok`", "Ok")
44+
};
45+
46+
acc.add(AssistId(assist_id, AssistKind::RefactorRewrite), message, target, |edit| {
47+
let replacement = format!("let {}(_tmp) = {}", text, receiver);
48+
edit.replace(target, replacement);
49+
})
50+
}
51+
_ => return None,
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use crate::tests::{check_assist, check_assist_not_applicable};
58+
59+
use super::replace_is_method_with_if_let_method;
60+
61+
#[test]
62+
fn replace_is_some_with_if_let_some_works() {
63+
check_assist(
64+
replace_is_method_with_if_let_method,
65+
r#"
66+
fn main() {
67+
let x = Some(1);
68+
if x.is_som$0e() {}
69+
}
70+
"#,
71+
r#"
72+
fn main() {
73+
let x = Some(1);
74+
if let Some(_tmp) = x {}
75+
}
76+
"#,
77+
);
78+
}
79+
80+
#[test]
81+
fn replace_is_some_with_if_let_some_not_applicable() {
82+
check_assist_not_applicable(
83+
replace_is_method_with_if_let_method,
84+
r#"
85+
fn main() {
86+
let x = Some(1);
87+
if x.is_non$0e() {}
88+
}
89+
"#,
90+
);
91+
}
92+
93+
#[test]
94+
fn replace_is_ok_with_if_let_ok_works() {
95+
check_assist(
96+
replace_is_method_with_if_let_method,
97+
r#"
98+
fn main() {
99+
let x = Ok(1);
100+
if x.is_o$0k() {}
101+
}
102+
"#,
103+
r#"
104+
fn main() {
105+
let x = Ok(1);
106+
if let Ok(_tmp) = x {}
107+
}
108+
"#,
109+
);
110+
}
111+
112+
#[test]
113+
fn replace_is_ok_with_if_let_ok_not_applicable() {
114+
check_assist_not_applicable(
115+
replace_is_method_with_if_let_method,
116+
r#"
117+
fn main() {
118+
let x = Ok(1);
119+
if x.is_e$0rr() {}
120+
}
121+
"#,
122+
);
123+
}
124+
}

Diff for: crates/ide-assists/src/handlers/replace_is_some_with_if_let_some.rs

-90
This file was deleted.

Diff for: crates/ide-assists/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ mod handlers {
195195
mod replace_try_expr_with_match;
196196
mod replace_derive_with_manual_impl;
197197
mod replace_if_let_with_match;
198+
mod replace_is_method_with_if_let_method;
198199
mod replace_method_eager_lazy;
199200
mod replace_arith_op;
200201
mod introduce_named_generic;
@@ -213,7 +214,6 @@ mod handlers {
213214
mod unwrap_block;
214215
mod unwrap_result_return_type;
215216
mod unqualify_method_call;
216-
mod replace_is_some_with_if_let_some;
217217
mod wrap_return_type_in_result;
218218
mod into_to_qualified_from;
219219

@@ -314,6 +314,7 @@ mod handlers {
314314
replace_derive_with_manual_impl::replace_derive_with_manual_impl,
315315
replace_if_let_with_match::replace_if_let_with_match,
316316
replace_if_let_with_match::replace_match_with_if_let,
317+
replace_is_method_with_if_let_method::replace_is_method_with_if_let_method,
317318
replace_let_with_if_let::replace_let_with_if_let,
318319
replace_method_eager_lazy::replace_with_eager_method,
319320
replace_method_eager_lazy::replace_with_lazy_method,
@@ -333,7 +334,6 @@ mod handlers {
333334
unwrap_result_return_type::unwrap_result_return_type,
334335
unwrap_tuple::unwrap_tuple,
335336
unqualify_method_call::unqualify_method_call,
336-
replace_is_some_with_if_let_some::replace_is_some_with_if_let_some,
337337
wrap_return_type_in_result::wrap_return_type_in_result,
338338
// These are manually sorted for better priorities. By default,
339339
// priority is determined by the size of the target range (smaller

0 commit comments

Comments
 (0)