Remove superfluous early returns

This commit is contained in:
Lukas Wirth 2022-06-17 17:49:25 +02:00
parent 85b68b1f7d
commit d97a8ee865
8 changed files with 41 additions and 59 deletions

View file

@ -82,16 +82,15 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
}; };
match qualified { match qualified {
Qualified::With { resolution, is_super_chain, .. } => { Qualified::With {
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
is_super_chain,
..
} => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }
let module = match resolution {
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
_ => return,
};
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_attr(ctx.db, def) { if let Some(def) = module_or_attr(ctx.db, def) {
acc.add_resolution(ctx, name, def); acc.add_resolution(ctx, name, def);
@ -110,7 +109,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }
Qualified::Infer => {} Qualified::Infer | Qualified::With { .. } => {}
} }
let attributes = annotated_item_kind.and_then(|kind| { let attributes = annotated_item_kind.and_then(|kind| {

View file

@ -21,16 +21,15 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let core = ctx.famous_defs().core(); let core = ctx.famous_defs().core();
match qualified { match qualified {
Qualified::With { resolution, is_super_chain, .. } => { Qualified::With {
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
is_super_chain,
..
} => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }
let module = match resolution {
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
_ => return,
};
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
let add_def = match def { let add_def = match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => { ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => {
@ -101,7 +100,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }
Qualified::Infer => {} Qualified::Infer | Qualified::With { .. } => {}
} }
} }

View file

@ -65,11 +65,8 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
.into_iter() .into_iter()
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, ctx, item)), .for_each(|item| add_assoc_item(acc, ctx, item)),
Qualified::With { resolution, .. } => { Qualified::With { resolution: None, .. } => {}
let resolution = match resolution { Qualified::With { resolution: Some(resolution), .. } => {
Some(it) => it,
None => return,
};
// Add associated types on type parameters and `Self`. // Add associated types on type parameters and `Self`.
ctx.scope.assoc_type_shorthand_candidates(resolution, |_, alias| { ctx.scope.assoc_type_shorthand_candidates(resolution, |_, alias| {
acc.add_type_alias(ctx, alias); acc.add_type_alias(ctx, alias);

View file

@ -44,14 +44,16 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
} }
match qualified { match qualified {
Qualified::With { resolution, is_super_chain, .. } => { Qualified::With {
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution { resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
is_super_chain,
..
} => {
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_fn_macro(ctx.db, def) { if let Some(def) = module_or_fn_macro(ctx.db, def) {
acc.add_resolution(ctx, name, def); acc.add_resolution(ctx, name, def);
} }
} }
}
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
@ -66,7 +68,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }
Qualified::Infer | Qualified::No => {} Qualified::Infer | Qualified::No | Qualified::With { .. } => {}
} }
} }

View file

@ -114,16 +114,11 @@ fn pattern_path_completion(
PathCompletionCtx { qualified, .. }: &PathCompletionCtx, PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
) { ) {
match qualified { match qualified {
Qualified::With { resolution, is_super_chain, .. } => { Qualified::With { resolution: Some(resolution), is_super_chain, .. } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }
let resolution = match resolution {
Some(it) => it,
None => return,
};
match resolution { match resolution {
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => { hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
let module_scope = module.scope(ctx.db, Some(ctx.module)); let module_scope = module.scope(ctx.db, Some(ctx.module));
@ -208,6 +203,6 @@ fn pattern_path_completion(
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }
Qualified::Infer => {} Qualified::Infer | Qualified::With { .. } => {}
} }
} }

View file

@ -58,11 +58,8 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
.into_iter() .into_iter()
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, item)), .for_each(|item| add_assoc_item(acc, item)),
Qualified::With { resolution, .. } => { Qualified::With { resolution: None, .. } => {}
let resolution = match resolution { Qualified::With { resolution: Some(resolution), .. } => {
Some(it) => it,
None => return,
};
// Add associated types on type parameters and `Self`. // Add associated types on type parameters and `Self`.
ctx.scope.assoc_type_shorthand_candidates(resolution, |_, alias| { ctx.scope.assoc_type_shorthand_candidates(resolution, |_, alias| {
acc.add_type_alias(ctx, alias); acc.add_type_alias(ctx, alias);

View file

@ -29,7 +29,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
}; };
match qualified { match qualified {
Qualified::With { path, resolution, is_super_chain } => { Qualified::With { path, resolution: Some(resolution), is_super_chain } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }
@ -43,11 +43,6 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
acc.add_keyword(ctx, "self"); acc.add_keyword(ctx, "self");
} }
let resolution = match resolution {
Some(it) => it,
None => return,
};
let mut already_imported_names = FxHashSet::default(); let mut already_imported_names = FxHashSet::default();
if let Some(list) = ctx.token.parent_ancestors().find_map(ast::UseTreeList::cast) { if let Some(list) = ctx.token.parent_ancestors().find_map(ast::UseTreeList::cast) {
let use_tree = list.parent_use_tree(); let use_tree = list.parent_use_tree();
@ -135,6 +130,6 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }
Qualified::Infer => {} Qualified::Infer | Qualified::With { resolution: None, .. } => {}
} }
} }

View file

@ -16,28 +16,26 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext)
}; };
match qualified { match qualified {
Qualified::With { resolution, is_super_chain, .. } => { Qualified::With {
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
is_super_chain,
..
} => {
// Try completing next child module of the path that is still a parent of the current module // Try completing next child module of the path that is still a parent of the current module
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution { let next_towards_current =
let next_towards_current = ctx ctx.module.path_to_root(ctx.db).into_iter().take_while(|it| it != module).last();
.module
.path_to_root(ctx.db)
.into_iter()
.take_while(|it| it != module)
.last();
if let Some(next) = next_towards_current { if let Some(next) = next_towards_current {
if let Some(name) = next.name(ctx.db) { if let Some(name) = next.name(ctx.db) {
cov_mark::hit!(visibility_qualified); cov_mark::hit!(visibility_qualified);
acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into())); acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into()));
} }
} }
}
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }
} }
Qualified::Absolute | Qualified::Infer => {} Qualified::Absolute | Qualified::Infer | Qualified::With { .. } => {}
Qualified::No => { Qualified::No => {
if !has_in_token { if !has_in_token {
cov_mark::hit!(kw_completion_in); cov_mark::hit!(kw_completion_in);