about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsoruh <mail@soruh.de>2022-06-22 16:34:01 +0200
committersoruh <mail@soruh.de>2022-06-22 16:34:01 +0200
commitf9379df630c860619813f3a2466f30b4b2c1a60b (patch)
treebfe817b4fa45c1dddd84364b92825f75f82de1e3
parentf780145c4a66af76bde7b07f24fb8f0bf81e1118 (diff)
downloadrust-f9379df630c860619813f3a2466f30b4b2c1a60b.tar.gz
rust-f9379df630c860619813f3a2466f30b4b2c1a60b.zip
add use_trivial_contructor.rs
-rw-r--r--crates/ide-db/src/use_trivial_contructor.rs31
1 files changed, 31 insertions, 0 deletions
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 00000000000..948670380f6
--- /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<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
+}