Skip to content

Commit c4bdb8e

Browse files
committed
feat: add config for inserting must_use in generate_enum_as_method
1 parent 97b357e commit c4bdb8e

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

crates/ide-assists/src/assist_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pub struct AssistConfig {
1414
pub allowed: Option<Vec<AssistKind>>,
1515
pub insert_use: InsertUseConfig,
1616
pub prefer_no_std: bool,
17+
pub assist_emit_must_use: bool,
1718
}

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ fn generate_enum_projection_method(
124124
happy_case,
125125
sad_case,
126126
} = props;
127+
127128
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
128129
let variant_name = variant.name()?;
129130
let parent_enum = ast::Adt::Enum(variant.parent_enum());
@@ -144,7 +145,7 @@ fn generate_enum_projection_method(
144145
ast::StructKind::Unit => return None,
145146
};
146147

147-
let fn_name = format!("{}_{}", fn_name_prefix, &to_lower_snake_case(&variant_name.text()));
148+
let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text()));
148149

149150
// Return early if we've found an existing new fn
150151
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;
@@ -156,15 +157,31 @@ fn generate_enum_projection_method(
156157
assist_description,
157158
target,
158159
|builder| {
159-
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{v} "));
160-
let method = format!(
161-
" {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type}{return_suffix} {{
160+
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
161+
162+
let field_type_syntax = field_type.syntax();
163+
164+
let method = if ctx.config.assist_emit_must_use
165+
{
166+
format!(
167+
" #[must_use]
168+
{vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{
169+
if let Self::{variant_name}{pattern_suffix} = self {{
170+
{happy_case}({bound_name})
171+
}} else {{
172+
{sad_case}
173+
}}
174+
}}")
175+
} else {
176+
format!(
177+
" {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{
162178
if let Self::{variant_name}{pattern_suffix} = self {{
163179
{happy_case}({bound_name})
164180
}} else {{
165181
{sad_case}
166182
}}
167-
}}");
183+
}}")
184+
};
168185

169186
add_method_to_adt(builder, &parent_enum, impl_def, &method);
170187
},

crates/ide-assists/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
3030
skip_glob_imports: true,
3131
},
3232
prefer_no_std: false,
33+
assist_emit_must_use: false,
3334
};
3435

3536
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ mod patch_old_style;
5656
// parsing the old name.
5757
config_data! {
5858
struct ConfigData {
59+
/// Whether to insert must_use derive macro while generating `as_` methods
60+
/// for enum variants.
61+
assist_emitMustUse: bool = "false",
5962
/// Placeholder expression to use for missing expressions in assists.
6063
assist_expressionFillDefault: ExprFillDefaultDef = "\"todo\"",
6164

@@ -1227,6 +1230,7 @@ impl Config {
12271230
allowed: None,
12281231
insert_use: self.insert_use_config(),
12291232
prefer_no_std: self.data.imports_prefer_no_std,
1233+
assist_emit_must_use: self.data.assist_emitMustUse,
12301234
}
12311235
}
12321236

docs/user/generated_config.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[[rust-analyzer.assist.emitMustUse]]rust-analyzer.assist.emitMustUse (default: `false`)::
2+
+
3+
--
4+
Whether to insert must_use derive macro while generating `as_` methods
5+
for enum variants.
6+
--
17
[[rust-analyzer.assist.expressionFillDefault]]rust-analyzer.assist.expressionFillDefault (default: `"todo"`)::
28
+
39
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@
397397
"type": "boolean"
398398
},
399399
"$generated-start": {},
400+
"rust-analyzer.assist.emitMustUse": {
401+
"markdownDescription": "Whether to insert must_use derive macro while generating `as_` methods\nfor enum variants.",
402+
"default": false,
403+
"type": "boolean"
404+
},
400405
"rust-analyzer.assist.expressionFillDefault": {
401406
"markdownDescription": "Placeholder expression to use for missing expressions in assists.",
402407
"default": "todo",

0 commit comments

Comments
 (0)