From 586acef5283e78f19e5e9daf4e8aac40e2a9ff16 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 22:44:24 +0300 Subject: Simplify ADT fields --- crates/ra_hir/src/code_model.rs | 14 +++++--------- crates/ra_hir/src/from_source.rs | 3 +-- crates/ra_hir/src/ty/lower.rs | 16 +++++----------- 3 files changed, 11 insertions(+), 22 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 96a5cc857..ab0544a4c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -301,7 +301,7 @@ pub enum FieldSource { impl StructField { pub fn name(&self, db: &impl HirDatabase) -> Name { - self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() + self.parent.variant_data(db).fields()[self.id].name.clone() } pub fn ty(&self, db: &impl HirDatabase) -> Ty { @@ -335,8 +335,7 @@ impl Struct { db.struct_data(self.id.into()) .variant_data .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: self.into(), id }) .collect() } @@ -345,8 +344,7 @@ impl Struct { db.struct_data(self.id.into()) .variant_data .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .find(|(_id, data)| data.name == *name) .map(|(id, _)| StructField { parent: self.into(), id }) } @@ -443,8 +441,7 @@ impl EnumVariant { pub fn fields(self, db: &impl HirDatabase) -> Vec { self.variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: self.into(), id }) .collect() } @@ -452,8 +449,7 @@ impl EnumVariant { pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option { self.variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .find(|(_id, data)| data.name == *name) .map(|(id, _)| StructField { parent: self.into(), id }) } diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 72d059a27..1e7c22774 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -200,8 +200,7 @@ impl FromSource for StructField { variant_def .variant_data(db) .fields() - .into_iter() - .flat_map(|it| it.iter()) + .iter() .map(|(id, _)| StructField { parent: variant_def, id }) .find(|f| f.source(db) == src) } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 2272510e8..2b40cb07d 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -557,7 +557,7 @@ pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { VariantDef::EnumVariant(it) => it.parent.id.resolver(db), }; let var_data = parent_def.variant_data(db); - let type_ref = &var_data.fields().unwrap()[field.id].type_ref; + let type_ref = &var_data.fields()[field.id].type_ref; Ty::from_hir(db, &resolver, type_ref) } @@ -696,10 +696,7 @@ impl From> for Uncertain { fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { let struct_data = db.struct_data(def.id.into()); - let fields = match struct_data.variant_data.fields() { - Some(fields) => fields, - None => panic!("fn_sig_for_struct_constructor called on unit struct"), - }; + let fields = struct_data.variant_data.fields(); let resolver = def.id.resolver(db); let params = fields .iter() @@ -712,7 +709,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { /// Build the type of a tuple struct constructor. fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { let struct_data = db.struct_data(def.id.into()); - if struct_data.variant_data.fields().is_none() { + if struct_data.variant_data.is_unit() { return type_for_adt(db, def); // Unit struct } let generics = db.generic_params(def.id.into()); @@ -722,10 +719,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { let var_data = def.variant_data(db); - let fields = match var_data.fields() { - Some(fields) => fields, - None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), - }; + let fields = var_data.fields(); let resolver = def.parent.id.resolver(db); let params = fields .iter() @@ -740,7 +734,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) /// Build the type of a tuple enum variant constructor. fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { let var_data = def.variant_data(db); - if var_data.fields().is_none() { + if var_data.is_unit() { return type_for_adt(db, def.parent_enum(db)); // Unit variant } let generics = db.generic_params(def.parent_enum(db).id.into()); -- cgit v1.2.3