Skip to content

Commit

Permalink
Auto merge of rust-lang#13359 - feniljain:feat-must-use-option, r=Vey…
Browse files Browse the repository at this point in the history
…kril

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? )
  • Loading branch information
bors committed Nov 2, 2022
2 parents 12ced8f + 691ce30 commit af1f48d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub struct AssistConfig {
pub allowed: Option<Vec<AssistKind>>,
pub insert_use: InsertUseConfig,
pub prefer_no_std: bool,
pub assist_emit_must_use: bool,
}
19 changes: 15 additions & 4 deletions crates/ide-assists/src/handlers/generate_enum_projection_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn generate_enum_projection_method(
happy_case,
sad_case,
} = props;

let variant = ctx.find_node_at_offset::<ast::Variant>()?;
let variant_name = variant.name()?;
let parent_enum = ast::Adt::Enum(variant.parent_enum());
Expand All @@ -144,7 +145,7 @@ fn generate_enum_projection_method(
ast::StructKind::Unit => return None,
};

let fn_name = format!("{}_{}", fn_name_prefix, &to_lower_snake_case(&variant_name.text()));
let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text()));

// Return early if we've found an existing new fn
let impl_def = find_struct_impl(ctx, &parent_enum, &[fn_name.clone()])?;
Expand All @@ -156,15 +157,25 @@ fn generate_enum_projection_method(
assist_description,
target,
|builder| {
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{v} "));
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));

let field_type_syntax = field_type.syntax();

let must_use = if ctx.config.assist_emit_must_use {
"#[must_use]\n "
} else {
""
};

let method = format!(
" {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type}{return_suffix} {{
" {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);
},
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
skip_glob_imports: true,
},
prefer_no_std: false,
assist_emit_must_use: false,
};

pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
Expand Down
4 changes: 4 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ mod patch_old_style;
// parsing the old name.
config_data! {
struct ConfigData {
/// 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.
assist_expressionFillDefault: ExprFillDefaultDef = "\"todo\"",

Expand Down Expand Up @@ -1276,6 +1279,7 @@ impl Config {
allowed: None,
insert_use: self.insert_use_config(),
prefer_no_std: self.data.imports_prefer_no_std,
assist_emit_must_use: self.data.assist_emitMustUse,
}
}

Expand Down
6 changes: 6 additions & 0 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[[rust-analyzer.assist.emitMustUse]]rust-analyzer.assist.emitMustUse (default: `false`)::
+
--
Whether to insert #[must_use] when generating `as_` methods
for enum variants.
--
[[rust-analyzer.assist.expressionFillDefault]]rust-analyzer.assist.expressionFillDefault (default: `"todo"`)::
+
--
Expand Down
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@
"type": "boolean"
},
"$generated-start": {},
"rust-analyzer.assist.emitMustUse": {
"markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.",
"default": false,
"type": "boolean"
},
"rust-analyzer.assist.expressionFillDefault": {
"markdownDescription": "Placeholder expression to use for missing expressions in assists.",
"default": "todo",
Expand Down

0 comments on commit af1f48d

Please sign in to comment.