Skip to content

Commit 4cf005d

Browse files
authored
Merge pull request #1832 from ISSOtm/clippy
Fix Clippy lints
2 parents b38792c + 248863a commit 4cf005d

File tree

10 files changed

+54
-60
lines changed

10 files changed

+54
-60
lines changed

src/book/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> {
386386
renderers
387387
}
388388

389-
const DEFAULT_PREPROCESSORS: &[&'static str] = &["links", "index"];
389+
const DEFAULT_PREPROCESSORS: &[&str] = &["links", "index"];
390390

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

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

759-
assert!(preprocessors
759+
assert!(!preprocessors
760760
.iter()
761-
.find(|preprocessor| preprocessor.name() == "random")
762-
.is_none());
761+
.any(|preprocessor| preprocessor.name() == "random"));
763762
}
764763

765764
#[test]
@@ -776,10 +775,9 @@ mod tests {
776775

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

779-
assert!(preprocessors
778+
assert!(!preprocessors
780779
.iter()
781-
.find(|preprocessor| preprocessor.name() == "links")
782-
.is_none());
780+
.any(|preprocessor| preprocessor.name() == "links"));
783781
}
784782

785783
#[test]

src/cmd/init.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,5 @@ fn confirm() -> bool {
122122
io::stdout().flush().unwrap();
123123
let mut s = String::new();
124124
io::stdin().read_line(&mut s).ok();
125-
match &*s.trim() {
126-
"Y" | "y" | "yes" | "Yes" => true,
127-
_ => false,
128-
}
125+
matches!(&*s.trim(), "Y" | "y" | "yes" | "Yes")
129126
}

src/config.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ impl Config {
227227
let value = Value::try_from(value)
228228
.with_context(|| "Unable to represent the item as a JSON Value")?;
229229

230-
if index.starts_with("book.") {
231-
self.book.update_value(&index[5..], value);
232-
} else if index.starts_with("build.") {
233-
self.build.update_value(&index[6..], value);
230+
if let Some(key) = index.strip_prefix("book.") {
231+
self.book.update_value(key, value);
232+
} else if let Some(key) = index.strip_prefix("build.") {
233+
self.build.update_value(key, value);
234234
} else {
235235
self.rest.insert(index, value);
236236
}
@@ -371,15 +371,8 @@ impl Serialize for Config {
371371
}
372372

373373
fn parse_env(key: &str) -> Option<String> {
374-
const PREFIX: &str = "MDBOOK_";
375-
376-
if key.starts_with(PREFIX) {
377-
let key = &key[PREFIX.len()..];
378-
379-
Some(key.to_lowercase().replace("__", ".").replace("_", "-"))
380-
} else {
381-
None
382-
}
374+
key.strip_prefix("MDBOOK_")
375+
.map(|key| key.to_lowercase().replace("__", ".").replace('_', "-"))
383376
}
384377

385378
fn is_legacy_format(table: &Value) -> bool {
@@ -828,7 +821,7 @@ mod tests {
828821
"#;
829822

830823
let got = Config::from_str(src).unwrap();
831-
assert_eq!(got.html_config().unwrap().playground.runnable, false);
824+
assert!(!got.html_config().unwrap().playground.runnable);
832825
}
833826

834827
#[test]
@@ -1037,7 +1030,7 @@ mod tests {
10371030
fn encode_env_var(key: &str) -> String {
10381031
format!(
10391032
"MDBOOK_{}",
1040-
key.to_uppercase().replace('.', "__").replace("-", "_")
1033+
key.to_uppercase().replace('.', "__").replace('-', "_")
10411034
)
10421035
}
10431036

@@ -1061,11 +1054,10 @@ mod tests {
10611054
}
10621055

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

10711063
assert!(cfg.get(key).is_none());
@@ -1184,15 +1176,15 @@ mod tests {
11841176
"#;
11851177
let got = Config::from_str(src).unwrap();
11861178
let html_config = got.html_config().unwrap();
1187-
assert_eq!(html_config.print.enable, false);
1188-
assert_eq!(html_config.print.page_break, true);
1179+
assert!(!html_config.print.enable);
1180+
assert!(html_config.print.page_break);
11891181
let src = r#"
11901182
[output.html.print]
11911183
page-break = false
11921184
"#;
11931185
let got = Config::from_str(src).unwrap();
11941186
let html_config = got.html_config().unwrap();
1195-
assert_eq!(html_config.print.enable, true);
1196-
assert_eq!(html_config.print.page_break, false);
1187+
assert!(html_config.print.enable);
1188+
assert!(!html_config.print.page_break);
11971189
}
11981190
}

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
8383
#![deny(missing_docs)]
8484
#![deny(rust_2018_idioms)]
85-
#![allow(clippy::comparison_chain)]
8685

8786
#[macro_use]
8887
extern crate lazy_static;

src/preprocess/links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ enum RangeOrAnchor {
146146
}
147147

148148
// A range of lines specified with some include directive.
149+
#[allow(clippy::enum_variant_names)] // The prefix can't be removed, and is meant to mirror the contained type
149150
#[derive(PartialEq, Debug, Clone)]
150151
enum LineRange {
151152
Range(Range<usize>),

src/renderer/html_handlebars/hbs_renderer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ fn fix_code_blocks(html: &str) -> String {
814814
FIX_CODE_BLOCKS
815815
.replace_all(html, |caps: &Captures<'_>| {
816816
let before = &caps[1];
817-
let classes = &caps[2].replace(",", " ");
817+
let classes = &caps[2].replace(',', " ");
818818
let after = &caps[3];
819819

820820
format!(

src/renderer/html_handlebars/helpers/navigation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn find_chapter(
6161
.as_json()
6262
.as_str()
6363
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
64-
.replace("\"", "");
64+
.replace('\"', "");
6565

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

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

147147
trace!("Render template");

src/renderer/html_handlebars/helpers/toc.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::collections::BTreeMap;
21
use std::path::Path;
2+
use std::{cmp::Ordering, collections::BTreeMap};
33

44
use crate::utils;
55
use crate::utils::bracket_escape;
@@ -33,7 +33,7 @@ impl HelperDef for RenderToc {
3333
.as_json()
3434
.as_str()
3535
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
36-
.replace("\"", "");
36+
.replace('\"', "");
3737

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

84-
if level > current_level {
85-
while level > current_level {
86-
out.write("<li>")?;
87-
out.write("<ol class=\"section\">")?;
88-
current_level += 1;
84+
match level.cmp(&current_level) {
85+
Ordering::Greater => {
86+
while level > current_level {
87+
out.write("<li>")?;
88+
out.write("<ol class=\"section\">")?;
89+
current_level += 1;
90+
}
91+
write_li_open_tag(out, is_expanded, false)?;
8992
}
90-
write_li_open_tag(out, is_expanded, false)?;
91-
} else if level < current_level {
92-
while level < current_level {
93-
out.write("</ol>")?;
94-
out.write("</li>")?;
95-
current_level -= 1;
93+
Ordering::Less => {
94+
while level < current_level {
95+
out.write("</ol>")?;
96+
out.write("</li>")?;
97+
current_level -= 1;
98+
}
99+
write_li_open_tag(out, is_expanded, false)?;
100+
}
101+
Ordering::Equal => {
102+
write_li_open_tag(out, is_expanded, item.get("section").is_none())?;
96103
}
97-
write_li_open_tag(out, is_expanded, false)?;
98-
} else {
99-
write_li_open_tag(out, is_expanded, item.get("section").is_none())?;
100104
}
101105

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

124128
// Add link
125129
out.write(&utils::fs::path_to_root(&current_path))?;

src/renderer/html_handlebars/search.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,13 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->
227227

228228
let mut fields = BTreeMap::new();
229229
let mut opt = SearchOptionsField::default();
230-
opt.boost = Some(search_config.boost_title);
231-
fields.insert("title".into(), opt);
232-
opt.boost = Some(search_config.boost_paragraph);
233-
fields.insert("body".into(), opt);
234-
opt.boost = Some(search_config.boost_hierarchy);
235-
fields.insert("breadcrumbs".into(), opt);
230+
let mut insert_boost = |key: &str, boost| {
231+
opt.boost = Some(boost);
232+
fields.insert(key.into(), opt);
233+
};
234+
insert_boost("title", search_config.boost_title);
235+
insert_boost("body", search_config.boost_paragraph);
236+
insert_boost("breadcrumbs", search_config.boost_hierarchy);
236237

237238
let search_options = SearchOptions {
238239
bool: if search_config.use_boolean_and {

src/utils/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ mod tests {
122122
};
123123

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

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

0 commit comments

Comments
 (0)