From c44ef1b2f0dce84c09e6f6a8d54ab35b035c40e4 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 10 May 2020 08:29:50 -0700 Subject: [PATCH] Fix some clippy warnings. --- src/book/mod.rs | 8 ++-- src/book/summary.rs | 2 +- src/config.rs | 2 +- src/lib.rs | 1 + .../html_handlebars/helpers/navigation.rs | 3 +- src/renderer/html_handlebars/helpers/toc.rs | 23 ++++------ src/renderer/mod.rs | 45 +++++++++---------- src/utils/fs.rs | 3 -- src/utils/string.rs | 24 +++++----- 9 files changed, 46 insertions(+), 65 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 57ae2e3f..c2effe0f 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -5,6 +5,7 @@ //! //! [1]: ../index.html +#[allow(clippy::module_inception)] mod book; mod init; mod summary; @@ -126,8 +127,6 @@ impl MDBook { /// ```no_run /// # use mdbook::MDBook; /// # use mdbook::book::BookItem; - /// # #[allow(unused_variables)] - /// # fn main() { /// # let book = MDBook::load("mybook").unwrap(); /// for item in book.iter() { /// match *item { @@ -143,7 +142,6 @@ impl MDBook { /// // 2. Chapter 2 /// // /// // etc. - /// # } /// ``` pub fn iter(&self) -> BookItems<'_> { self.book.iter() @@ -405,7 +403,7 @@ fn interpret_custom_preprocessor(key: &str, table: &Value) -> Box Box { @@ -418,7 +416,7 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box { let command = table_dot_command.unwrap_or_else(|| format!("mdbook-{}", key)); - Box::new(CmdRenderer::new(key.to_string(), command.to_string())) + Box::new(CmdRenderer::new(key.to_string(), command)) } /// Check whether we should run a particular `Preprocessor` in combination diff --git a/src/book/summary.rs b/src/book/summary.rs index 1e130537..d9d6bcb5 100644 --- a/src/book/summary.rs +++ b/src/book/summary.rs @@ -281,7 +281,7 @@ impl<'a> SummaryParser<'a> { } else { Ok(Link { name, - location: PathBuf::from(href.to_string()), + location: PathBuf::from(href), number: None, nested_items: Vec::new(), }) diff --git a/src/config.rs b/src/config.rs index 36190ebf..0eae4667 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,7 +44,7 @@ //! assert_eq!(got, Some(PathBuf::from("./themes"))); //! # Ok(()) //! # } -//! # fn main() { run().unwrap() } +//! # run().unwrap() //! ``` #![deny(missing_docs)] diff --git a/src/lib.rs b/src/lib.rs index 67c3def5..c70babca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,7 @@ #![deny(missing_docs)] #![deny(rust_2018_idioms)] +#![allow(clippy::comparison_chain)] #[macro_use] extern crate error_chain; diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index 9d9ea76d..3ff99560 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -75,8 +75,7 @@ fn find_chapter( // Skip things like "spacer" chapter.contains_key("path") }) - .skip(1) - .next() + .nth(1) { Some(chapter) => return Ok(Some(chapter.clone())), None => return Ok(None), diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index df04cfdc..67fe4101 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -32,7 +32,7 @@ impl HelperDef for RenderToc { .evaluate(ctx, "@root/path")? .as_json() .as_str() - .ok_or(RenderError::new("Type error for `path`, string expected"))? + .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .replace("\"", ""); let current_section = rc @@ -46,17 +46,13 @@ impl HelperDef for RenderToc { .evaluate(ctx, "@root/fold_enable")? .as_json() .as_bool() - .ok_or(RenderError::new( - "Type error for `fold_enable`, bool expected", - ))?; + .ok_or_else(|| RenderError::new("Type error for `fold_enable`, bool expected"))?; let fold_level = rc .evaluate(ctx, "@root/fold_level")? .as_json() .as_u64() - .ok_or(RenderError::new( - "Type error for `fold_level`, u64 expected", - ))?; + .ok_or_else(|| RenderError::new("Type error for `fold_level`, u64 expected"))?; out.write("
    ")?; @@ -75,18 +71,15 @@ impl HelperDef for RenderToc { ("", 1) }; - let is_expanded = { - if !fold_enable { - // Disable fold. Expand all chapters. - true - } else if !section.is_empty() && current_section.starts_with(section) { - // The section is ancestor or the current section itself. + let is_expanded = + if !fold_enable || (!section.is_empty() && current_section.starts_with(section)) { + // Expand if folding is disabled, or if the section is an + // ancestor or the current section itself. true } else { // Levels that are larger than this would be folded. level - 1 < fold_level as usize - } - }; + }; if level > current_level { while level > current_level { diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 318e7da3..59fa8b9e 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -152,34 +152,31 @@ impl CmdRenderer { impl CmdRenderer { fn handle_render_command_error(&self, ctx: &RenderContext, error: io::Error) -> Result<()> { - match error.kind() { - ErrorKind::NotFound => { - // Look for "output.{self.name}.optional". - // If it exists and is true, treat this as a warning. - // Otherwise, fail the build. + if let ErrorKind::NotFound = error.kind() { + // Look for "output.{self.name}.optional". + // If it exists and is true, treat this as a warning. + // Otherwise, fail the build. - let optional_key = format!("output.{}.optional", self.name); + let optional_key = format!("output.{}.optional", self.name); - let is_optional = match ctx.config.get(&optional_key) { - Some(Value::Boolean(value)) => *value, - _ => false, - }; + let is_optional = match ctx.config.get(&optional_key) { + Some(Value::Boolean(value)) => *value, + _ => false, + }; - if is_optional { - warn!( - "The command `{}` for backend `{}` was not found, \ - but was marked as optional.", - self.cmd, self.name - ); - return Ok(()); - } else { - error!( - "The command `{}` wasn't found, is the `{}` backend installed?", - self.cmd, self.name - ); - } + if is_optional { + warn!( + "The command `{}` for backend `{}` was not found, \ + but was marked as optional.", + self.cmd, self.name + ); + return Ok(()); + } else { + error!( + "The command `{}` wasn't found, is the `{}` backend installed?", + self.cmd, self.name + ); } - _ => {} } Err(error).chain_err(|| "Unable to start the backend")? } diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 570e8428..d4c0cb5f 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -28,11 +28,8 @@ pub fn write_file>(build_dir: &Path, filename: P, content: &[u8]) /// ```rust /// # use std::path::Path; /// # use mdbook::utils::fs::path_to_root; -/// # -/// # fn main() { /// let path = Path::new("some/relative/path"); /// assert_eq!(path_to_root(path), "../../"); -/// # } /// ``` /// /// **note:** it's not very fool-proof, if you find a situation where diff --git a/src/utils/string.rs b/src/utils/string.rs index 4e1c6c96..2b3636d0 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -43,11 +43,9 @@ pub fn take_anchored_lines(s: &str, anchor: &str) -> String { } } } - } else { - if let Some(cap) = ANCHOR_START.captures(l) { - if &cap["anchor_name"] == anchor { - anchor_found = true; - } + } else if let Some(cap) = ANCHOR_START.captures(l) { + if &cap["anchor_name"] == anchor { + anchor_found = true; } } } @@ -96,16 +94,14 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { } } } - } else { - if let Some(cap) = ANCHOR_START.captures(l) { - if &cap["anchor_name"] == anchor { - within_anchored_section = true; - } - } else if !ANCHOR_END.is_match(l) { - output.push_str("# "); - output.push_str(l); - output.push_str("\n"); + } else if let Some(cap) = ANCHOR_START.captures(l) { + if &cap["anchor_name"] == anchor { + within_anchored_section = true; } + } else if !ANCHOR_END.is_match(l) { + output.push_str("# "); + output.push_str(l); + output.push_str("\n"); } }