From f9379df630c860619813f3a2466f30b4b2c1a60b Mon Sep 17 00:00:00 2001 From: soruh Date: Wed, 22 Jun 2022 16:34:01 +0200 Subject: [PATCH] add use_trivial_contructor.rs --- crates/ide-db/src/use_trivial_contructor.rs | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 crates/ide-db/src/use_trivial_contructor.rs diff --git a/crates/ide-db/src/use_trivial_contructor.rs b/crates/ide-db/src/use_trivial_contructor.rs new file mode 100644 index 0000000000..948670380f --- /dev/null +++ b/crates/ide-db/src/use_trivial_contructor.rs @@ -0,0 +1,31 @@ +use hir::StructKind; +use syntax::ast; + +pub fn use_trivial_constructor( + db: &crate::RootDatabase, + path: ast::Path, + ty: &hir::Type, +) -> Option { + 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 +}