feat(diagnostics): use default expression instead of todo! when missing fields

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2022-01-07 14:13:34 +01:00
parent 0a4239a815
commit b60a29ca94
6 changed files with 12 additions and 13 deletions

View file

@ -73,7 +73,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
let generate_fill_expr = |ty: &Type| match ctx.config.expr_fill_default { let generate_fill_expr = |ty: &Type| match ctx.config.expr_fill_default {
crate::ExprFillDefaultMode::Todo => Some(make::ext::expr_todo()), crate::ExprFillDefaultMode::Todo => Some(make::ext::expr_todo()),
crate::ExprFillDefaultMode::DefaultImpl => { crate::ExprFillDefaultMode::Default => {
let default_constr = get_default_constructor(ctx, d, ty); let default_constr = get_default_constructor(ctx, d, ty);
match default_constr { match default_constr {
Some(default_constr) => Some(default_constr), Some(default_constr) => Some(default_constr),
@ -159,7 +159,6 @@ fn get_default_constructor(
if let AssocItem::Function(func) = assoc_item { if let AssocItem::Function(func) = assoc_item {
if func.name(ctx.sema.db) == known::new if func.name(ctx.sema.db) == known::new
&& func.assoc_fn_params(ctx.sema.db).is_empty() && func.assoc_fn_params(ctx.sema.db).is_empty()
&& func.self_param(ctx.sema.db).is_none()
{ {
return Some(()); return Some(());
} }

View file

@ -132,7 +132,7 @@ pub enum Severity {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExprFillDefaultMode { pub enum ExprFillDefaultMode {
Todo, Todo,
DefaultImpl, Default,
} }
impl Default for ExprFillDefaultMode { impl Default for ExprFillDefaultMode {
fn default() -> Self { fn default() -> Self {

View file

@ -37,7 +37,7 @@ fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
let (db, file_position) = RootDatabase::with_position(ra_fixture_before); let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
let mut conf = DiagnosticsConfig::default(); let mut conf = DiagnosticsConfig::default();
conf.expr_fill_default = ExprFillDefaultMode::DefaultImpl; conf.expr_fill_default = ExprFillDefaultMode::Default;
let diagnostic = let diagnostic =
super::diagnostics(&db, &conf, &AssistResolveStrategy::All, file_position.file_id) super::diagnostics(&db, &conf, &AssistResolveStrategy::All, file_position.file_id)
.pop() .pop()

View file

@ -698,7 +698,7 @@ impl Config {
disabled: self.data.diagnostics_disabled.clone(), disabled: self.data.diagnostics_disabled.clone(),
expr_fill_default: match self.data.assist_exprFillDefault { expr_fill_default: match self.data.assist_exprFillDefault {
ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo, ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo,
ExprFillDefaultDef::DefaultImpl => ExprFillDefaultMode::DefaultImpl, ExprFillDefaultDef::Default => ExprFillDefaultMode::Default,
}, },
} }
} }
@ -1070,8 +1070,8 @@ enum ManifestOrProjectJson {
pub enum ExprFillDefaultDef { pub enum ExprFillDefaultDef {
#[serde(alias = "todo")] #[serde(alias = "todo")]
Todo, Todo,
#[serde(alias = "defaultImpl")] #[serde(alias = "default")]
DefaultImpl, Default,
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@ -1270,9 +1270,9 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
}, },
"ExprFillDefaultDef" => set! { "ExprFillDefaultDef" => set! {
"type": "string", "type": "string",
"enum": ["todo", "defaultImpl"], "enum": ["todo", "default"],
"enumDescriptions": [ "enumDescriptions": [
"Fill missing elements with 'todo' macro", "Fill missing expressions with the 'todo' macro",
"Fill missing expressions with reasonable defaults, `new` or `default` constructors." "Fill missing expressions with reasonable defaults, `new` or `default` constructors."
], ],
}, },

View file

@ -76,7 +76,7 @@ pub mod ext {
expr_from_text(r#""""#) expr_from_text(r#""""#)
} }
pub fn empty_char() -> ast::Expr { pub fn empty_char() -> ast::Expr {
expr_from_text("''") expr_from_text("'\x00'")
} }
pub fn default_bool() -> ast::Expr { pub fn default_bool() -> ast::Expr {
expr_from_text("false") expr_from_text("false")

View file

@ -384,11 +384,11 @@
"type": "string", "type": "string",
"enum": [ "enum": [
"todo", "todo",
"defaultImpl" "default"
], ],
"enumDescriptions": [ "enumDescriptions": [
"Fill missing elements with 'todo' macro", "Fill missing expressions with the 'todo' macro",
"Fill missing elements with T::default()" "Fill missing expressions with reasonable defaults, `new` or `default` constructors."
] ]
}, },
"rust-analyzer.assist.importGranularity": { "rust-analyzer.assist.importGranularity": {