From 9c65cd66152786c840eb73dc1dffe87d1b7f67e2 Mon Sep 17 00:00:00 2001 From: juanperias Date: Thu, 24 Jul 2025 12:08:33 -0400 Subject: [PATCH 1/2] refactor: clippy errors --- packages/tools/src/bin/cleanup_blockquotes.rs | 10 +- packages/tools/src/bin/concat_chapters.rs | 2 +- packages/tools/src/bin/lfp.rs | 8 +- packages/tools/src/bin/link2print.rs | 107 +++++++----------- packages/tools/src/bin/release_listings.rs | 10 +- packages/tools/src/bin/remove_hidden_lines.rs | 6 +- packages/tools/src/bin/remove_links.rs | 4 +- packages/tools/src/bin/remove_markup.rs | 13 ++- 8 files changed, 70 insertions(+), 90 deletions(-) diff --git a/packages/tools/src/bin/cleanup_blockquotes.rs b/packages/tools/src/bin/cleanup_blockquotes.rs index 41c31b65..15af6ea2 100644 --- a/packages/tools/src/bin/cleanup_blockquotes.rs +++ b/packages/tools/src/bin/cleanup_blockquotes.rs @@ -20,12 +20,12 @@ fn main() { buffer }; - let fixed = cleanup_blockquotes(input); + let fixed = cleanup_blockquotes(&input); print!("{fixed}"); } -fn cleanup_blockquotes(input: String) -> String { - let normal_start = EXTRA_SPACE.replace_all(&input, ">"); +fn cleanup_blockquotes(input: &str) -> String { + let normal_start = EXTRA_SPACE.replace_all(input, ">"); let sans_empty_leading = EMPTY_LEADING.replace_all(&normal_start, "\n\n"); sans_empty_leading.to_string() } @@ -42,7 +42,7 @@ mod tests { #[test] fn extra_space() { let input = " > Hello".to_string(); - let actual = cleanup_blockquotes(input); + let actual = cleanup_blockquotes(&input); assert_eq!(actual, "> Hello"); } @@ -110,7 +110,7 @@ here, but `map` is more idiomatic.) In the body of the function we supply to a `String`. When all is said and done, we have an `Option`. "#.to_string(); - let actual = cleanup_blockquotes(input); + let actual = cleanup_blockquotes(&input); assert_eq!( actual, r#" diff --git a/packages/tools/src/bin/concat_chapters.rs b/packages/tools/src/bin/concat_chapters.rs index 127e93a8..bf8b70ac 100644 --- a/packages/tools/src/bin/concat_chapters.rs +++ b/packages/tools/src/bin/concat_chapters.rs @@ -48,7 +48,7 @@ fn match_files( ) -> Vec<(PathBuf, PathBuf)> { read_dir(source_dir) .expect("Unable to read source directory") - .filter_map(|maybe_entry| maybe_entry.ok()) + .filter_map(Result::ok) .filter_map(|entry| { let source_filename = entry.file_name(); let source_filename = diff --git a/packages/tools/src/bin/lfp.rs b/packages/tools/src/bin/lfp.rs index d4ab62d5..e7440a38 100644 --- a/packages/tools/src/bin/lfp.rs +++ b/packages/tools/src/bin/lfp.rs @@ -35,10 +35,10 @@ fn main() { path.display(), line_num, line - ) + ); } LintingError::UnableToOpenFile => { - eprintln!("Unable to open {}.", path.display()) + eprintln!("Unable to open {}.", path.display()); } } } @@ -93,13 +93,13 @@ where Ok(()) } }) - .filter(|result| result.is_err()) + .filter(Result::is_err) .map(|result| result.unwrap_err()) .collect() } fn is_file_of_interest(path: &path::Path) -> bool { - path.extension().map_or(false, |ext| ext == "md") + path.extension().is_some_and(|ext| ext == "md") } fn is_line_of_interest(line: &str) -> bool { diff --git a/packages/tools/src/bin/link2print.rs b/packages/tools/src/bin/link2print.rs index 46244a2d..b834010d 100644 --- a/packages/tools/src/bin/link2print.rs +++ b/packages/tools/src/bin/link2print.rs @@ -8,7 +8,7 @@ use std::io::Read; use regex::{Captures, Regex}; fn main() { - write_md(parse_links(parse_references(read_md()))); + write_md(&parse_links(parse_references(&read_md()))); } fn read_md() -> String { @@ -19,32 +19,34 @@ fn read_md() -> String { } } -fn write_md(output: String) { +fn write_md(output: &str) { print!("{output}"); } -fn parse_references(buffer: String) -> (String, HashMap) { +fn parse_references(buffer: &str) -> (String, HashMap) { let mut ref_map = HashMap::new(); // FIXME: currently doesn't handle "title" in following line. - let re = Regex::new(r###"(?m)\n?^ {0,3}\[([^]]+)\]:[[:blank:]]*(.*)$"###) + let re = Regex::new(r"(?m)\n?^ {0,3}\[([^]]+)\]:[[:blank:]]*(.*)$") .unwrap(); let output = re - .replace_all(&buffer, |caps: &Captures<'_>| { + .replace_all(buffer, |caps: &Captures<'_>| { let key_def = caps.get(1).unwrap().as_str(); let key = key_def.to_uppercase(); let val = caps.get(2).unwrap().as_str().to_string(); - if ref_map.insert(key, val).is_some() { - panic!("unexpected page had duplicate reference for {key_def}",); - } - "".to_string() + + assert!(ref_map.insert(key, val).is_none(), "unexpected page had duplicate reference for {key_def}"); + + + String::new() }) .to_string(); (output, ref_map) } +#[allow(clippy::single_match_else)] fn parse_links((buffer, ref_map): (String, HashMap)) -> String { // FIXME: check which punctuation is allowed by spec. - let re = Regex::new(r###"(?:(?P
(?:```(?:[^`]|`[^`])*`?\n```\n)|(?:[^\[]`[^`\n]+[\n]?[^`\n]*`))|(?:\[(?P[^]]+)\](?:(?:\([[:blank:]]*(?P[^")]*[^ ])(?:[[:blank:]]*"[^"]*")?\))|(?:\[(?P[^]]*)\]))?))"###).expect("could not create regex");
+    let re = Regex::new(r#"(?:(?P
(?:```(?:[^`]|`[^`])*`?\n```\n)|(?:[^\[]`[^`\n]+[\n]?[^`\n]*`))|(?:\[(?P[^]]+)\](?:(?:\([[:blank:]]*(?P[^")]*[^ ])(?:[[:blank:]]*"[^"]*")?\))|(?:\[(?P[^]]*)\]))?))"#).expect("could not create regex");
     let output = re.replace_all(&buffer, |caps: &Captures<'_>| {
         match caps.name("pre") {
             Some(pre_section) => pre_section.as_str().to_string(),
@@ -80,29 +82,25 @@ fn parse_links((buffer, ref_map): (String, HashMap)) -> String {
 
 #[cfg(test)]
 mod tests {
-    fn parse(source: String) -> String {
+    fn parse(source: &str) -> String {
         super::parse_links(super::parse_references(source))
     }
 
     #[test]
     fn parses_inline_link() {
         let source =
-            r"This is a [link](http://google.com) that should be expanded"
-                .to_string();
+            r"This is a [link](http://google.com) that should be expanded";
         let target =
-            r"This is a link at *http://google.com* that should be expanded"
-                .to_string();
+            r"This is a link at *http://google.com* that should be expanded";
         assert_eq!(parse(source), target);
     }
 
     #[test]
     fn parses_multiline_links() {
         let source = r"This is a [link](http://google.com) that
-should appear expanded. Another [location](/here/) and [another](http://gogogo)"
-            .to_string();
+should appear expanded. Another [location](/here/) and [another](http://gogogo)";
         let target = r"This is a link at *http://google.com* that
-should appear expanded. Another location at */here/* and another at *http://gogogo*"
-            .to_string();
+should appear expanded. Another location at */here/* and another at *http://gogogo*";
         assert_eq!(parse(source), target);
     }
 
@@ -110,27 +108,23 @@ should appear expanded. Another location at */here/* and another at *http://gogo
     fn parses_reference() {
         let source = r"This is a [link][theref].
 [theref]: http://example.com/foo
-more text"
-            .to_string();
+more text";
         let target = r"This is a link at *http://example.com/foo*.
-more text"
-            .to_string();
+more text";
         assert_eq!(parse(source), target);
     }
 
     #[test]
     fn parses_implicit_link() {
         let source = r"This is an [implicit][] link.
-[implicit]: /The Link/"
-            .to_string();
+[implicit]: /The Link/";
         let target = r"This is an implicit at */The Link/* link.".to_string();
         assert_eq!(parse(source), target);
     }
     #[test]
     fn parses_refs_with_one_space_indentation() {
         let source = r"This is a [link][ref]
- [ref]: The link"
-            .to_string();
+ [ref]: The link";
         let target = r"This is a link at *The link*".to_string();
         assert_eq!(parse(source), target);
     }
@@ -138,8 +132,7 @@ more text"
     #[test]
     fn parses_refs_with_two_space_indentation() {
         let source = r"This is a [link][ref]
-  [ref]: The link"
-            .to_string();
+  [ref]: The link";
         let target = r"This is a link at *The link*".to_string();
         assert_eq!(parse(source), target);
     }
@@ -147,8 +140,7 @@ more text"
     #[test]
     fn parses_refs_with_three_space_indentation() {
         let source = r"This is a [link][ref]
-   [ref]: The link"
-            .to_string();
+   [ref]: The link";
         let target = r"This is a link at *The link*".to_string();
         assert_eq!(parse(source), target);
     }
@@ -157,8 +149,7 @@ more text"
     #[should_panic]
     fn rejects_refs_with_four_space_indentation() {
         let source = r"This is a [link][ref]
-    [ref]: The link"
-            .to_string();
+    [ref]: The link";
         let target = r"This is a link at *The link*".to_string();
         assert_eq!(parse(source), target);
     }
@@ -166,8 +157,7 @@ more text"
     #[test]
     fn ignores_optional_inline_title() {
         let source =
-            r###"This is a titled [link](http://example.com "My title")."###
-                .to_string();
+            r###"This is a titled [link](http://example.com "My title")."###;
         let target =
             r"This is a titled link at *http://example.com*.".to_string();
         assert_eq!(parse(source), target);
@@ -176,20 +166,20 @@ more text"
     #[test]
     fn parses_title_with_puctuation() {
         let source =
-            r###"[link](http://example.com "It's Title")"###.to_string();
+            r###"[link](http://example.com "It's Title")"###;
         let target = r"link at *http://example.com*".to_string();
         assert_eq!(parse(source), target);
     }
 
     #[test]
     fn parses_name_with_punctuation() {
-        let source = r###"[I'm here](there)"###.to_string();
+        let source = r###"[I'm here](there)"###;
         let target = r###"I'm here at *there*"###.to_string();
         assert_eq!(parse(source), target);
     }
     #[test]
     fn parses_name_with_utf8() {
-        let source = r###"[user’s forum](the user’s forum)"###.to_string();
+        let source = r###"[user’s forum](the user’s forum)"###;
         let target = r###"user’s forum at *the user’s forum*"###.to_string();
         assert_eq!(parse(source), target);
     }
@@ -197,8 +187,7 @@ more text"
     #[test]
     fn parses_reference_with_punctuation() {
         let source = r###"[link][the ref-ref]
-[the ref-ref]:http://example.com/ref-ref"###
-            .to_string();
+[the ref-ref]:http://example.com/ref-ref"###;
         let target = r###"link at *http://example.com/ref-ref*"###.to_string();
         assert_eq!(parse(source), target);
     }
@@ -206,16 +195,14 @@ more text"
     #[test]
     fn parses_reference_case_insensitively() {
         let source = r"[link][Ref]
-[ref]: The reference"
-            .to_string();
+[ref]: The reference";
         let target = r"link at *The reference*".to_string();
         assert_eq!(parse(source), target);
     }
     #[test]
     fn parses_link_as_reference_when_reference_is_empty() {
         let source = r"[link as reference][]
-[link as reference]: the actual reference"
-            .to_string();
+[link as reference]: the actual reference";
         let target = r"link as reference at *the actual reference*".to_string();
         assert_eq!(parse(source), target);
     }
@@ -223,8 +210,7 @@ more text"
     #[test]
     fn does_not_parse_link_without_reference_as_reference() {
         let source = r"[link] is alone
-[link]: The contents"
-            .to_string();
+[link]: The contents";
         let target = r"[link] is alone".to_string();
         assert_eq!(parse(source), target);
     }
@@ -233,8 +219,7 @@ more text"
     #[ignore]
     fn parses_link_without_reference_as_reference_with_asterisks() {
         let source = r"*[link]* is alone
-[link]: The contents"
-            .to_string();
+[link]: The contents";
         let target = r"*link* at *The contents* is alone".to_string();
         assert_eq!(parse(source), target);
     }
@@ -247,23 +232,21 @@ version = "0.1.0"
 
 [dependencies]
 ```
-"###
-        .to_string();
+"###;
         let target = source.clone();
         assert_eq!(parse(source), target);
     }
 
     #[test]
     fn ignores_links_in_quoted_sections() {
-        let source = r###"do not change `[package]`."###.to_string();
+        let source = r###"do not change `[package]`."###;
         let target = source.clone();
         assert_eq!(parse(source), target);
     }
     #[test]
     fn ignores_links_in_quoted_sections_containing_newlines() {
         let source = r"do not change `this [package]
-is still here` [link](ref)"
-            .to_string();
+is still here` [link](ref)";
         let target = r"do not change `this [package]
 is still here` link at *ref*"
             .to_string();
@@ -282,8 +265,7 @@ version = "0.1.0"
 Another [link][]
 more text
 [link]: http://gohere
-"###
-        .to_string();
+"###;
         let target = r###"```toml
 [package]
 name = "hello_cargo"
@@ -311,14 +293,13 @@ src/main.rs:23:21: 23:35 note:    found type `&_`
 error: aborting due to previous error
 Could not compile `guessing_game`.
 ```
-"###
-            .to_string();
+"###;
         let target = source.clone();
         assert_eq!(parse(source), target);
     }
     #[test]
     fn ignores_short_quotes() {
-        let source = r"to `1` at index `[0]` i".to_string();
+        let source = r"to `1` at index `[0]` i";
         let target = source.clone();
         assert_eq!(parse(source), target);
     }
@@ -338,7 +319,7 @@ note: `Point` cannot be formatted with the default formatter; try using `:?` ins
 note: required by `std::fmt::Display::fmt`
 ```
 `here` is another [link](the ref)
-"###.to_string();
+"###;
         let target = r###"```bash
 $ cargo run
    Compiling points v0.1.0 (file:///projects/points)
@@ -353,7 +334,7 @@ note: `Point` cannot be formatted with the default formatter; try using `:?` ins
 note: required by `std::fmt::Display::fmt`
 ```
 `here` is another link at *the ref*
-"###.to_string();
+"###;
         assert_eq!(parse(source), target);
     }
     #[test]
@@ -378,8 +359,7 @@ Some text to show that the reference links can follow later.
 
 [arbitrary case-insensitive reference text]: https://www.mozilla.org
 [1]: http://slashdot.org
-[link text itself]: http://www.reddit.com"###
-            .to_string();
+[link text itself]: http://www.reddit.com"###;
 
         let target = r###"I'm an inline-style link at *https://www.google.com*
 
@@ -398,8 +378,7 @@ http://www.example.com or  and sometimes
 example.com (but not on Github, for example).
 
 Some text to show that the reference links can follow later.
-"###
-            .to_string();
+"###;
         assert_eq!(parse(source), target);
     }
 }
diff --git a/packages/tools/src/bin/release_listings.rs b/packages/tools/src/bin/release_listings.rs
index 468c4637..4b8621b2 100644
--- a/packages/tools/src/bin/release_listings.rs
+++ b/packages/tools/src/bin/release_listings.rs
@@ -83,7 +83,7 @@ fn main() -> Result<(), Box> {
             })?;
 
             // Copy all the cleaned files in the listing to the tmp directory
-            copy_cleaned_listing_files(listing_path, output_listing_dir)?;
+            copy_cleaned_listing_files(&listing_path, &output_listing_dir)?;
         }
     }
 
@@ -109,10 +109,10 @@ fn main() -> Result<(), Box> {
 // - anchor comments or snip comments
 // - empty `main` functions in `lib.rs` files used to trick rustdoc
 fn copy_cleaned_listing_files(
-    from: PathBuf,
-    to: PathBuf,
+    from: &Path,
+    to: &Path,
 ) -> Result<(), Box> {
-    for item in fs::read_dir(&from).map_err(|e| {
+    for item in fs::read_dir(from).map_err(|e| {
         format!("Could not read_dir on '{}': {e}", from.display())
     })? {
         let item = item.map_err(|e| {
@@ -133,7 +133,7 @@ fn copy_cleaned_listing_files(
                         output_item.display()
                     )
                 })?;
-                copy_cleaned_listing_files(item_path, output_item)?;
+                copy_cleaned_listing_files(&item_path, &output_item)?;
             }
         } else {
             // Don't copy output files or files that tell update-rustc.sh not to format
diff --git a/packages/tools/src/bin/remove_hidden_lines.rs b/packages/tools/src/bin/remove_hidden_lines.rs
index 934b64eb..2c1d4743 100644
--- a/packages/tools/src/bin/remove_hidden_lines.rs
+++ b/packages/tools/src/bin/remove_hidden_lines.rs
@@ -2,7 +2,7 @@ use std::io;
 use std::io::prelude::*;
 
 fn main() {
-    write_md(remove_hidden_lines(&read_md()));
+    write_md(&remove_hidden_lines(&read_md()));
 }
 
 fn read_md() -> String {
@@ -13,7 +13,7 @@ fn read_md() -> String {
     }
 }
 
-fn write_md(output: String) {
+fn write_md(output: &str) {
     print!("{output}");
 }
 
@@ -27,7 +27,7 @@ fn remove_hidden_lines(input: &str) -> String {
         }
 
         if !within_codeblock || (!line.starts_with("# ") && line != "#") {
-            resulting_lines.push(line)
+            resulting_lines.push(line);
         }
     }
 
diff --git a/packages/tools/src/bin/remove_links.rs b/packages/tools/src/bin/remove_links.rs
index ebb46e2c..bda66955 100644
--- a/packages/tools/src/bin/remove_links.rs
+++ b/packages/tools/src/bin/remove_links.rs
@@ -28,11 +28,11 @@ fn main() {
     // Search for the references we need to delete.
     let ref_regex = Regex::new(r"(?m)^\[([^\]]+)\]:\s.*\n").unwrap();
     let out = ref_regex.replace_all(&first_pass, |caps: &Captures<'_>| {
-        let capture = caps.get(1).unwrap().to_owned();
+        let capture = caps.get(1).unwrap();
 
         // Check if we've marked this reference for deletion ...
         if refs.contains(capture.as_str()) {
-            return "".to_string();
+            return String::new();
         }
 
         // ... else we put back everything we captured.
diff --git a/packages/tools/src/bin/remove_markup.rs b/packages/tools/src/bin/remove_markup.rs
index 6ec0fdfb..3188e863 100644
--- a/packages/tools/src/bin/remove_markup.rs
+++ b/packages/tools/src/bin/remove_markup.rs
@@ -4,7 +4,7 @@ use std::io::Read;
 use regex::{Captures, Regex};
 
 fn main() {
-    write_md(remove_markup(read_md()));
+    write_md(&remove_markup(&read_md()));
 }
 
 fn read_md() -> String {
@@ -15,22 +15,23 @@ fn read_md() -> String {
     }
 }
 
-fn write_md(output: String) {
+fn write_md(output: &str) {
     print!("{output}");
 }
 
-fn remove_markup(input: String) -> String {
+fn remove_markup(input: &str) -> String {
     let filename_regex =
         Regex::new(r#"\A(.*)\z"#).unwrap();
     // Captions sometimes take up multiple lines.
     let caption_start_regex =
         Regex::new(r#"\A(.*)\z"#).unwrap();
-    let caption_end_regex = Regex::new(r#"(.*)\z"#).unwrap();
+    let caption_end_regex = Regex::new(r"(.*)\z").unwrap();
     let regexen = [filename_regex, caption_start_regex, caption_end_regex];
 
+    #[allow(clippy::unnecessary_filter_map)]
     let lines: Vec<_> = input
-        .lines()
-        .flat_map(|line| {
+        .lines() 
+        .filter_map(|line| {
             // Remove our syntax highlighting and rustdoc markers.
             if line.starts_with("```") {
                 Some(String::from("```"))

From b4ae7d38111d31a76b34bb98a4f2f4f93900e72d Mon Sep 17 00:00:00 2001
From: juanperias 
Date: Thu, 24 Jul 2025 12:12:11 -0400
Subject: [PATCH 2/2] chore: fmt

---
 packages/tools/src/bin/link2print.rs    | 13 +++++++------
 packages/tools/src/bin/remove_markup.rs |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/packages/tools/src/bin/link2print.rs b/packages/tools/src/bin/link2print.rs
index b834010d..26bff91e 100644
--- a/packages/tools/src/bin/link2print.rs
+++ b/packages/tools/src/bin/link2print.rs
@@ -26,16 +26,18 @@ fn write_md(output: &str) {
 fn parse_references(buffer: &str) -> (String, HashMap) {
     let mut ref_map = HashMap::new();
     // FIXME: currently doesn't handle "title" in following line.
-    let re = Regex::new(r"(?m)\n?^ {0,3}\[([^]]+)\]:[[:blank:]]*(.*)$")
-        .unwrap();
+    let re =
+        Regex::new(r"(?m)\n?^ {0,3}\[([^]]+)\]:[[:blank:]]*(.*)$").unwrap();
     let output = re
         .replace_all(buffer, |caps: &Captures<'_>| {
             let key_def = caps.get(1).unwrap().as_str();
             let key = key_def.to_uppercase();
             let val = caps.get(2).unwrap().as_str().to_string();
 
-            assert!(ref_map.insert(key, val).is_none(), "unexpected page had duplicate reference for {key_def}");
-
+            assert!(
+                ref_map.insert(key, val).is_none(),
+                "unexpected page had duplicate reference for {key_def}"
+            );
 
             String::new()
         })
@@ -165,8 +167,7 @@ more text";
 
     #[test]
     fn parses_title_with_puctuation() {
-        let source =
-            r###"[link](http://example.com "It's Title")"###;
+        let source = r###"[link](http://example.com "It's Title")"###;
         let target = r"link at *http://example.com*".to_string();
         assert_eq!(parse(source), target);
     }
diff --git a/packages/tools/src/bin/remove_markup.rs b/packages/tools/src/bin/remove_markup.rs
index 3188e863..f3b15115 100644
--- a/packages/tools/src/bin/remove_markup.rs
+++ b/packages/tools/src/bin/remove_markup.rs
@@ -30,7 +30,7 @@ fn remove_markup(input: &str) -> String {
 
     #[allow(clippy::unnecessary_filter_map)]
     let lines: Vec<_> = input
-        .lines() 
+        .lines()
         .filter_map(|line| {
             // Remove our syntax highlighting and rustdoc markers.
             if line.starts_with("```") {