Skip to content

Commit d7f38d0

Browse files
authored
Merge pull request #205 from frewsxcv/clippy
Address warnings found by rust-clippy.
2 parents 864be6c + 95fd292 commit d7f38d0

File tree

9 files changed

+103
-121
lines changed

9 files changed

+103
-121
lines changed

src/bin/mdbook.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#[macro_use]
21
extern crate mdbook;
32
#[macro_use]
43
extern crate clap;
5-
#[macro_use]
64
extern crate log;
75
extern crate env_logger;
86
extern crate open;
@@ -204,9 +202,8 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
204202

205203
trigger_on_change(&mut book, |path, book| {
206204
println!("File changed: {:?}\nBuilding book...\n", path);
207-
match book.build() {
208-
Err(e) => println!("Error while building: {:?}", e),
209-
_ => {},
205+
if let Err(e) = book.build() {
206+
println!("Error while building: {:?}", e);
210207
}
211208
println!("");
212209
});
@@ -348,10 +345,10 @@ fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
348345

349346
// Add the book.{json,toml} file to the watcher if it exists, because it's not
350347
// located in the source directory
351-
if let Err(_) = watcher.watch(book.get_root().join("book.json"), NonRecursive) {
348+
if watcher.watch(book.get_root().join("book.json"), NonRecursive).is_err() {
352349
// do nothing if book.json is not found
353350
}
354-
if let Err(_) = watcher.watch(book.get_root().join("book.toml"), NonRecursive) {
351+
if watcher.watch(book.get_root().join("book.toml"), NonRecursive).is_err() {
355352
// do nothing if book.toml is not found
356353
}
357354

src/book/bookconfig.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl BookConfig {
5353
exit(2);
5454
}
5555
};
56-
if let Err(_) = f.read_to_string(&mut data) {
56+
if f.read_to_string(&mut data).is_err() {
5757
error!("[*]: Failed to read {:?}", &path);
5858
exit(2);
5959
}
@@ -81,9 +81,9 @@ impl BookConfig {
8181
self
8282
}
8383

84-
pub fn parse_from_toml_string(&mut self, data: &String) -> &mut Self {
84+
pub fn parse_from_toml_string(&mut self, data: &str) -> &mut Self {
8585

86-
let mut parser = toml::Parser::new(&data);
86+
let mut parser = toml::Parser::new(data);
8787

8888
let config = match parser.parse() {
8989
Some(x) => {x},
@@ -99,17 +99,17 @@ impl BookConfig {
9999
}
100100

101101
/// Parses the string to JSON and converts it to BTreeMap<String, toml::Value>.
102-
pub fn parse_from_json_string(&mut self, data: &String) -> &mut Self {
102+
pub fn parse_from_json_string(&mut self, data: &str) -> &mut Self {
103103

104-
let c: serde_json::Value = match serde_json::from_str(&data) {
104+
let c: serde_json::Value = match serde_json::from_str(data) {
105105
Ok(x) => x,
106106
Err(e) => {
107107
error!("[*]: JSON parse errors in book.json: {:?}", e);
108108
exit(2);
109109
}
110110
};
111111

112-
let config = json_object_to_btreemap(&c.as_object().unwrap());
112+
let config = json_object_to_btreemap(c.as_object().unwrap());
113113
self.parse_from_btreemap(&config);
114114

115115
self

src/book/bookitem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a> Iterator for BookItems<'a> {
6363
},
6464
}
6565
} else {
66-
let cur = self.items.get(self.current_index).unwrap();
66+
let cur = &self.items[self.current_index];
6767

6868
match *cur {
6969
BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => {

src/book/mod.rs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -346,29 +346,26 @@ impl MDBook {
346346
try!(self.parse_summary());
347347
for item in self.iter() {
348348

349-
match *item {
350-
BookItem::Chapter(_, ref ch) => {
351-
if ch.path != PathBuf::new() {
349+
if let BookItem::Chapter(_, ref ch) = *item {
350+
if ch.path != PathBuf::new() {
352351

353-
let path = self.get_src().join(&ch.path);
352+
let path = self.get_src().join(&ch.path);
354353

355-
println!("[*]: Testing file: {:?}", path);
354+
println!("[*]: Testing file: {:?}", path);
356355

357-
let output_result = Command::new("rustdoc")
358-
.arg(&path)
359-
.arg("--test")
360-
.output();
361-
let output = try!(output_result);
356+
let output_result = Command::new("rustdoc")
357+
.arg(&path)
358+
.arg("--test")
359+
.output();
360+
let output = try!(output_result);
362361

363-
if !output.status.success() {
364-
return Err(Box::new(io::Error::new(ErrorKind::Other, format!(
365-
"{}\n{}",
366-
String::from_utf8_lossy(&output.stdout),
367-
String::from_utf8_lossy(&output.stderr)))) as Box<Error>);
368-
}
362+
if !output.status.success() {
363+
return Err(Box::new(io::Error::new(ErrorKind::Other, format!(
364+
"{}\n{}",
365+
String::from_utf8_lossy(&output.stdout),
366+
String::from_utf8_lossy(&output.stderr)))) as Box<Error>);
369367
}
370-
},
371-
_ => {},
368+
}
372369
}
373370
}
374371
Ok(())
@@ -381,14 +378,11 @@ impl MDBook {
381378
pub fn set_dest(mut self, dest: &Path) -> Self {
382379

383380
// Handle absolute and relative paths
384-
match dest.is_absolute() {
385-
true => {
386-
self.dest = dest.to_owned();
387-
},
388-
false => {
389-
let dest = self.root.join(dest).to_owned();
390-
self.dest = dest;
391-
},
381+
if dest.is_absolute() {
382+
self.dest = dest.to_owned();
383+
} else {
384+
let dest = self.root.join(dest).to_owned();
385+
self.dest = dest;
392386
}
393387

394388
self
@@ -401,14 +395,11 @@ impl MDBook {
401395
pub fn set_src(mut self, src: &Path) -> Self {
402396

403397
// Handle absolute and relative paths
404-
match src.is_absolute() {
405-
true => {
406-
self.src = src.to_owned();
407-
},
408-
false => {
409-
let src = self.root.join(src).to_owned();
410-
self.src = src;
411-
},
398+
if src.is_absolute() {
399+
self.src = src.to_owned();
400+
} else {
401+
let src = self.root.join(src).to_owned();
402+
self.src = src;
412403
}
413404

414405
self
@@ -456,16 +447,14 @@ impl MDBook {
456447
}
457448

458449
pub fn get_livereload(&self) -> Option<&String> {
459-
match self.livereload {
460-
Some(ref livereload) => Some(&livereload),
461-
None => None,
462-
}
450+
self.livereload.as_ref()
463451
}
464452

465453
pub fn set_theme_path(mut self, theme_path: &Path) -> Self {
466-
self.theme_path = match theme_path.is_absolute() {
467-
true => theme_path.to_owned(),
468-
false => self.root.join(theme_path).to_owned(),
454+
self.theme_path = if theme_path.is_absolute() {
455+
theme_path.to_owned()
456+
} else {
457+
self.root.join(theme_path).to_owned()
469458
};
470459
self
471460
}

src/parse/summary.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ fn parse_level(summary: &mut Vec<&str>, current_level: i32, mut section: Vec<i32
4747
continue;
4848
} else {
4949
return Err(Error::new(ErrorKind::Other,
50-
format!("Your summary.md is messed up\n\n
50+
"Your summary.md is messed up\n\n
5151
Prefix, \
52-
Suffix and Spacer elements can only exist on the root level.\n
52+
Suffix and Spacer elements can only exist on the root level.\n
5353
\
54-
Prefix elements can only exist before any chapter and there can be \
55-
no chapters after suffix elements.")));
54+
Prefix elements can only exist before any chapter and there can be \
55+
no chapters after suffix elements."));
5656
};
5757

5858
} else {
@@ -64,25 +64,25 @@ fn parse_level(summary: &mut Vec<&str>, current_level: i32, mut section: Vec<i32
6464
// error if level != 0 and BookItem is != Chapter
6565
BookItem::Affix(_) | BookItem::Spacer if level > 0 => {
6666
return Err(Error::new(ErrorKind::Other,
67-
format!("Your summary.md is messed up\n\n
67+
"Your summary.md is messed up\n\n
6868
\
69-
Prefix, Suffix and Spacer elements can only exist on the \
70-
root level.\n
69+
Prefix, Suffix and Spacer elements can only exist on the \
70+
root level.\n
7171
Prefix \
72-
elements can only exist before any chapter and there can be \
73-
no chapters after suffix elements.")))
72+
elements can only exist before any chapter and there can be \
73+
no chapters after suffix elements."))
7474
},
7575

7676
// error if BookItem == Chapter and section == -1
7777
BookItem::Chapter(_, _) if section[0] == -1 => {
7878
return Err(Error::new(ErrorKind::Other,
79-
format!("Your summary.md is messed up\n\n
79+
"Your summary.md is messed up\n\n
8080
\
81-
Prefix, Suffix and Spacer elements can only exist on the \
82-
root level.\n
81+
Prefix, Suffix and Spacer elements can only exist on the \
82+
root level.\n
8383
Prefix \
84-
elements can only exist before any chapter and there can be \
85-
no chapters after suffix elements.")))
84+
elements can only exist before any chapter and there can be \
85+
no chapters after suffix elements."))
8686
},
8787

8888
// Set section = -1 after suffix

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use handlebars::Handlebars;
1717
use serde_json;
1818

1919

20+
#[derive(Default)]
2021
pub struct HtmlHandlebars;
2122

2223
impl HtmlHandlebars {
@@ -50,7 +51,7 @@ impl Renderer for HtmlHandlebars {
5051

5152
// Check if dest directory exists
5253
debug!("[*]: Check if destination directory exists");
53-
if let Err(_) = fs::create_dir_all(book.get_dest()) {
54+
if fs::create_dir_all(book.get_dest()).is_err() {
5455
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
5556
"Unexpected error when constructing destination path")));
5657
}
@@ -83,8 +84,8 @@ impl Renderer for HtmlHandlebars {
8384
print_content.push_str(&content);
8485

8586
// Update the context with data for this file
86-
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
87-
"Could not convert path to str"))?;
87+
let path = ch.path.to_str().ok_or_else(||
88+
io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?;
8889
data.insert("path".to_owned(), json!(path));
8990
data.insert("content".to_owned(), json!(content));
9091
data.insert("chapter_title".to_owned(), json!(ch.name));
@@ -188,15 +189,15 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
188189
match *item {
189190
BookItem::Affix(ref ch) => {
190191
chapter.insert("name".to_owned(), json!(ch.name));
191-
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
192-
"Could not convert path to str"))?;
192+
let path = ch.path.to_str().ok_or_else(||
193+
io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?;
193194
chapter.insert("path".to_owned(), json!(path));
194195
},
195196
BookItem::Chapter(ref s, ref ch) => {
196197
chapter.insert("section".to_owned(), json!(s));
197198
chapter.insert("name".to_owned(), json!(ch.name));
198-
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
199-
"Could not convert path to str"))?;
199+
let path = ch.path.to_str().ok_or_else(||
200+
io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?;
200201
chapter.insert("path".to_owned(), json!(path));
201202
},
202203
BookItem::Spacer => {

src/renderer/html_handlebars/helpers/playpen.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn render_playpen(s: &str, path: &Path) -> String {
3131
continue;
3232
};
3333
let mut file_content = String::new();
34-
if let Err(_) = file.read_to_string(&mut file_content) {
34+
if file.read_to_string(&mut file_content).is_err() {
3535
continue;
3636
};
3737

@@ -86,23 +86,18 @@ fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {
8686
if end_i - 2 - (i + 10) < 1 {
8787
continue;
8888
}
89-
if s[i + 10..end_i - 2].trim().len() == 0 {
89+
if s[i + 10..end_i - 2].trim().is_empty() {
9090
continue;
9191
}
9292

9393
debug!("{}", s[i + 10..end_i - 2].to_string());
9494

9595
// Split on whitespaces
9696
let params: Vec<&str> = s[i + 10..end_i - 2].split_whitespace().collect();
97-
let mut editable = false;
98-
99-
if params.len() > 1 {
100-
editable = if let Some(_) = params[1].find("editable") {
101-
true
102-
} else {
103-
false
104-
};
105-
}
97+
let editable = params
98+
.get(1)
99+
.map(|p| p.find("editable").is_some())
100+
.unwrap_or(false);
106101

107102
playpens.push(Playpen {
108103
start_index: i,

0 commit comments

Comments
 (0)