Skip to content

Fix Clippy lints #1832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> {
renderers
}

const DEFAULT_PREPROCESSORS: &[&'static str] = &["links", "index"];
const DEFAULT_PREPROCESSORS: &[&str] = &["links", "index"];

fn is_default_preprocessor(pre: &dyn Preprocessor) -> bool {
let name = pre.name();
Expand Down Expand Up @@ -756,10 +756,9 @@ mod tests {

let preprocessors = determine_preprocessors(&cfg).unwrap();

assert!(preprocessors
assert!(!preprocessors
.iter()
.find(|preprocessor| preprocessor.name() == "random")
.is_none());
.any(|preprocessor| preprocessor.name() == "random"));
}

#[test]
Expand All @@ -776,10 +775,9 @@ mod tests {

let preprocessors = determine_preprocessors(&cfg).unwrap();

assert!(preprocessors
assert!(!preprocessors
.iter()
.find(|preprocessor| preprocessor.name() == "links")
.is_none());
.any(|preprocessor| preprocessor.name() == "links"));
}

#[test]
Expand Down
5 changes: 1 addition & 4 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,5 @@ fn confirm() -> bool {
io::stdout().flush().unwrap();
let mut s = String::new();
io::stdin().read_line(&mut s).ok();
match &*s.trim() {
"Y" | "y" | "yes" | "Yes" => true,
_ => false,
}
matches!(&*s.trim(), "Y" | "y" | "yes" | "Yes")
}
34 changes: 13 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ impl Config {
let value = Value::try_from(value)
.with_context(|| "Unable to represent the item as a JSON Value")?;

if index.starts_with("book.") {
self.book.update_value(&index[5..], value);
} else if index.starts_with("build.") {
self.build.update_value(&index[6..], value);
if let Some(key) = index.strip_prefix("book.") {
self.book.update_value(key, value);
} else if let Some(key) = index.strip_prefix("build.") {
self.build.update_value(key, value);
} else {
self.rest.insert(index, value);
}
Expand Down Expand Up @@ -371,15 +371,8 @@ impl Serialize for Config {
}

fn parse_env(key: &str) -> Option<String> {
const PREFIX: &str = "MDBOOK_";

if key.starts_with(PREFIX) {
let key = &key[PREFIX.len()..];

Some(key.to_lowercase().replace("__", ".").replace("_", "-"))
} else {
None
}
key.strip_prefix("MDBOOK_")
.map(|key| key.to_lowercase().replace("__", ".").replace('_', "-"))
}

fn is_legacy_format(table: &Value) -> bool {
Expand Down Expand Up @@ -828,7 +821,7 @@ mod tests {
"#;

let got = Config::from_str(src).unwrap();
assert_eq!(got.html_config().unwrap().playground.runnable, false);
assert!(!got.html_config().unwrap().playground.runnable);
}

#[test]
Expand Down Expand Up @@ -1037,7 +1030,7 @@ mod tests {
fn encode_env_var(key: &str) -> String {
format!(
"MDBOOK_{}",
key.to_uppercase().replace('.', "__").replace("-", "_")
key.to_uppercase().replace('.', "__").replace('-', "_")
)
}

Expand All @@ -1061,11 +1054,10 @@ mod tests {
}

#[test]
#[allow(clippy::approx_constant)]
fn update_config_using_env_var_and_complex_value() {
let mut cfg = Config::default();
let key = "foo-bar.baz";
let value = json!({"array": [1, 2, 3], "number": 3.14});
let value = json!({"array": [1, 2, 3], "number": 13.37});
let value_str = serde_json::to_string(&value).unwrap();

assert!(cfg.get(key).is_none());
Expand Down Expand Up @@ -1184,15 +1176,15 @@ mod tests {
"#;
let got = Config::from_str(src).unwrap();
let html_config = got.html_config().unwrap();
assert_eq!(html_config.print.enable, false);
assert_eq!(html_config.print.page_break, true);
assert!(!html_config.print.enable);
assert!(html_config.print.page_break);
let src = r#"
[output.html.print]
page-break = false
"#;
let got = Config::from_str(src).unwrap();
let html_config = got.html_config().unwrap();
assert_eq!(html_config.print.enable, true);
assert_eq!(html_config.print.page_break, false);
assert!(html_config.print.enable);
assert!(!html_config.print.page_break);
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@

#![deny(missing_docs)]
#![deny(rust_2018_idioms)]
#![allow(clippy::comparison_chain)]

#[macro_use]
extern crate lazy_static;
Expand Down
1 change: 1 addition & 0 deletions src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ enum RangeOrAnchor {
}

// A range of lines specified with some include directive.
#[allow(clippy::enum_variant_names)] // The prefix can't be removed, and is meant to mirror the contained type
#[derive(PartialEq, Debug, Clone)]
enum LineRange {
Range(Range<usize>),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ fn fix_code_blocks(html: &str) -> String {
FIX_CODE_BLOCKS
.replace_all(html, |caps: &Captures<'_>| {
let before = &caps[1];
let classes = &caps[2].replace(",", " ");
let classes = &caps[2].replace(',', " ");
let after = &caps[3];

format!(
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/html_handlebars/helpers/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn find_chapter(
.as_json()
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
.replace('\"', "");

if !rc.evaluate(ctx, "@root/is_index")?.is_missing() {
// Special case for index.md which may be a synthetic page.
Expand Down Expand Up @@ -121,7 +121,7 @@ fn render(
.as_json()
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
.replace('\"', "");

context.insert(
"path_to_root".to_owned(),
Expand All @@ -141,7 +141,7 @@ fn render(
.with_extension("html")
.to_str()
.ok_or_else(|| RenderError::new("Link could not be converted to str"))
.map(|p| context.insert("link".to_owned(), json!(p.replace("\\", "/"))))
.map(|p| context.insert("link".to_owned(), json!(p.replace('\\', "/"))))
})?;

trace!("Render template");
Expand Down
38 changes: 21 additions & 17 deletions src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use std::path::Path;
use std::{cmp::Ordering, collections::BTreeMap};

use crate::utils;
use crate::utils::bracket_escape;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl HelperDef for RenderToc {
.as_json()
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
.replace('\"', "");

let current_section = rc
.evaluate(ctx, "@root/section")?
Expand Down Expand Up @@ -81,22 +81,26 @@ impl HelperDef for RenderToc {
level - 1 < fold_level as usize
};

if level > current_level {
while level > current_level {
out.write("<li>")?;
out.write("<ol class=\"section\">")?;
current_level += 1;
match level.cmp(&current_level) {
Ordering::Greater => {
while level > current_level {
out.write("<li>")?;
out.write("<ol class=\"section\">")?;
current_level += 1;
}
write_li_open_tag(out, is_expanded, false)?;
}
write_li_open_tag(out, is_expanded, false)?;
} else if level < current_level {
while level < current_level {
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
Ordering::Less => {
while level < current_level {
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
}
write_li_open_tag(out, is_expanded, false)?;
}
Ordering::Equal => {
write_li_open_tag(out, is_expanded, item.get("section").is_none())?;
}
write_li_open_tag(out, is_expanded, false)?;
} else {
write_li_open_tag(out, is_expanded, item.get("section").is_none())?;
}

// Part title
Expand All @@ -119,7 +123,7 @@ impl HelperDef for RenderToc {
.to_str()
.unwrap()
// Hack for windows who tends to use `\` as separator instead of `/`
.replace("\\", "/");
.replace('\\', "/");

// Add link
out.write(&utils::fs::path_to_root(&current_path))?;
Expand Down
13 changes: 7 additions & 6 deletions src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->

let mut fields = BTreeMap::new();
let mut opt = SearchOptionsField::default();
opt.boost = Some(search_config.boost_title);
fields.insert("title".into(), opt);
opt.boost = Some(search_config.boost_paragraph);
fields.insert("body".into(), opt);
opt.boost = Some(search_config.boost_hierarchy);
fields.insert("breadcrumbs".into(), opt);
let mut insert_boost = |key: &str, boost| {
opt.boost = Some(boost);
fields.insert(key.into(), opt);
};
insert_boost("title", search_config.boost_title);
insert_boost("body", search_config.boost_paragraph);
insert_boost("breadcrumbs", search_config.boost_hierarchy);

let search_options = SearchOptions {
bool: if search_config.use_boolean_and {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ mod tests {
};

#[test]
#[allow(clippy::reversed_empty_ranges)] // Intentionally checking that those are correctly handled
fn take_lines_test() {
let s = "Lorem\nipsum\ndolor\nsit\namet";
assert_eq!(take_lines(s, 1..3), "ipsum\ndolor");
Expand Down Expand Up @@ -163,6 +164,7 @@ mod tests {
}

#[test]
#[allow(clippy::reversed_empty_ranges)] // Intentionally checking that those are correctly handled
fn take_rustdoc_include_lines_test() {
let s = "Lorem\nipsum\ndolor\nsit\namet";
assert_eq!(
Expand Down