Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f9379df

Browse files
committed
add use_trivial_contructor.rs
1 parent f780145 commit f9379df

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use hir::StructKind;
2+
use syntax::ast;
3+
4+
pub fn use_trivial_constructor(
5+
db: &crate::RootDatabase,
6+
path: ast::Path,
7+
ty: &hir::Type,
8+
) -> Option<ast::Expr> {
9+
match ty.as_adt() {
10+
Some(hir::Adt::Enum(x)) => {
11+
if let &[variant] = &*x.variants(db) {
12+
if variant.kind(db) == hir::StructKind::Unit {
13+
let path = ast::make::path_qualified(
14+
path,
15+
syntax::ast::make::path_segment(ast::make::name_ref(
16+
&variant.name(db).to_smol_str(),
17+
)),
18+
);
19+
20+
return Some(syntax::ast::make::expr_path(path));
21+
}
22+
}
23+
}
24+
Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => {
25+
return Some(syntax::ast::make::expr_path(path));
26+
}
27+
_ => {}
28+
}
29+
30+
None
31+
}

0 commit comments

Comments
 (0)