From 248863addf8396bcec0c0bf723a6532280179ce9 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sun, 26 Jun 2022 11:37:52 +0200 Subject: [PATCH] Fix Clippy lints Also remove `allow(clippy::*)`s where possible --- src/book/mod.rs | 12 +++--- src/cmd/init.rs | 5 +-- src/config.rs | 34 +++++++---------- src/lib.rs | 1 - src/preprocess/links.rs | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 2 +- .../html_handlebars/helpers/navigation.rs | 6 +-- src/renderer/html_handlebars/helpers/toc.rs | 38 ++++++++++--------- src/renderer/html_handlebars/search.rs | 13 ++++--- src/utils/string.rs | 2 + 10 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 3370d92c..9745d2b7 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -386,7 +386,7 @@ fn determine_renderers(config: &Config) -> Vec> { 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(); @@ -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] @@ -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] diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 1ee5ff21..c964dcc1 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -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") } diff --git a/src/config.rs b/src/config.rs index 951957bd..b7d03d1a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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); } @@ -371,15 +371,8 @@ impl Serialize for Config { } fn parse_env(key: &str) -> Option { - 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 { @@ -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] @@ -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('-', "_") ) } @@ -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()); @@ -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); } } diff --git a/src/lib.rs b/src/lib.rs index 23309fb0..cc62b0ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,6 @@ #![deny(missing_docs)] #![deny(rust_2018_idioms)] -#![allow(clippy::comparison_chain)] #[macro_use] extern crate lazy_static; diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index edd97ba9..7ca6fd34 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -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), diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 8a13db9f..52d0ab65 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -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!( diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index 83bdadb3..d3f6ca90 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -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. @@ -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(), @@ -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"); diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index 5869dd36..6ae62aa7 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -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; @@ -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")? @@ -81,22 +81,26 @@ impl HelperDef for RenderToc { level - 1 < fold_level as usize }; - if level > current_level { - while level > current_level { - out.write("
  • ")?; - out.write("
      ")?; - current_level += 1; + match level.cmp(¤t_level) { + Ordering::Greater => { + while level > current_level { + out.write("
    1. ")?; + out.write("
        ")?; + 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("
      ")?; - out.write("
    2. ")?; - current_level -= 1; + Ordering::Less => { + while level < current_level { + out.write("
    ")?; + out.write("
  • ")?; + 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 @@ -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(¤t_path))?; diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index 0a59ffe9..fb188fb0 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -211,12 +211,13 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec) -> 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 { diff --git a/src/utils/string.rs b/src/utils/string.rs index 59931743..97485d7b 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -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"); @@ -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!(