about summary refs log tree commit diff
path: root/crates/sourcegen
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-09-13 13:28:00 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-09-13 13:29:27 +0300
commit940b3afd00704dcfd5e6f57015ce2741da2b191c (patch)
tree3fc434446e640931fd138ea1be346b144e36177d /crates/sourcegen
parent80991356e15c372491312c5e47e56c7c22be9773 (diff)
downloadrust-940b3afd00704dcfd5e6f57015ce2741da2b191c.tar.gz
rust-940b3afd00704dcfd5e6f57015ce2741da2b191c.zip
internal: fix bugs in tests by simplifying code
Diffstat (limited to 'crates/sourcegen')
-rw-r--r--crates/sourcegen/src/lib.rs26
1 files changed, 12 insertions, 14 deletions
diff --git a/crates/sourcegen/src/lib.rs b/crates/sourcegen/src/lib.rs
index 7d179e00e70..d9226d0681c 100644
--- a/crates/sourcegen/src/lib.rs
+++ b/crates/sourcegen/src/lib.rs
@@ -55,22 +55,20 @@ impl CommentBlock {
         assert!(tag.starts_with(char::is_uppercase));
 
         let tag = format!("{}:", tag);
-        let mut res = Vec::new();
-        for mut block in CommentBlock::do_extract(text, true) {
-            let first = block.contents.remove(0);
-            if let Some(id) = first.strip_prefix(&tag) {
-                block.id = id.trim().to_string();
-                res.push(block);
-            }
-        }
-        res
+        // Would be nice if we had `.retain_mut` here!
+        CommentBlock::extract_untagged(text)
+            .into_iter()
+            .filter_map(|mut block| {
+                let first = block.contents.remove(0);
+                first.strip_prefix(&tag).map(|id| {
+                    block.id = id.trim().to_string();
+                    block
+                })
+            })
+            .collect()
     }
 
     pub fn extract_untagged(text: &str) -> Vec<CommentBlock> {
-        CommentBlock::do_extract(text, false)
-    }
-
-    fn do_extract(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<CommentBlock> {
         let mut res = Vec::new();
 
         let prefix = "// ";
@@ -79,7 +77,7 @@ impl CommentBlock {
         let dummy_block = CommentBlock { id: String::new(), line: 0, contents: Vec::new() };
         let mut block = dummy_block.clone();
         for (line_num, line) in lines.enumerate() {
-            if line == "//" && allow_blocks_with_empty_lines {
+            if line == "//" {
                 block.contents.push(String::new());
                 continue;
             }