1+ use either:: Either ;
12use ide_db:: syntax_helpers:: suggest_name;
23use syntax:: ast:: { self , AstNode , syntax_factory:: SyntaxFactory } ;
34
@@ -24,9 +25,9 @@ pub(crate) fn replace_is_method_with_if_let_method(
2425 acc : & mut Assists ,
2526 ctx : & AssistContext < ' _ > ,
2627) -> Option < ( ) > {
27- let if_expr = ctx. find_node_at_offset :: < ast:: IfExpr > ( ) ?;
28+ let has_cond = ctx. find_node_at_offset :: < Either < ast:: IfExpr , ast :: WhileExpr > > ( ) ?;
2829
29- let cond = if_expr . condition ( ) ?;
30+ let cond = either :: for_both! ( & has_cond , it => it . condition( ) ) ?;
3031 let cond = cover_let_chain ( cond, ctx. selection_trimmed ( ) ) ?;
3132 let call_expr = match cond {
3233 ast:: Expr :: MethodCallExpr ( call) => call,
@@ -39,7 +40,7 @@ pub(crate) fn replace_is_method_with_if_let_method(
3940 let receiver = call_expr. receiver ( ) ?;
4041
4142 let mut name_generator = suggest_name:: NameGenerator :: new_from_scope_locals (
42- ctx. sema . scope ( if_expr . syntax ( ) ) ,
43+ ctx. sema . scope ( has_cond . syntax ( ) ) ,
4344 ) ;
4445 let var_name = if let ast:: Expr :: PathExpr ( path_expr) = receiver. clone ( ) {
4546 name_generator. suggest_name ( & path_expr. path ( ) ?. to_string ( ) )
@@ -48,9 +49,9 @@ pub(crate) fn replace_is_method_with_if_let_method(
4849 } ;
4950
5051 let ( assist_id, message, text) = if name_ref. text ( ) == "is_some" {
51- ( "replace_is_some_with_if_let_some" , "Replace `is_some` with `if let Some`" , "Some" )
52+ ( "replace_is_some_with_if_let_some" , "Replace `is_some` with `let Some`" , "Some" )
5253 } else {
53- ( "replace_is_ok_with_if_let_ok" , "Replace `is_ok` with `if let Ok`" , "Ok" )
54+ ( "replace_is_ok_with_if_let_ok" , "Replace `is_ok` with `let Ok`" , "Ok" )
5455 } ;
5556
5657 acc. add (
@@ -250,6 +251,25 @@ fn main() {
250251 ) ;
251252 }
252253
254+ #[ test]
255+ fn replace_is_some_with_while_let_some ( ) {
256+ check_assist (
257+ replace_is_method_with_if_let_method,
258+ r#"
259+ fn main() {
260+ let mut x = Some(1);
261+ while x.is_som$0e() { x = None }
262+ }
263+ "# ,
264+ r#"
265+ fn main() {
266+ let mut x = Some(1);
267+ while let Some(${0:x1}) = x { x = None }
268+ }
269+ "# ,
270+ ) ;
271+ }
272+
253273 #[ test]
254274 fn replace_is_some_with_if_let_some_not_applicable_after_l_curly ( ) {
255275 check_assist_not_applicable (
0 commit comments