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 732ee49f66..ad88a04ce8 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 21d7538fdc..9bd0e22d8f 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -56,7 +56,7 @@ mod patch_old_style; // 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 82ec1d56f2..152b7c5d81 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 1446a60377..a25e431384 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" },