From d93b70916981200e8a566bf511dd44a88863b010 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Thu, 12 Sep 2024 00:17:30 +0300 Subject: [PATCH] fix a whole bunch of clippy warnings --- src/annotation.rs | 3 +-- src/builder/builder.rs | 8 ++++---- src/builder/comment.rs | 2 +- src/builder/function.rs | 4 +++- src/builder/markdown.rs | 27 +++++++++------------------ src/builder/namespace.rs | 2 +- src/builder/shared.rs | 8 ++++---- src/builder/traits.rs | 4 +--- src/builder/tutorial.rs | 23 ++++++++--------------- src/html/mod.rs | 2 +- src/lookahead.rs | 4 ++-- src/url.rs | 2 +- 12 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/annotation.rs b/src/annotation.rs index a2035fb..36445ca 100644 --- a/src/annotation.rs +++ b/src/annotation.rs @@ -49,7 +49,7 @@ impl<'a> Annotations<'a> { pub fn next(&mut self) -> Option { self.annotations.iter() .skip(self.next_in_iter) - .skip_while(|a| { + .find(|a| { if a.value.is_some() { self.next_in_iter += 1; true @@ -58,7 +58,6 @@ impl<'a> Annotations<'a> { false } }) - .next() .inspect(|_| self.next_in_iter += 1) .map(|a| a.raw.clone()) } diff --git a/src/builder/builder.rs b/src/builder/builder.rs index d732c36..f6f5d5d 100644 --- a/src/builder/builder.rs +++ b/src/builder/builder.rs @@ -56,7 +56,7 @@ impl<'e> Builder<'e> { // transpile, minify, and copy JS for script in &self.config.scripts.js { fs::write( - &self.config.output_dir.join(&script.name), + self.config.output_dir.join(&script.name), minify_js(script.content.to_string())?, ).map_err(|e| format!("Unable to copy {}: {e}", script.name))?; } @@ -264,7 +264,7 @@ impl<'e> Builder<'e> { } fs::write( - &self.config.output_dir.join("functions.json"), + self.config.output_dir.join("functions.json"), serde_json::to_string( &self.root.nav().suboptions_titles(self.config.clone()) .into_iter() @@ -315,7 +315,7 @@ fn default_format(config: Arc) -> HashMap { ("project_version".into(), config.project.version.clone()), ( "project_repository".into(), - config.project.repository.clone().unwrap_or(String::new()), + config.project.repository.clone().unwrap_or_default(), ), ( "project_icon".into(), @@ -330,7 +330,7 @@ fn default_format(config: Arc) -> HashMap { .as_ref() .unwrap_or(&UrlPath::new()) ))) - .unwrap_or(String::new()), + .unwrap_or_default(), ), ( "output_url".into(), diff --git a/src/builder/comment.rs b/src/builder/comment.rs index 16e4895..bac6bc4 100644 --- a/src/builder/comment.rs +++ b/src/builder/comment.rs @@ -140,7 +140,7 @@ impl<'s> CommentLexer<'s> { let value = self .eat_until(|c| matches!(c, ']' | ',')) .map(|s| s.trim().to_owned()) - .unwrap_or(String::new()); + .unwrap_or_default(); attrs.insert(key, Some(value)); } diff --git a/src/builder/function.rs b/src/builder/function.rs index 5d2224c..e09b4e3 100644 --- a/src/builder/function.rs +++ b/src/builder/function.rs @@ -4,7 +4,9 @@ use crate::{html::Html, url::UrlPath}; use clang::Entity; use super::{ - builder::Builder, shared::{output_entity, output_function}, traits::{ASTEntry, BuildResult, EntityMethods, Entry, NavItem, OutputEntry} + builder::Builder, + shared::output_function, + traits::{ASTEntry, BuildResult, EntityMethods, Entry, NavItem, OutputEntry} }; pub struct Function<'e> { diff --git a/src/builder/markdown.rs b/src/builder/markdown.rs index 170a437..16829c7 100644 --- a/src/builder/markdown.rs +++ b/src/builder/markdown.rs @@ -45,7 +45,7 @@ impl Metadata { } } -fn parse_markdown_metadata<'a>(doc: &'a str) -> (&'a str, Option) { +fn parse_markdown_metadata(doc: &str) -> (&str, Option) { // if the document has no metadata just parse it as markdown if !doc.trim_start().starts_with("---") { return (doc, None); @@ -83,7 +83,7 @@ struct MDStream<'i, 'c, 'b, 'e, const SIZE: usize, F: Fn(UrlPath) -> Option Option, > MDStream<'i, 'c, 'b, 'e, SIZE, F> { @@ -124,9 +124,7 @@ impl< self.insert_para_stage = InsertP::Dont; return Some(Event::End(Tag::BlockQuote)); } - let Some(event) = self.iter.next() else { - return None; - }; + let event = self.iter.next()?; Some(match event { // Don't format emojis inside code blocks lol Event::Text(t) => if self.inside_code_block { @@ -205,7 +203,7 @@ impl< } } // replace spaces with single hyphens - buf = buf.trim() + buf = buf .split_whitespace() .collect::>() .join("-"); @@ -314,18 +312,11 @@ pub fn extract_metadata_from_md(text: &String, default_title: Option) -> Some(metadata) } // otherwise only return Some if a title was found + else if res.is_empty() { + default_title.map(Metadata::new_with_title) + } else { - if res.is_empty() { - if let Some(title) = default_title { - Some(Metadata::new_with_title(title)) - } - else { - None - } - } - else { - Some(Metadata::new_with_title(res)) - } + Some(Metadata::new_with_title(res)) } } @@ -341,7 +332,7 @@ pub fn output_tutorial<'e, T: Entry<'e>>( "content", fmt_markdown( builder, - &content, + content, Some(|url: UrlPath| { Some(url.remove_extension(".md")) }), diff --git a/src/builder/namespace.rs b/src/builder/namespace.rs index 6c0f1df..67a2d30 100644 --- a/src/builder/namespace.rs +++ b/src/builder/namespace.rs @@ -20,7 +20,7 @@ pub enum CppItemKind { } impl CppItemKind { - pub fn from<'e>(entity: &Entity<'e>) -> Option { + pub fn from(entity: &Entity) -> Option { match entity.get_kind() { EntityKind::StructDecl => Some(Self::Struct), EntityKind::ClassDecl | EntityKind::ClassTemplate | EntityKind::ClassTemplatePartialSpecialization => Some(Self::Class), diff --git a/src/builder/shared.rs b/src/builder/shared.rs index 8355a16..699b088 100644 --- a/src/builder/shared.rs +++ b/src/builder/shared.rs @@ -377,7 +377,7 @@ pub fn fmt_base_classes<'e, T: ASTEntry<'e>>(entry: &T, kw: &str, builder: &Buil ), base.get_type().map(|ty| fmt_type(&ty, builder)) ].into_iter().flatten().collect()).into()) - .intersperse_with(|| Html::span(&["space-after"], ",").into()) + .intersperse_with(|| Html::span(&["space-after"], ",")) .collect() ) .with_child(Html::span(&["space-before"], "{ ... }")) @@ -514,10 +514,10 @@ pub fn output_function<'e, T: ASTEntry<'e>>( ent } -fn fmt_autolinks_recursive<'a>( +fn fmt_autolinks_recursive( entity: &CppItem, config: Arc, - annotations: &mut Annotations<'a>, + annotations: &mut Annotations<'_>, prefix: &Option, ) { annotations.rewind(); @@ -590,5 +590,5 @@ pub fn fmt_emoji(text: &CowStr) -> String { } pub fn member_fun_link(entity: &Entity) -> Option { - Some(entity.get_name()?) + entity.get_name() } diff --git a/src/builder/traits.rs b/src/builder/traits.rs index afbe973..82ec571 100644 --- a/src/builder/traits.rs +++ b/src/builder/traits.rs @@ -208,8 +208,7 @@ impl<'e> EntityMethods<'e> for Entity<'e> { Some(self.extract_source_string()? .trim() .replace('\t', " ") - .replace('\r', "") - .replace('\n', "") + .replace(['\r', '\n'], "") .split(' ') .filter(|x| !x.is_empty()) .intersperse(" ") @@ -296,7 +295,6 @@ impl NavItem { NavItem::Dir(name, items, _, _) => items.iter() .flat_map(|i| i.suboptions_titles(config.clone())) - .into_iter() .map(|(t, count)| (format!("{}::{}", name, t), count)) .collect(), diff --git a/src/builder/tutorial.rs b/src/builder/tutorial.rs index a2d150a..20554e6 100644 --- a/src/builder/tutorial.rs +++ b/src/builder/tutorial.rs @@ -32,7 +32,7 @@ impl Tutorial { metadata: extract_metadata_from_md( &unparsed_content, path.remove_extension(".md").raw_file_name() - ).unwrap(), + ).unwrap_or_default(), unparsed_content, path, } @@ -103,11 +103,7 @@ impl TutorialFolder { let mut tutorials = HashMap::new(); let stripped_path = path - .strip_prefix( - &config - .input_dir - .join(&config.tutorials.as_ref().unwrap().dir), - ) + .strip_prefix(config.input_dir.join(&config.tutorials.as_ref().unwrap().dir)) .unwrap_or(path) .to_path_buf(); @@ -134,15 +130,12 @@ impl TutorialFolder { } { let stripped_path = path - .strip_prefix( - &config - .input_dir - .join(&config.tutorials.as_ref().unwrap().dir), - ) + .strip_prefix(config.input_dir.join(&config.tutorials.as_ref().unwrap().dir)) .unwrap_or(&path) .to_path_buf(); let Ok(url) = UrlPath::try_from(&stripped_path) else { continue; }; + println!("creating tutorial for {}", url); let tut = Tutorial::new(config.clone(), url); tutorials.insert(tut.name(), tut); } @@ -195,7 +188,7 @@ impl TutorialFolder { (Some(a), Some(b)) => a.cmp(&b), (Some(_), None) => Ordering::Less, (None, Some(_)) => Ordering::Greater, - (None, None) => a.0.cmp(&b.0), + (None, None) => a.0.cmp(b.0), } }); vec.into_iter().map(|(_, v)| v).collect() @@ -208,7 +201,7 @@ impl TutorialFolder { (Some(a), Some(b)) => a.cmp(&b), (Some(_), None) => Ordering::Less, (None, Some(_)) => Ordering::Greater, - (None, None) => a.0.cmp(&b.0), + (None, None) => a.0.cmp(b.0), } }); vec.into_iter().map(|(_, v)| v).collect() @@ -281,7 +274,7 @@ impl<'e> OutputEntry<'e> for TutorialFolder { output_tutorial( self, builder, - self.index.as_ref().map(|s| s.as_str()).unwrap_or(""), + self.index.as_deref().unwrap_or(""), fmt_section( "Pages", self.tutorials_sorted() @@ -290,7 +283,7 @@ impl<'e> OutputEntry<'e> for TutorialFolder { HtmlElement::new("ul") .with_child(HtmlElement::new("li").with_child( HtmlElement::new("a") - .with_text(&tut.name()) + .with_text(tut.name()) .with_attr( "href", tut.url().to_absolute(builder.config.clone()), diff --git a/src/html/mod.rs b/src/html/mod.rs index a91396d..dc3b24a 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -132,7 +132,7 @@ impl HtmlElement { } pub fn attr_mut(&mut self, attr: &str) -> &mut String { - self.attributes.entry(attr.into()).or_insert(String::new()) + self.attributes.entry(attr.into()).or_default() } pub fn with_attrs(mut self, attrs: &Vec<(String, String)>) -> Self { diff --git a/src/lookahead.rs b/src/lookahead.rs index c48dbf8..088ec66 100644 --- a/src/lookahead.rs +++ b/src/lookahead.rs @@ -11,8 +11,8 @@ pub struct CachedLookahead { impl CachedLookahead { pub fn new(mut iter: I) -> Self { let mut next_items: [Option; SIZE] = [(); SIZE].map(|_| None); - for i in 0..SIZE { - next_items[i] = iter.next(); + for item in next_items.iter_mut().take(SIZE) { + *item = iter.next(); } Self { iter, next_items } } diff --git a/src/url.rs b/src/url.rs index 0f3511d..5b3a3c2 100644 --- a/src/url.rs +++ b/src/url.rs @@ -155,7 +155,7 @@ impl UrlPath { } pub fn is_absolute(&self, config: Arc) -> bool { - self.starts_with(&config.output_url.as_ref().unwrap_or(&UrlPath::new())) + self.starts_with(config.output_url.as_ref().unwrap_or(&UrlPath::new())) } pub fn is_empty(&self) -> bool {