Skip to content

Commit 4b44a1d

Browse files
committed
feat: add config for inserting must_use in generate_enum_as_method
1 parent a415fb4 commit 4b44a1d

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
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_generate_enum_projection_method_insert_must_use_macro: bool,
1718
}

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,52 @@ fn generate_enum_projection_method(
150150
target,
151151
|builder| {
152152
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
153-
let method = format!(
154-
" {0}fn {1}({2}) -> {3}{4}{5} {{
153+
154+
let method = if ctx.config.assist_generate_enum_projection_method_insert_must_use_macro
155+
{
156+
format!(
157+
" #[must_use]
158+
{0}fn {1}({2}) -> {3}{4}{5} {{
159+
if let Self::{6}{7} = self {{
160+
{8}({9})
161+
}} else {{
162+
{10}
163+
}}
164+
}}",
165+
vis,
166+
fn_name,
167+
props.self_param,
168+
props.return_prefix,
169+
field_type.syntax(),
170+
props.return_suffix,
171+
variant_name,
172+
pattern_suffix,
173+
props.happy_case,
174+
bound_name,
175+
props.sad_case,
176+
)
177+
} else {
178+
format!(
179+
" {0}fn {1}({2}) -> {3}{4}{5} {{
155180
if let Self::{6}{7} = self {{
156181
{8}({9})
157182
}} else {{
158183
{10}
159184
}}
160185
}}",
161-
vis,
162-
fn_name,
163-
props.self_param,
164-
props.return_prefix,
165-
field_type.syntax(),
166-
props.return_suffix,
167-
variant_name,
168-
pattern_suffix,
169-
props.happy_case,
170-
bound_name,
171-
props.sad_case,
172-
);
186+
vis,
187+
fn_name,
188+
props.self_param,
189+
props.return_prefix,
190+
field_type.syntax(),
191+
props.return_suffix,
192+
variant_name,
193+
pattern_suffix,
194+
props.happy_case,
195+
bound_name,
196+
props.sad_case,
197+
)
198+
};
173199

174200
add_method_to_adt(builder, &parent_enum, impl_def, &method);
175201
},

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_generate_enum_projection_method_insert_must_use_macro: false,
3334
};
3435

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

crates/rust-analyzer/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ config_data! {
5858
struct ConfigData {
5959
/// Placeholder expression to use for missing expressions in assists.
6060
assist_expressionFillDefault: ExprFillDefaultDef = "\"todo\"",
61+
/// Whether to insert must_use derive macro while generating `as_` methods
62+
/// for enum variants.
63+
assist_generateEnumProjectionMethod_insert_must_use_macro: bool = "false",
6164

6265
/// Warm up caches on project load.
6366
cachePriming_enable: bool = "true",
@@ -1227,6 +1230,9 @@ impl Config {
12271230
allowed: None,
12281231
insert_use: self.insert_use_config(),
12291232
prefer_no_std: self.data.imports_prefer_no_std,
1233+
assist_generate_enum_projection_method_insert_must_use_macro: self
1234+
.data
1235+
.assist_generateEnumProjectionMethod_insert_must_use_macro,
12301236
}
12311237
}
12321238

docs/user/generated_config.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
--
44
Placeholder expression to use for missing expressions in assists.
55
--
6+
[[rust-analyzer.assist.generateEnumProjectionMethod.insert.must.use.macro]]rust-analyzer.assist.generateEnumProjectionMethod.insert.must.use.macro (default: `false`)::
7+
+
8+
--
9+
Whether to insert must_use derive macro while generating `as_` methods
10+
for enum variants.
11+
--
612
[[rust-analyzer.cachePriming.enable]]rust-analyzer.cachePriming.enable (default: `true`)::
713
+
814
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@
399399
"Fill missing expressions with reasonable defaults, `new` or `default` constructors."
400400
]
401401
},
402+
"rust-analyzer.assist.generateEnumProjectionMethod.insert.must.use.macro": {
403+
"markdownDescription": "Whether to insert must_use derive macro while generating `as_` methods\nfor enum variants.",
404+
"default": false,
405+
"type": "boolean"
406+
},
402407
"rust-analyzer.cachePriming.enable": {
403408
"markdownDescription": "Warm up caches on project load.",
404409
"default": true,

0 commit comments

Comments
 (0)