From 45ee88f9cb7536a725c359efc89b73c904d5d0aa Mon Sep 17 00:00:00 2001 From: Young-Flash <871946895@qq.com> Date: Mon, 23 Oct 2023 23:12:07 +0800 Subject: [PATCH] fix: remove unwrap --- .../src/handlers/unqualify_method_call.rs | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/crates/ide-assists/src/handlers/unqualify_method_call.rs b/crates/ide-assists/src/handlers/unqualify_method_call.rs index e1f812b0bf..0bf1782a48 100644 --- a/crates/ide-assists/src/handlers/unqualify_method_call.rs +++ b/crates/ide-assists/src/handlers/unqualify_method_call.rs @@ -89,31 +89,38 @@ fn add_import( ctx: &AssistContext<'_>, edit: &mut ide_db::source_change::SourceChangeBuilder, ) { - // for `` - let path_type = - qualifier.segment().unwrap().syntax().children().filter_map(ast::PathType::cast).last(); - let import = match path_type { - Some(it) => it.path().unwrap(), - None => qualifier, - }; - - // in case for `<_>` - if import.coloncolon_token().is_none() { - return; - } - - let scope = ide_db::imports::insert_use::ImportScope::find_insert_use_container( - import.syntax(), - &ctx.sema, - ); - - if let Some(scope) = scope { - let scope = match scope { - ImportScope::File(it) => ImportScope::File(edit.make_mut(it)), - ImportScope::Module(it) => ImportScope::Module(edit.make_mut(it)), - ImportScope::Block(it) => ImportScope::Block(edit.make_mut(it)), + if let Some(path_segment) = qualifier.segment() { + // for `` + let path_type = path_segment.syntax().children().filter_map(ast::PathType::cast).last(); + let import = match path_type { + Some(it) => { + if let Some(path) = it.path() { + path + } else { + return; + } + } + None => qualifier, }; - ide_db::imports::insert_use::insert_use(&scope, import, &ctx.config.insert_use); + + // in case for `<_>` + if import.coloncolon_token().is_none() { + return; + } + + let scope = ide_db::imports::insert_use::ImportScope::find_insert_use_container( + import.syntax(), + &ctx.sema, + ); + + if let Some(scope) = scope { + let scope = match scope { + ImportScope::File(it) => ImportScope::File(edit.make_mut(it)), + ImportScope::Module(it) => ImportScope::Module(edit.make_mut(it)), + ImportScope::Block(it) => ImportScope::Block(edit.make_mut(it)), + }; + ide_db::imports::insert_use::insert_use(&scope, import, &ctx.config.insert_use); + } } }