mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
add use_trivial_contructor.rs
This commit is contained in:
parent
f780145c4a
commit
f9379df630
1 changed files with 31 additions and 0 deletions
31
crates/ide-db/src/use_trivial_contructor.rs
Normal file
31
crates/ide-db/src/use_trivial_contructor.rs
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue