Repository: rust-analyzer/rust-analyzer
Pull request #7812 (2021-02-28 18:14:40)

Use consistent naming for assist
bors r+
🤖


Patch:
diff --git a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
similarity index 87%
rename from crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs
rename to crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
index 9fddf889cf..27da28bc0a 100644
--- a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs
+++ b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
@@ -3,17 +3,18 @@ use hir::known;
 use ide_db::helpers::FamousDefs;
 use stdx::format_to;
 use syntax::{ast, AstNode};
+use test_utils::mark;
 
 use crate::{AssistContext, AssistId, AssistKind, Assists};
 
-// Assist: convert_for_to_iter_for_each
+// Assist: replace_for_loop_with_for_each
 //
 // Converts a for loop into a for_each loop on the Iterator.
 //
 // ```
 // fn main() {
 //     let x = vec![1, 2, 3];
-//     for $0v in x {
+//     for$0 v in x {
 //         let y = v * 2;
 //     }
 // }
@@ -27,15 +28,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
 //     });
 // }
 // ```
-pub(crate) fn convert_for_to_iter_for_each(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
+pub(crate) fn replace_for_loop_with_for_each(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
     let for_loop = ctx.find_node_at_offset::<ast::ForExpr>()?;
     let iterable = for_loop.iterable()?;
     let pat = for_loop.pat()?;
     let body = for_loop.loop_body()?;
+    if body.syntax().text_range().start() < ctx.offset() {
+        mark::hit!(not_available_in_body);
+        return None;
+    }
 
     acc.add(
-        AssistId("convert_for_to_iter_for_each", AssistKind::RefactorRewrite),
-        "Convert a for loop into an Iterator::for_each",
+        AssistId("replace_for_loop_with_for_each", AssistKind::RefactorRewrite),
+        "Replace this for loop with `Iterator::for_each`",
         for_loop.syntax().text_range(),
         |builder| {
             let mut buf = String::new();
@@ -145,13 +150,13 @@ pub struct NoIterMethod;
             FamousDefs::FIXTURE,
             EMPTY_ITER_FIXTURE
         );
-        check_assist(convert_for_to_iter_for_each, before, after);
+        check_assist(replace_for_loop_with_for_each, before, after);
     }
 
     #[test]
     fn test_not_for() {
         check_assist_not_applicable(
-            convert_for_to_iter_for_each,
+            replace_for_loop_with_for_each,
             r"
 let mut x = vec![1, 2, 3];
 x.iter_mut().$0for_each(|v| *v *= 2);
@@ -162,7 +167,7 @@ x.iter_mut().$0for_each(|v| *v *= 2);
     #[test]
     fn test_simple_for() {
         check_assist(
-            convert_for_to_iter_for_each,
+            replace_for_loop_with_for_each,
             r"
 fn main() {
     let x = vec![1, 2, 3];
@@ -180,6 +185,21 @@ fn main() {
         )
     }
 
+    #[test]
+    fn not_available_in_body() {
+        mark::check!(not_available_in_body);
+        check_assist_not_applicable(
+            replace_for_loop_with_for_each,
+            r"
+fn main() {
+    let x = vec![1, 2, 3];
+    for v in x {
+        $0v *= 2;
+    }
+}",
+        )
+    }
+
     #[test]
     fn test_for_borrowed() {
         check_assist_with_fixtures(
@@ -255,7 +275,7 @@ fn main() {
     #[test]
     fn test_for_borrowed_mut_behind_var() {
         check_assist(
-            convert_for_to_iter_for_each,
+            replace_for_loop_with_for_each,
             r"
 fn main() {
     let x = vec![1, 2, 3];
diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
index 5a27ada6b6..be7e724b56 100644
--- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
+++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
@@ -1,5 +1,6 @@
 use std::iter::once;
 
+use ide_db::ty_filter::TryEnum;
 use syntax::{
     ast::{
         self,
@@ -10,7 +11,6 @@ use syntax::{
 };
 
 use crate::{AssistContext, AssistId, AssistKind, Assists};
-use ide_db::ty_filter::TryEnum;
 
 // Assist: replace_let_with_if_let
 //
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs
index 4c067d4511..53542d4334 100644
--- a/crates/ide_assists/src/lib.rs
+++ b/crates/ide_assists/src/lib.rs
@@ -114,7 +114,6 @@ mod handlers {
     mod apply_demorgan;
     mod auto_import;
     mod change_visibility;
-    mod convert_for_to_iter_for_each;
     mod convert_integer_literal;
     mod early_return;
     mod expand_glob_import;
@@ -132,8 +131,8 @@ mod handlers {
     mod generate_enum_projection_method;
     mod generate_from_impl_for_enum;
     mod generate_function;
-    mod generate_getter;
     mod generate_getter_mut;
+    mod generate_getter;
     mod generate_impl;
     mod generate_new;
     mod generate_setter;
@@ -156,6 +155,7 @@ mod handlers {
     mod reorder_fields;
     mod reorder_impl;
     mod replace_derive_with_manual_impl;
+    mod replace_for_loop_with_for_each;
     mod replace_if_let_with_match;
     mod replace_impl_trait_with_generic;
     mod replace_let_with_if_let;
@@ -177,11 +177,9 @@ mod handlers {
             apply_demorgan::apply_demorgan,
             auto_import::auto_import,
             change_visibility::change_visibility,
-            convert_for_to_iter_for_each::convert_for_to_iter_for_each,
             convert_integer_literal::convert_integer_literal,
             early_return::convert_to_guarded_return,
             expand_glob_import::expand_glob_import,
-            move_module_to_file::move_module_to_file,
             extract_struct_from_enum_variant::extract_struct_from_enum_variant,
             fill_match_arms::fill_match_arms,
             fix_visibility::fix_visibility,
@@ -191,12 +189,12 @@ mod handlers {
             generate_default_from_enum_variant::generate_default_from_enum_variant,
             generate_derive::generate_derive,
             generate_enum_is_method::generate_enum_is_method,
-            generate_enum_projection_method::generate_enum_try_into_method,
             generate_enum_projection_method::generate_enum_as_method,
+            generate_enum_projection_method::generate_enum_try_into_method,
             generate_from_impl_for_enum::generate_from_impl_for_enum,
             generate_function::generate_function,
-            generate_getter::generate_getter,
             generate_getter_mut::generate_getter_mut,
+            generate_getter::generate_getter,
             generate_impl::generate_impl,
             generate_new::generate_new,
             generate_setter::generate_setter,
@@ -210,6 +208,7 @@ mod handlers {
             move_bounds::move_bounds_to_where_clause,
             move_guard::move_arm_cond_to_match_guard,
             move_guard::move_guard_to_arm_body,
+            move_module_to_file::move_module_to_file,
             pull_assignment_up::pull_assignment_up,
             qualify_path::qualify_path,
             raw_string::add_hash,
@@ -221,6 +220,7 @@ mod handlers {
             reorder_fields::reorder_fields,
             reorder_impl::reorder_impl,
             replace_derive_with_manual_impl::replace_derive_with_manual_impl,
+            replace_for_loop_with_for_each::replace_for_loop_with_for_each,
             replace_if_let_with_match::replace_if_let_with_match,
             replace_if_let_with_match::replace_match_with_if_let,
             replace_impl_trait_with_generic::replace_impl_trait_with_generic,


Test patch:
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index 384eb7eeea..b7f6167607 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -190,8 +190,8 @@ fn assist_order_field_struct() {
     let mut assists = assists.iter();
 
     assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)");
-    assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method");
     assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method");
+    assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method");
     assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method");
     assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`");
 }
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 7f6dbbccf2..4f007aa48b 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -192,29 +192,6 @@ pub(crate) fn frobnicate() {}
     )
 }
 
-#[test]
-fn doctest_convert_for_to_iter_for_each() {
-    check_doc_test(
-        "convert_for_to_iter_for_each",
-        r#####"
-fn main() {
-    let x = vec![1, 2, 3];
-    for $0v in x {
-        let y = v * 2;
-    }
-}
-"#####,
-        r#####"
-fn main() {
-    let x = vec![1, 2, 3];
-    x.into_iter().for_each(|v| {
-        let y = v * 2;
-    });
-}
-"#####,
-    )
-}
-
 #[test]
 fn doctest_convert_integer_literal() {
     check_doc_test(
@@ -1179,6 +1156,29 @@ impl Debug for S {
     )
 }
 
+#[test]
+fn doctest_replace_for_loop_with_for_each() {
+    check_doc_test(
+        "replace_for_loop_with_for_each",
+        r#####"
+fn main() {
+    let x = vec![1, 2, 3];
+    for$0 v in x {
+        let y = v * 2;
+    }
+}
+"#####,
+        r#####"
+fn main() {
+    let x = vec![1, 2, 3];
+    x.into_iter().for_each(|v| {
+        let y = v * 2;
+    });
+}
+"#####,
+    )
+}
+
 #[test]
 fn doctest_replace_if_let_with_match() {
     check_doc_test(