Skip to content

Commit df6fa50

Browse files
committed
feat(diagnostics): add new config to fill default expression
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent 0435463 commit df6fa50

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub use ide_db::{
118118
symbol_index::Query,
119119
RootDatabase, SymbolKind,
120120
};
121-
pub use ide_diagnostics::{Diagnostic, DiagnosticsConfig, Severity};
121+
pub use ide_diagnostics::{Diagnostic, DiagnosticsConfig, ExprFillDefaultMode, Severity};
122122
pub use ide_ssr::SsrError;
123123
pub use syntax::{TextRange, TextSize};
124124
pub use text_edit::{Indel, TextEdit};

crates/ide_diagnostics/src/handlers/missing_fields.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
6464
});
6565
let missing_fields = ctx.sema.record_literal_missing_fields(&field_list_parent);
6666

67-
let generate_default_expr = |ty: &Type| {
68-
let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate();
69-
let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
70-
71-
match default_trait {
72-
Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => {
73-
Some(make::ext::expr_default())
67+
let generate_fill_expr = |ty: &Type| match ctx.config.expr_fill_default {
68+
crate::ExprFillDefaultMode::Todo => Some(make::ext::expr_todo()),
69+
crate::ExprFillDefaultMode::DefaultImpl => {
70+
let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate();
71+
let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
72+
73+
match default_trait {
74+
Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => {
75+
Some(make::ext::expr_default())
76+
}
77+
_ => Some(make::ext::expr_todo()),
7478
}
75-
_ => Some(make::ext::expr_todo()),
7679
}
7780
};
7881

@@ -83,10 +86,10 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
8386
if ty.could_unify_with(ctx.sema.db, &candidate_ty) {
8487
None
8588
} else {
86-
generate_default_expr(ty)
89+
generate_fill_expr(ty)
8790
}
8891
} else {
89-
generate_default_expr(ty)
92+
generate_fill_expr(ty)
9093
};
9194
let field =
9295
make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)

crates/ide_diagnostics/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,22 @@ pub enum Severity {
129129
WeakWarning,
130130
}
131131

132+
#[derive(Clone, Debug, PartialEq, Eq)]
133+
pub enum ExprFillDefaultMode {
134+
Todo,
135+
DefaultImpl,
136+
}
137+
impl Default for ExprFillDefaultMode {
138+
fn default() -> Self {
139+
Self::Todo
140+
}
141+
}
142+
132143
#[derive(Default, Debug, Clone)]
133144
pub struct DiagnosticsConfig {
134145
pub disable_experimental: bool,
135146
pub disabled: FxHashSet<String>,
147+
pub expr_fill_default: ExprFillDefaultMode,
136148
}
137149

138150
struct DiagnosticsContext<'a> {

crates/rust-analyzer/src/config.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::{ffi::OsString, iter, path::PathBuf};
1111

1212
use flycheck::FlycheckConfig;
1313
use ide::{
14-
AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig,
15-
HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope,
14+
AssistConfig, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, HighlightRelatedConfig,
15+
HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope,
1616
};
1717
use ide_db::helpers::{
1818
insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -45,6 +45,8 @@ use crate::{
4545
// parsing the old name.
4646
config_data! {
4747
struct ConfigData {
48+
/// How assists will fill missing elements in an expression.
49+
assist_exprFillDefault: ExprFillDefaultDef = "\"todo\"",
4850
/// How imports should be grouped into use statements.
4951
assist_importGranularity |
5052
assist_importMergeBehavior |
@@ -694,6 +696,10 @@ impl Config {
694696
DiagnosticsConfig {
695697
disable_experimental: !self.data.diagnostics_enableExperimental,
696698
disabled: self.data.diagnostics_disabled.clone(),
699+
expr_fill_default: match self.data.assist_exprFillDefault {
700+
ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo,
701+
ExprFillDefaultDef::DefaultImpl => ExprFillDefaultMode::DefaultImpl,
702+
},
697703
}
698704
}
699705
pub fn diagnostics_map(&self) -> DiagnosticsMapConfig {
@@ -1059,6 +1065,15 @@ enum ManifestOrProjectJson {
10591065
ProjectJson(ProjectJsonData),
10601066
}
10611067

1068+
#[derive(Deserialize, Debug, Clone)]
1069+
#[serde(rename_all = "snake_case")]
1070+
pub enum ExprFillDefaultDef {
1071+
#[serde(alias = "todo")]
1072+
Todo,
1073+
#[serde(alias = "defaultImpl")]
1074+
DefaultImpl,
1075+
}
1076+
10621077
#[derive(Deserialize, Debug, Clone)]
10631078
#[serde(rename_all = "snake_case")]
10641079
enum ImportGranularityDef {
@@ -1253,6 +1268,14 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
12531268
"Merge imports from the same module into a single `use` statement."
12541269
],
12551270
},
1271+
"ExprFillDefaultDef" => set! {
1272+
"type": "string",
1273+
"enum": ["todo", "defaultImpl"],
1274+
"enumDescriptions": [
1275+
"Fill missing elements with 'todo' macro",
1276+
"Fill missing elements with Default::default()"
1277+
],
1278+
},
12561279
"ImportGranularityDef" => set! {
12571280
"type": "string",
12581281
"enum": ["preserve", "crate", "module", "item"],

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
[[rust-analyzer.assist.exprFillDefault]]rust-analyzer.assist.exprFillDefault (default: `"todo"`)::
2+
+
3+
--
4+
How assists will fill missing elements in an expression.
5+
--
16
[[rust-analyzer.assist.importGranularity]]rust-analyzer.assist.importGranularity (default: `"crate"`)::
27
+
38
--

editors/code/package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,19 @@
378378
"markdownDescription": "Optional settings passed to the debug engine. Example: `{ \"lldb\": { \"terminal\":\"external\"} }`"
379379
},
380380
"$generated-start": {},
381+
"rust-analyzer.assist.exprFillDefault": {
382+
"markdownDescription": "How assists will fill missing elements in an expression.",
383+
"default": "todo",
384+
"type": "string",
385+
"enum": [
386+
"todo",
387+
"defaultImpl"
388+
],
389+
"enumDescriptions": [
390+
"Fill missing elements with 'todo' macro",
391+
"Fill missing elements with Default::default()"
392+
]
393+
},
381394
"rust-analyzer.assist.importGranularity": {
382395
"markdownDescription": "How imports should be grouped into use statements.",
383396
"default": "crate",
@@ -1494,4 +1507,4 @@
14941507
]
14951508
}
14961509
}
1497-
}
1510+
}

0 commit comments

Comments
 (0)