]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide-db/src/use_trivial_contructor.rs
Auto merge of #102692 - nnethercote:TokenStreamBuilder, r=Aaron1011
[rust.git] / src / tools / rust-analyzer / crates / ide-db / src / use_trivial_contructor.rs
1 //! Functionality for generating trivial contructors
2
3 use hir::StructKind;
4 use syntax::ast;
5
6 /// given a type return the trivial contructor (if one exists)
7 pub fn use_trivial_constructor(
8     db: &crate::RootDatabase,
9     path: ast::Path,
10     ty: &hir::Type,
11 ) -> Option<ast::Expr> {
12     match ty.as_adt() {
13         Some(hir::Adt::Enum(x)) => {
14             if let &[variant] = &*x.variants(db) {
15                 if variant.kind(db) == hir::StructKind::Unit {
16                     let path = ast::make::path_qualified(
17                         path,
18                         syntax::ast::make::path_segment(ast::make::name_ref(
19                             &variant.name(db).to_smol_str(),
20                         )),
21                     );
22
23                     return Some(syntax::ast::make::expr_path(path));
24                 }
25             }
26         }
27         Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => {
28             return Some(syntax::ast::make::expr_path(path));
29         }
30         _ => {}
31     }
32
33     None
34 }