Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs30
1 files changed, 25 insertions, 5 deletions
diff --git a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs
index c57fd4d439..5a2307739c 100644
--- a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs
+++ b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs
@@ -1,3 +1,4 @@
+use either::Either;
use ide_db::syntax_helpers::suggest_name;
use syntax::ast::{self, AstNode, syntax_factory::SyntaxFactory};
@@ -24,9 +25,9 @@ pub(crate) fn replace_is_method_with_if_let_method(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
- let if_expr = ctx.find_node_at_offset::<ast::IfExpr>()?;
+ let has_cond = ctx.find_node_at_offset::<Either<ast::IfExpr, ast::WhileExpr>>()?;
- let cond = if_expr.condition()?;
+ let cond = either::for_both!(&has_cond, it => it.condition())?;
let cond = cover_let_chain(cond, ctx.selection_trimmed())?;
let call_expr = match cond {
ast::Expr::MethodCallExpr(call) => call,
@@ -39,7 +40,7 @@ pub(crate) fn replace_is_method_with_if_let_method(
let receiver = call_expr.receiver()?;
let mut name_generator = suggest_name::NameGenerator::new_from_scope_locals(
- ctx.sema.scope(if_expr.syntax()),
+ ctx.sema.scope(has_cond.syntax()),
);
let var_name = if let ast::Expr::PathExpr(path_expr) = receiver.clone() {
name_generator.suggest_name(&path_expr.path()?.to_string())
@@ -48,9 +49,9 @@ pub(crate) fn replace_is_method_with_if_let_method(
};
let (assist_id, message, text) = if name_ref.text() == "is_some" {
- ("replace_is_some_with_if_let_some", "Replace `is_some` with `if let Some`", "Some")
+ ("replace_is_some_with_if_let_some", "Replace `is_some` with `let Some`", "Some")
} else {
- ("replace_is_ok_with_if_let_ok", "Replace `is_ok` with `if let Ok`", "Ok")
+ ("replace_is_ok_with_if_let_ok", "Replace `is_ok` with `let Ok`", "Ok")
};
acc.add(
@@ -251,6 +252,25 @@ fn main() {
}
#[test]
+ fn replace_is_some_with_while_let_some() {
+ check_assist(
+ replace_is_method_with_if_let_method,
+ r#"
+fn main() {
+ let mut x = Some(1);
+ while x.is_som$0e() { x = None }
+}
+"#,
+ r#"
+fn main() {
+ let mut x = Some(1);
+ while let Some(${0:x1}) = x { x = None }
+}
+"#,
+ );
+ }
+
+ #[test]
fn replace_is_some_with_if_let_some_not_applicable_after_l_curly() {
check_assist_not_applicable(
replace_is_method_with_if_let_method,