Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-db/src/use_trivial_constructor.rs')
| -rw-r--r-- | crates/ide-db/src/use_trivial_constructor.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/crates/ide-db/src/use_trivial_constructor.rs b/crates/ide-db/src/use_trivial_constructor.rs new file mode 100644 index 0000000000..39431bed38 --- /dev/null +++ b/crates/ide-db/src/use_trivial_constructor.rs @@ -0,0 +1,34 @@ +//! Functionality for generating trivial contructors + +use hir::StructKind; +use syntax::ast; + +/// given a type return the trivial contructor (if one exists) +pub fn use_trivial_constructor( + db: &crate::RootDatabase, + path: ast::Path, + ty: &hir::Type, +) -> Option<ast::Expr> { + match ty.as_adt() { + Some(hir::Adt::Enum(x)) => { + if let &[variant] = &*x.variants(db) { + if variant.kind(db) == hir::StructKind::Unit { + let path = ast::make::path_qualified( + path, + syntax::ast::make::path_segment(ast::make::name_ref( + &variant.name(db).to_smol_str(), + )), + ); + + return Some(syntax::ast::make::expr_path(path)); + } + } + } + Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => { + return Some(syntax::ast::make::expr_path(path)); + } + _ => {} + } + + None +} |