Skip to content

Commit

Permalink
fix: remove unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Oct 23, 2023
1 parent a7f77d8 commit 45ee88f
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions crates/ide-assists/src/handlers/unqualify_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,38 @@ fn add_import(
ctx: &AssistContext<'_>,
edit: &mut ide_db::source_change::SourceChangeBuilder,
) {
// for `<i32 as std::ops::Add>`
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;
}
if let Some(path_segment) = qualifier.segment() {
// for `<i32 as std::ops::Add>`
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,
};

let scope = ide_db::imports::insert_use::ImportScope::find_insert_use_container(
import.syntax(),
&ctx.sema,
);
// in case for `<_>`
if import.coloncolon_token().is_none() {
return;
}

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);
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);
}
}
}

Expand Down

0 comments on commit 45ee88f

Please sign in to comment.