Adjusted the hashset buckets to lump functions/consts together as their names must be unique.

This commit is contained in:
Kevin DeLorey 2020-02-10 21:09:04 -06:00
parent 785723e0d9
commit f0f242cb4f

View File

@ -9,24 +9,25 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
/// Generate a collection of associated items that are missing from a
/// `impl Trait for` block.
pub fn get_missing_impl_items( pub fn get_missing_impl_items(
db: &impl HirDatabase, db: &impl HirDatabase,
analyzer: &hir::SourceAnalyzer, analyzer: &hir::SourceAnalyzer,
impl_block: &ast::ImplBlock, impl_block: &ast::ImplBlock,
) -> Vec<hir::AssocItem> { ) -> Vec<hir::AssocItem> {
// since the names are unique only to each associated type (fn/type/const),
// create buckets of each already implemented type that we'll use in the // Names must be unique between constants and functions. However, type aliases
// lookup later. // may share the same name as a function or constant.
let mut impl_fns = FxHashSet::default(); let mut impl_fns_consts = FxHashSet::default();
let mut impl_type = FxHashSet::default(); let mut impl_type = FxHashSet::default();
let mut impl_const = FxHashSet::default();
if let Some(item_list) = impl_block.item_list() { if let Some(item_list) = impl_block.item_list() {
for item in item_list.impl_items() { for item in item_list.impl_items() {
match item { match item {
ast::ImplItem::FnDef(f) => { ast::ImplItem::FnDef(f) => {
if let Some(n) = f.name() { if let Some(n) = f.name() {
impl_fns.insert(n.syntax().to_string()); impl_fns_consts.insert(n.syntax().to_string());
} }
} }
@ -38,7 +39,7 @@ pub fn get_missing_impl_items(
ast::ImplItem::ConstDef(c) => { ast::ImplItem::ConstDef(c) => {
if let Some(n) = c.name() { if let Some(n) = c.name() {
impl_const.insert(n.syntax().to_string()); impl_fns_consts.insert(n.syntax().to_string());
} }
} }
} }
@ -50,10 +51,10 @@ pub fn get_missing_impl_items(
.items(db) .items(db)
.iter() .iter()
.filter(|i| match i { .filter(|i| match i {
hir::AssocItem::Function(f) => !impl_fns.contains(&f.name(db).to_string()), hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()),
hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()),
hir::AssocItem::Const(c) => { hir::AssocItem::Const(c) => {
c.name(db).map(|n| !impl_const.contains(&n.to_string())).unwrap_or_default() c.name(db).map(|n| !impl_fns_consts.contains(&n.to_string())).unwrap_or_default()
} }
}) })
.map(|i| i.clone()) .map(|i| i.clone())