Skip to content

Commit

Permalink
Merge pull request #1214 from ehuss/fix-clippy
Browse files Browse the repository at this point in the history
Fix some clippy warnings.
  • Loading branch information
ehuss authored May 10, 2020
2 parents 07dfc4b + c44ef1b commit 8ee950e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 68 deletions.
8 changes: 3 additions & 5 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! [1]: ../index.html
#[allow(clippy::module_inception)]
mod book;
mod init;
mod summary;
Expand Down Expand Up @@ -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 {
Expand All @@ -143,7 +142,6 @@ impl MDBook {
/// // 2. Chapter 2
/// //
/// // etc.
/// # }
/// ```
pub fn iter(&self) -> BookItems<'_> {
self.book.iter()
Expand Down Expand Up @@ -405,7 +403,7 @@ fn interpret_custom_preprocessor(key: &str, table: &Value) -> Box<CmdPreprocesso
.map(ToString::to_string)
.unwrap_or_else(|| format!("mdbook-{}", key));

Box::new(CmdPreprocessor::new(key.to_string(), command.to_string()))
Box::new(CmdPreprocessor::new(key.to_string(), command))
}

fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
Expand All @@ -418,7 +416,7 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {

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
Expand Down
2 changes: 1 addition & 1 deletion src/book/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//! assert_eq!(got, Some(PathBuf::from("./themes")));
//! # Ok(())
//! # }
//! # fn main() { run().unwrap() }
//! # run().unwrap()
//! ```
#![deny(missing_docs)]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#![deny(missing_docs)]
#![deny(rust_2018_idioms)]
#![allow(clippy::comparison_chain)]

#[macro_use]
extern crate error_chain;
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/html_handlebars/helpers/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 8 additions & 15 deletions src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("<ol class=\"chapter\">")?;

Expand All @@ -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 {
Expand Down
51 changes: 24 additions & 27 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

let optional_key = format!("output.{}.optional", self.name);

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 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 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
);
}
_ => {}
}
Err(error).chain_err(|| "Unable to start the backend")?
}
Expand Down
3 changes: 0 additions & 3 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ pub fn write_file<P: AsRef<Path>>(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
Expand Down
24 changes: 10 additions & 14 deletions src/utils/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 8ee950e

Please sign in to comment.