|
| 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 | +} |
0 commit comments