2022-06-22 09:49:50 -05:00
|
|
|
//! Functionality for generating trivial contructors
|
|
|
|
|
2022-06-22 09:34:01 -05:00
|
|
|
use hir::StructKind;
|
|
|
|
use syntax::ast;
|
|
|
|
|
2022-06-22 09:49:50 -05:00
|
|
|
/// given a type return the trivial contructor (if one exists)
|
2022-06-22 09:34:01 -05:00
|
|
|
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
|
|
|
|
}
|