about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAli Bektas <bektasali@protonmail.com>2023-08-09 23:34:30 +0200
committerAli Bektas <bektasali@protonmail.com>2023-09-10 23:15:05 +0200
commitb316bccc9716dc058120b4cf73c753973e2cd802 (patch)
tree6459128ec813e98e300cb92bc50ebcce1f9f1b59
parent9c6257138de5f093b2dd03893aa694b6165ea157 (diff)
downloadrust-b316bccc9716dc058120b4cf73c753973e2cd802.tar.gz
rust-b316bccc9716dc058120b4cf73c753973e2cd802.zip
replace for loops with sth more idiomatic
-rw-r--r--crates/ide-assists/src/handlers/convert_comment_block.rs9
-rw-r--r--crates/ide-assists/src/handlers/desugar_doc_comment.rs15
2 files changed, 10 insertions, 14 deletions
diff --git a/crates/ide-assists/src/handlers/convert_comment_block.rs b/crates/ide-assists/src/handlers/convert_comment_block.rs
index b6ad2dc0b65..4cbb30ec15c 100644
--- a/crates/ide-assists/src/handlers/convert_comment_block.rs
+++ b/crates/ide-assists/src/handlers/convert_comment_block.rs
@@ -86,11 +86,10 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
     // contents of each line comment when they're put into the block comment.
     let indentation = IndentLevel::from_token(comment.syntax());
 
-    let mut cms: Vec<String> = Vec::new();
-    for cm in comments {
-        let lcm = line_comment_text(indentation, cm)?;
-        cms.push(lcm);
-    }
+    let cms = comments
+        .into_iter()
+        .map(|c| line_comment_text(indentation, c))
+        .collect::<Option<Vec<String>>>()?;
 
     acc.add(
         AssistId("line_to_block", AssistKind::RefactorRewrite),
diff --git a/crates/ide-assists/src/handlers/desugar_doc_comment.rs b/crates/ide-assists/src/handlers/desugar_doc_comment.rs
index daa2c1df0cc..2f8cef1e4a7 100644
--- a/crates/ide-assists/src/handlers/desugar_doc_comment.rs
+++ b/crates/ide-assists/src/handlers/desugar_doc_comment.rs
@@ -50,7 +50,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
             (
                 TextRange::new(
                     comments[0].syntax().text_range().start(),
-                    comments.last().unwrap().syntax().text_range().end(),
+                    comments.last()?.syntax().text_range().end(),
                 ),
                 Either::Right(comments),
             )
@@ -66,14 +66,11 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
                 .map(|l| l.strip_prefix(&indentation).unwrap_or(l))
                 .join("\n")
         }
-        Either::Right(comments) => {
-            let mut cms: Vec<String> = Vec::new();
-            for cm in comments {
-                let lcm = line_comment_text(IndentLevel(0), cm)?;
-                cms.push(lcm);
-            }
-            cms.into_iter().join("\n")
-        }
+        Either::Right(comments) => comments
+            .into_iter()
+            .map(|cm| line_comment_text(IndentLevel(0), cm))
+            .collect::<Option<Vec<_>>>()?
+            .join("\n"),
     };
 
     acc.add(