diff options
| author | winstxnhdw <winstxnhdw@gmail.com> | 2024-07-09 22:17:07 +0100 |
|---|---|---|
| committer | winstxnhdw <winstxnhdw@gmail.com> | 2024-07-09 22:17:07 +0100 |
| commit | 36c344ee973ab579c60ce0f61e612ef5be356450 (patch) | |
| tree | f43607e8c8db2b27fcdb7b910cfea8b4c1c23200 /src/tools | |
| parent | 5445aef843f1a921ba146ace01a2cbbfc33ee38f (diff) | |
feat: do not add new enum if it already exists
Diffstat (limited to 'src/tools')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs index f094c5c09f0..36ad608468e 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs @@ -461,7 +461,17 @@ fn add_enum_def( usages: &UsageSearchResult, target_node: SyntaxNode, target_module: &hir::Module, -) { +) -> Option<()> { + if ctx + .find_node_at_offset::<ast::SourceFile>()? + .syntax() + .children() + .filter_map(|node| ast::Enum::cast(node).and_then(|e| ctx.sema.to_def(&e))) + .any(|def| def.name(ctx.db()).as_str() == Some("Bool")) + { + return None; + } + let make_enum_pub = usages .iter() .flat_map(|(_, refs)| refs) @@ -480,6 +490,8 @@ fn add_enum_def( insert_before.text_range().start(), format!("{}\n\n{indent}", enum_def.syntax().text()), ); + + Some(()) } /// Finds where to put the new enum definition. @@ -554,6 +566,33 @@ fn function(foo: Bool, bar: bool) { } #[test] + fn no_duplicate_enums() { + check_assist( + bool_to_enum, + r#" +#[derive(PartialEq, Eq)] +enum Bool { True, False } + +fn function(foo: bool, $0bar: bool) { + if bar { + println!("bar"); + } +} +"#, + r#" +#[derive(PartialEq, Eq)] +enum Bool { True, False } + +fn function(foo: bool, bar: Bool) { + if bar == Bool::True { + println!("bar"); + } +} +"#, + ) + } + + #[test] fn parameter_with_last_param_usage() { check_assist( bool_to_enum, |
