Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/remove_underscore.rs')
-rw-r--r--crates/ide-assists/src/handlers/remove_underscore.rs35
1 files changed, 32 insertions, 3 deletions
diff --git a/crates/ide-assists/src/handlers/remove_underscore.rs b/crates/ide-assists/src/handlers/remove_underscore.rs
index efe5c945be..1ccdcda52d 100644
--- a/crates/ide-assists/src/handlers/remove_underscore.rs
+++ b/crates/ide-assists/src/handlers/remove_underscore.rs
@@ -57,14 +57,17 @@ pub(crate) fn remove_underscore(acc: &mut Assists, ctx: &AssistContext<'_, '_>)
}
let new_name = text.trim_start_matches('_');
+ if new_name.is_empty() {
+ return None;
+ }
+ let changes =
+ def.rename(&ctx.sema, new_name, RenameDefinition::Yes, &ctx.config.rename_config()).ok()?;
+
acc.add(
AssistId::refactor("remove_underscore_from_used_variables"),
"Remove underscore from a used variable",
text_range,
|builder| {
- let changes = def
- .rename(&ctx.sema, new_name, RenameDefinition::Yes, &ctx.config.rename_config())
- .unwrap();
builder.source_change = changes;
},
)
@@ -176,6 +179,32 @@ fn foo(foo: i32) {
}
#[test]
+ fn not_applicable_when_result_is_keyword() {
+ check_assist_not_applicable(
+ remove_underscore,
+ r#"
+fn main() {
+ let _$0crate = 1;
+ _crate;
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn not_applicable_when_result_is_empty() {
+ check_assist_not_applicable(
+ remove_underscore,
+ r#"
+fn main() {
+ let _$0_ = 1;
+ __;
+}
+"#,
+ );
+ }
+
+ #[test]
fn remove_underscore_in_function_parameter() {
check_assist(
remove_underscore,