Skip to content

Commit

Permalink
fix a whole bunch of clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Sep 11, 2024
1 parent 463dadc commit d93b709
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a> Annotations<'a> {
pub fn next(&mut self) -> Option<String> {
self.annotations.iter()
.skip(self.next_in_iter)
.skip_while(|a| {
.find(|a| {
if a.value.is_some() {
self.next_in_iter += 1;
true
Expand All @@ -58,7 +58,6 @@ impl<'a> Annotations<'a> {
false
}
})
.next()
.inspect(|_| self.next_in_iter += 1)
.map(|a| a.raw.clone())
}
Expand Down
8 changes: 4 additions & 4 deletions src/builder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -315,7 +315,7 @@ fn default_format(config: Arc<Config>) -> HashMap<String, String> {
("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(),
Expand All @@ -330,7 +330,7 @@ fn default_format(config: Arc<Config>) -> HashMap<String, String> {
.as_ref()
.unwrap_or(&UrlPath::new())
)))
.unwrap_or(String::new()),
.unwrap_or_default(),
),
(
"output_url".into(),
Expand Down
2 changes: 1 addition & 1 deletion src/builder/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 3 additions & 1 deletion src/builder/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
27 changes: 9 additions & 18 deletions src/builder/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Metadata {
}
}

fn parse_markdown_metadata<'a>(doc: &'a str) -> (&'a str, Option<Metadata>) {
fn parse_markdown_metadata(doc: &str) -> (&str, Option<Metadata>) {
// if the document has no metadata just parse it as markdown
if !doc.trim_start().starts_with("---") {
return (doc, None);
Expand Down Expand Up @@ -83,7 +83,7 @@ struct MDStream<'i, 'c, 'b, 'e, const SIZE: usize, F: Fn(UrlPath) -> Option<UrlP
}

impl<
'i, 'c, 'b, 'e, 'm,
'i, 'c, 'b, 'e,
const SIZE: usize,
F: Fn(UrlPath) -> Option<UrlPath>,
> MDStream<'i, 'c, 'b, 'e, SIZE, F> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -205,7 +203,7 @@ impl<
}
}
// replace spaces with single hyphens
buf = buf.trim()
buf = buf
.split_whitespace()
.collect::<Vec<_>>()
.join("-");
Expand Down Expand Up @@ -314,18 +312,11 @@ pub fn extract_metadata_from_md(text: &String, default_title: Option<String>) ->
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))
}
}

Expand All @@ -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"))
}),
Expand Down
2 changes: 1 addition & 1 deletion src/builder/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum CppItemKind {
}

impl CppItemKind {
pub fn from<'e>(entity: &Entity<'e>) -> Option<Self> {
pub fn from(entity: &Entity) -> Option<Self> {
match entity.get_kind() {
EntityKind::StructDecl => Some(Self::Struct),
EntityKind::ClassDecl | EntityKind::ClassTemplate | EntityKind::ClassTemplatePartialSpecialization => Some(Self::Class),
Expand Down
8 changes: 4 additions & 4 deletions src/builder/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"], "{ ... }"))
Expand Down Expand Up @@ -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<Config>,
annotations: &mut Annotations<'a>,
annotations: &mut Annotations<'_>,
prefix: &Option<char>,
) {
annotations.rewind();
Expand Down Expand Up @@ -590,5 +590,5 @@ pub fn fmt_emoji(text: &CowStr) -> String {
}

pub fn member_fun_link(entity: &Entity) -> Option<String> {
Some(entity.get_name()?)
entity.get_name()
}
4 changes: 1 addition & 3 deletions src/builder/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
Expand Down Expand Up @@ -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(),

Expand Down
23 changes: 8 additions & 15 deletions src/builder/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/lookahead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct CachedLookahead<I: Iterator, const SIZE: usize> {
impl<I: Iterator, const SIZE: usize> CachedLookahead<I, SIZE> {
pub fn new(mut iter: I) -> Self {
let mut next_items: [Option<I::Item>; 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 }
}
Expand Down
2 changes: 1 addition & 1 deletion src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl UrlPath {
}

pub fn is_absolute(&self, config: Arc<Config>) -> 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 {
Expand Down

0 comments on commit d93b709

Please sign in to comment.