Skip to content

Commit af1f48d

Browse files
committed
Auto merge of rust-lang#13359 - feniljain:feat-must-use-option, r=Veykril
feat: add config for inserting must_use in `generate_enum_as_method` Should fix rust-lang#13312 Didn't add a test because I was not sure on how to add test for a specific configuration option, tried to look for the usages for other `AssistConfig` variants but couldn't find any in `tests`. If there is a way to test this, do point me towards it. I tried to extract the formatting string as a common `template_string` and only have if-else for that, but it didn't compile :( Also it seems these tests are failing: ``` test config::tests::generate_config_documentation ... FAILED test config::tests::generate_package_json_config ... FAILED ``` Can you also point me to how to correct these 😅 ( I guess there is some command to automatically generate these? )
2 parents 12ced8f + 691ce30 commit af1f48d

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
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: 15 additions & 4 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.clone()])?;
@@ -156,15 +157,25 @@ 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 vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
161+
162+
let field_type_syntax = field_type.syntax();
163+
164+
let must_use = if ctx.config.assist_emit_must_use {
165+
"#[must_use]\n "
166+
} else {
167+
""
168+
};
169+
160170
let method = format!(
161-
" {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type}{return_suffix} {{
171+
" {must_use}{vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{
162172
if let Self::{variant_name}{pattern_suffix} = self {{
163173
{happy_case}({bound_name})
164174
}} else {{
165175
{sad_case}
166176
}}
167-
}}");
177+
}}"
178+
);
168179

169180
add_method_to_adt(builder, &parent_enum, impl_def, &method);
170181
},

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] when 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

@@ -1276,6 +1279,7 @@ impl Config {
12761279
allowed: None,
12771280
insert_use: self.insert_use_config(),
12781281
prefer_no_std: self.data.imports_prefer_no_std,
1282+
assist_emit_must_use: self.data.assist_emitMustUse,
12791283
}
12801284
}
12811285

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] when 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] when 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)