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

Commit f780145

Browse files
committed
apply suggestions
1 parent f52f5fe commit f780145

File tree

3 files changed

+10
-97
lines changed

3 files changed

+10
-97
lines changed

crates/ide-assists/src/handlers/generate_new.rs

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use ide_db::imports::import_assets::item_for_path_search;
1+
use ide_db::{
2+
imports::import_assets::item_for_path_search, use_trivial_contructor::use_trivial_constructor,
3+
};
24
use itertools::Itertools;
35
use stdx::format_to;
46
use syntax::ast::{self, AstNode, HasName, HasVisibility, StructKind};
@@ -8,51 +10,6 @@ use crate::{
810
AssistContext, AssistId, AssistKind, Assists,
911
};
1012

11-
// FIXME: how to depupicate with `ide-diagnostics/mssing_fields`
12-
fn use_trivial_constructor(
13-
db: &ide_db::RootDatabase,
14-
path: ast::Path,
15-
ty: &hir::Type,
16-
) -> Option<ast::Expr> {
17-
match ty.as_adt() {
18-
Some(hir::Adt::Enum(x)) => {
19-
let variants = x.variants(db);
20-
21-
if variants.len() == 1 {
22-
let variant = variants[0];
23-
24-
if variant.fields(db).is_empty() {
25-
let path = ast::make::path_qualified(
26-
path,
27-
syntax::ast::make::path_segment(ast::make::name_ref(
28-
&variant.name(db).to_smol_str(),
29-
)),
30-
);
31-
32-
let is_record = variant.kind(db) == hir::StructKind::Record;
33-
34-
return Some(if is_record {
35-
ast::Expr::RecordExpr(syntax::ast::make::record_expr(
36-
path,
37-
ast::make::record_expr_field_list(std::iter::empty()),
38-
))
39-
} else {
40-
syntax::ast::make::expr_path(path)
41-
});
42-
}
43-
}
44-
}
45-
Some(hir::Adt::Struct(x)) => {
46-
if x.fields(db).is_empty() {
47-
return Some(syntax::ast::make::expr_path(path));
48-
}
49-
}
50-
_ => {}
51-
}
52-
53-
None
54-
}
55-
5613
// Assist: generate_new
5714
//
5815
// Adds a new inherent impl for a type.
@@ -84,6 +41,8 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
8441
// Return early if we've found an existing new fn
8542
let impl_def = find_struct_impl(ctx, &ast::Adt::Struct(strukt.clone()), "new")?;
8643

44+
let current_module = ctx.sema.scope(strukt.syntax())?.module();
45+
8746
let target = strukt.syntax().text_range();
8847
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| {
8948
let mut buf = String::with_capacity(512);
@@ -94,8 +53,6 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
9453

9554
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
9655

97-
let current_module = ctx.sema.scope(strukt.syntax()).unwrap().module();
98-
9956
let trivial_constructors = field_list
10057
.fields()
10158
.map(|f| {

crates/ide-db/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod source_change;
2020
pub mod symbol_index;
2121
pub mod traits;
2222
pub mod ty_filter;
23+
pub mod use_trivial_contructor;
2324

2425
pub mod imports {
2526
pub mod import_assets;

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::{
55
};
66
use ide_db::{
77
assists::Assist, famous_defs::FamousDefs, imports::import_assets::item_for_path_search,
8-
source_change::SourceChange, FxHashMap,
8+
source_change::SourceChange, use_trivial_contructor::use_trivial_constructor, FxHashMap,
99
};
1010
use stdx::format_to;
1111
use syntax::{
@@ -17,51 +17,6 @@ use text_edit::TextEdit;
1717

1818
use crate::{fix, Diagnostic, DiagnosticsContext};
1919

20-
// FIXME: how to depupicate with `ide-assists/generate_new`
21-
fn use_trivial_constructor(
22-
db: &ide_db::RootDatabase,
23-
path: ast::Path,
24-
ty: &hir::Type,
25-
) -> Option<ast::Expr> {
26-
match ty.as_adt() {
27-
Some(hir::Adt::Enum(x)) => {
28-
let variants = x.variants(db);
29-
30-
if variants.len() == 1 {
31-
let variant = variants[0];
32-
33-
if variant.fields(db).is_empty() {
34-
let path = ast::make::path_qualified(
35-
path,
36-
syntax::ast::make::path_segment(ast::make::name_ref(
37-
&variant.name(db).to_smol_str(),
38-
)),
39-
);
40-
41-
let is_record = variant.kind(db) == hir::StructKind::Record;
42-
43-
return Some(if is_record {
44-
ast::Expr::RecordExpr(syntax::ast::make::record_expr(
45-
path,
46-
ast::make::record_expr_field_list(std::iter::empty()),
47-
))
48-
} else {
49-
syntax::ast::make::expr_path(path)
50-
});
51-
}
52-
}
53-
}
54-
Some(hir::Adt::Struct(x)) => {
55-
if x.fields(db).is_empty() {
56-
return Some(syntax::ast::make::expr_path(path));
57-
}
58-
}
59-
_ => {}
60-
}
61-
62-
None
63-
}
64-
6520
// Diagnostic: missing-fields
6621
//
6722
// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
@@ -104,8 +59,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
10459
let root = ctx.sema.db.parse_or_expand(d.file)?;
10560

10661
let current_module = match &d.field_list_parent {
107-
Either::Left(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).unwrap().module(),
108-
Either::Right(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).unwrap().module(),
62+
Either::Left(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).map(|it| it.module()),
63+
Either::Right(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).map(|it| it.module()),
10964
};
11065

11166
let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
@@ -166,7 +121,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
166121
let expr = (|| -> Option<ast::Expr> {
167122
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
168123

169-
let type_path = current_module.find_use_path(
124+
let type_path = current_module?.find_use_path(
170125
ctx.sema.db,
171126
item_for_path_search(ctx.sema.db, item_in_ns)?,
172127
)?;

0 commit comments

Comments
 (0)