From 4bf9b9b0031f64755bfa102a3cf9363aadea98c5 Mon Sep 17 00:00:00 2001 From: feniljain Date: Mon, 24 Oct 2022 21:06:32 +0530 Subject: [PATCH] refactor: remove repetitive string interpolation and doc changes --- .../generate_enum_projection_method.rs | 24 +++++++------------ crates/rust-analyzer/src/config.rs | 2 +- docs/user/generated_config.adoc | 2 +- editors/code/package.json | 2 +- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs index 732ee49f66d..ad88a04ce87 100644 --- a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs +++ b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs @@ -161,27 +161,21 @@ fn generate_enum_projection_method( let field_type_syntax = field_type.syntax(); - let method = if ctx.config.assist_emit_must_use - { - format!( - " #[must_use] - {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ - if let Self::{variant_name}{pattern_suffix} = self {{ - {happy_case}({bound_name}) - }} else {{ - {sad_case} - }} - }}") + let must_use = if ctx.config.assist_emit_must_use { + "#[must_use]\n" } else { - format!( - " {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ + "" + }; + + let method = format!( + " {must_use}{vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ if let Self::{variant_name}{pattern_suffix} = self {{ {happy_case}({bound_name}) }} else {{ {sad_case} }} - }}") - }; + }}" + ); add_method_to_adt(builder, &parent_enum, impl_def, &method); }, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 21d7538fdc1..9bd0e22d8fc 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -56,7 +56,7 @@ // parsing the old name. config_data! { struct ConfigData { - /// Whether to insert must_use derive macro while generating `as_` methods + /// Whether to insert #[must_use] when generating `as_` methods /// for enum variants. assist_emitMustUse: bool = "false", /// Placeholder expression to use for missing expressions in assists. diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 82ec1d56f2b..152b7c5d815 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -1,7 +1,7 @@ [[rust-analyzer.assist.emitMustUse]]rust-analyzer.assist.emitMustUse (default: `false`):: + -- -Whether to insert must_use derive macro while generating `as_` methods +Whether to insert #[must_use] when generating `as_` methods for enum variants. -- [[rust-analyzer.assist.expressionFillDefault]]rust-analyzer.assist.expressionFillDefault (default: `"todo"`):: diff --git a/editors/code/package.json b/editors/code/package.json index 1446a603772..a25e4313844 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -398,7 +398,7 @@ }, "$generated-start": {}, "rust-analyzer.assist.emitMustUse": { - "markdownDescription": "Whether to insert must_use derive macro while generating `as_` methods\nfor enum variants.", + "markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.", "default": false, "type": "boolean" },