Apply clippy::needless_borrow

This commit is contained in:
Eric Huss 2023-05-13 09:44:11 -07:00
parent f4507aeb9b
commit 45a8575b95
17 changed files with 50 additions and 54 deletions

View File

@ -277,7 +277,7 @@ fn load_chapter<P: AsRef<Path>>(
} }
let stripped = location let stripped = location
.strip_prefix(&src_dir) .strip_prefix(src_dir)
.expect("Chapters are always inside a book"); .expect("Chapters are always inside a book");
Chapter::new(&link.name, content, stripped, parent_names.clone()) Chapter::new(&link.name, content, stripped, parent_names.clone())

View File

@ -198,8 +198,7 @@ impl BookBuilder {
writeln!(f, "- [Chapter 1](./chapter_1.md)")?; writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
let chapter_1 = src_dir.join("chapter_1.md"); let chapter_1 = src_dir.join("chapter_1.md");
let mut f = let mut f = File::create(chapter_1).with_context(|| "Unable to create chapter_1.md")?;
File::create(&chapter_1).with_context(|| "Unable to create chapter_1.md")?;
writeln!(f, "# Chapter 1")?; writeln!(f, "# Chapter 1")?;
} else { } else {
trace!("Existing summary found, no need to create stub files."); trace!("Existing summary found, no need to create stub files.");
@ -212,10 +211,10 @@ impl BookBuilder {
fs::create_dir_all(&self.root)?; fs::create_dir_all(&self.root)?;
let src = self.root.join(&self.config.book.src); let src = self.root.join(&self.config.book.src);
fs::create_dir_all(&src)?; fs::create_dir_all(src)?;
let build = self.root.join(&self.config.build.build_dir); let build = self.root.join(&self.config.build.build_dir);
fs::create_dir_all(&build)?; fs::create_dir_all(build)?;
Ok(()) Ok(())
} }

View File

@ -99,7 +99,7 @@ impl MDBook {
let root = book_root.into(); let root = book_root.into();
let src_dir = root.join(&config.book.src); let src_dir = root.join(&config.book.src);
let book = book::load_book(&src_dir, &config.build)?; let book = book::load_book(src_dir, &config.build)?;
let renderers = determine_renderers(&config); let renderers = determine_renderers(&config);
let preprocessors = determine_preprocessors(&config)?; let preprocessors = determine_preprocessors(&config)?;
@ -122,7 +122,7 @@ impl MDBook {
let root = book_root.into(); let root = book_root.into();
let src_dir = root.join(&config.book.src); let src_dir = root.join(&config.book.src);
let book = book::load_book_from_disk(&summary, &src_dir)?; let book = book::load_book_from_disk(&summary, src_dir)?;
let renderers = determine_renderers(&config); let renderers = determine_renderers(&config);
let preprocessors = determine_preprocessors(&config)?; let preprocessors = determine_preprocessors(&config)?;
@ -309,7 +309,7 @@ impl MDBook {
info!("Testing chapter '{}': {:?}", ch.name, chapter_path); info!("Testing chapter '{}': {:?}", ch.name, chapter_path);
// write preprocessed file to tempdir // write preprocessed file to tempdir
let path = temp_dir.path().join(&chapter_path); let path = temp_dir.path().join(chapter_path);
let mut tmpf = utils::fs::create_file(&path)?; let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(ch.content.as_bytes())?; tmpf.write_all(ch.content.as_bytes())?;
@ -319,13 +319,13 @@ impl MDBook {
if let Some(edition) = self.config.rust.edition { if let Some(edition) = self.config.rust.edition {
match edition { match edition {
RustEdition::E2015 => { RustEdition::E2015 => {
cmd.args(&["--edition", "2015"]); cmd.args(["--edition", "2015"]);
} }
RustEdition::E2018 => { RustEdition::E2018 => {
cmd.args(&["--edition", "2018"]); cmd.args(["--edition", "2018"]);
} }
RustEdition::E2021 => { RustEdition::E2021 => {
cmd.args(&["--edition", "2021"]); cmd.args(["--edition", "2021"]);
} }
} }
} }

View File

@ -16,7 +16,7 @@ pub fn make_subcommand() -> Command {
// Build command implementation // Build command implementation
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?; let mut book = MDBook::load(book_dir)?;
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") { if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
book.config.build.build_dir = dest_dir.into(); book.config.build.build_dir = dest_dir.into();

View File

@ -16,7 +16,7 @@ pub fn make_subcommand() -> Command {
// Clean command implementation // Clean command implementation
pub fn execute(args: &ArgMatches) -> mdbook::errors::Result<()> { pub fn execute(args: &ArgMatches) -> mdbook::errors::Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let book = MDBook::load(&book_dir)?; let book = MDBook::load(book_dir)?;
let dir_to_remove = match args.get_one::<PathBuf>("dest-dir") { let dir_to_remove = match args.get_one::<PathBuf>("dest-dir") {
Some(dest_dir) => dest_dir.into(), Some(dest_dir) => dest_dir.into(),

View File

@ -86,7 +86,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
/// Obtains author name from git config file by running the `git config` command. /// Obtains author name from git config file by running the `git config` command.
fn get_author_name() -> Option<String> { fn get_author_name() -> Option<String> {
let output = Command::new("git") let output = Command::new("git")
.args(&["config", "--get", "user.name"]) .args(["config", "--get", "user.name"])
.output() .output()
.ok()?; .ok()?;

View File

@ -48,7 +48,7 @@ pub fn make_subcommand() -> Command {
// Serve command implementation // Serve command implementation
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?; let mut book = MDBook::load(book_dir)?;
let port = args.get_one::<String>("port").unwrap(); let port = args.get_one::<String>("port").unwrap();
let hostname = args.get_one::<String>("hostname").unwrap(); let hostname = args.get_one::<String>("hostname").unwrap();
@ -102,7 +102,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
info!("Building book..."); info!("Building book...");
// FIXME: This area is really ugly because we need to re-set livereload :( // FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(&book_dir).and_then(|mut b| { let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b); update_config(&mut b);
b.build() b.build()
}); });

View File

@ -44,7 +44,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let chapter: Option<&str> = args.get_one::<String>("chapter").map(|s| s.as_str()); let chapter: Option<&str> = args.get_one::<String>("chapter").map(|s| s.as_str());
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?; let mut book = MDBook::load(book_dir)?;
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") { if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
book.config.build.build_dir = dest_dir.to_path_buf(); book.config.build.build_dir = dest_dir.to_path_buf();

View File

@ -21,7 +21,7 @@ pub fn make_subcommand() -> Command {
// Watch command implementation // Watch command implementation
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::load(&book_dir)?; let mut book = MDBook::load(book_dir)?;
let update_config = |book: &mut MDBook| { let update_config = |book: &mut MDBook| {
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") { if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
@ -42,7 +42,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
trigger_on_change(&book, |paths, book_dir| { trigger_on_change(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths); info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(&book_dir).and_then(|mut b| { let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b); update_config(&mut b);
b.build() b.build()
}); });

View File

@ -93,7 +93,7 @@ where
for link in find_links(s) { for link in find_links(s) {
replaced.push_str(&s[previous_end_index..link.start_index]); replaced.push_str(&s[previous_end_index..link.start_index]);
match link.render_with_path(&path, chapter_title) { match link.render_with_path(path, chapter_title) {
Ok(new_content) => { Ok(new_content) => {
if depth < MAX_LINK_NESTED_DEPTH { if depth < MAX_LINK_NESTED_DEPTH {
if let Some(rel_path) = link.link_type.relative_path(path) { if let Some(rel_path) = link.link_type.relative_path(path) {
@ -327,7 +327,7 @@ impl<'a> Link<'a> {
let base = base.as_ref(); let base = base.as_ref();
match self.link_type { match self.link_type {
// omit the escape char // omit the escape char
LinkType::Escaped => Ok((&self.link_text[1..]).to_owned()), LinkType::Escaped => Ok(self.link_text[1..].to_owned()),
LinkType::Include(ref pat, ref range_or_anchor) => { LinkType::Include(ref pat, ref range_or_anchor) => {
let target = base.join(pat); let target = base.join(pat);

View File

@ -99,7 +99,7 @@ impl HtmlHandlebars {
ctx.data.insert("title".to_owned(), json!(title)); ctx.data.insert("title".to_owned(), json!(title));
ctx.data.insert( ctx.data.insert(
"path_to_root".to_owned(), "path_to_root".to_owned(),
json!(utils::fs::path_to_root(&path)), json!(utils::fs::path_to_root(path)),
); );
if let Some(ref section) = ch.number { if let Some(ref section) = ch.number {
ctx.data ctx.data
@ -292,7 +292,7 @@ impl HtmlHandlebars {
} }
if let Some(fonts_css) = &theme.fonts_css { if let Some(fonts_css) = &theme.fonts_css {
if !fonts_css.is_empty() { if !fonts_css.is_empty() {
write_file(destination, "fonts/fonts.css", &fonts_css)?; write_file(destination, "fonts/fonts.css", fonts_css)?;
} }
} }
if !html_config.copy_fonts && theme.fonts_css.is_none() { if !html_config.copy_fonts && theme.fonts_css.is_none() {
@ -547,7 +547,7 @@ impl Renderer for HtmlHandlebars {
// Print version // Print version
let mut print_content = String::new(); let mut print_content = String::new();
fs::create_dir_all(&destination) fs::create_dir_all(destination)
.with_context(|| "Unexpected error when constructing destination path")?; .with_context(|| "Unexpected error when constructing destination path")?;
let mut is_index = true; let mut is_index = true;

View File

@ -127,7 +127,7 @@ fn render(
context.insert( context.insert(
"path_to_root".to_owned(), "path_to_root".to_owned(),
json!(utils::fs::path_to_root(&base_path)), json!(utils::fs::path_to_root(base_path)),
); );
chapter chapter

View File

@ -37,14 +37,14 @@ impl Renderer for MarkdownRenderer {
if !ch.is_draft_chapter() { if !ch.is_draft_chapter() {
utils::fs::write_file( utils::fs::write_file(
&ctx.destination, &ctx.destination,
&ch.path.as_ref().expect("Checked path exists before"), ch.path.as_ref().expect("Checked path exists before"),
ch.content.as_bytes(), ch.content.as_bytes(),
)?; )?;
} }
} }
} }
fs::create_dir_all(&destination) fs::create_dir_all(destination)
.with_context(|| "Unexpected error when constructing destination path")?; .with_context(|| "Unexpected error when constructing destination path")?;
Ok(()) Ok(())

View File

@ -210,39 +210,36 @@ mod tests {
}; };
// Create a couple of files // Create a couple of files
if let Err(err) = fs::File::create(&tmp.path().join("file.txt")) { if let Err(err) = fs::File::create(tmp.path().join("file.txt")) {
panic!("Could not create file.txt: {}", err); panic!("Could not create file.txt: {}", err);
} }
if let Err(err) = fs::File::create(&tmp.path().join("file.md")) { if let Err(err) = fs::File::create(tmp.path().join("file.md")) {
panic!("Could not create file.md: {}", err); panic!("Could not create file.md: {}", err);
} }
if let Err(err) = fs::File::create(&tmp.path().join("file.png")) { if let Err(err) = fs::File::create(tmp.path().join("file.png")) {
panic!("Could not create file.png: {}", err); panic!("Could not create file.png: {}", err);
} }
if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir")) { if let Err(err) = fs::create_dir(tmp.path().join("sub_dir")) {
panic!("Could not create sub_dir: {}", err); panic!("Could not create sub_dir: {}", err);
} }
if let Err(err) = fs::File::create(&tmp.path().join("sub_dir/file.png")) { if let Err(err) = fs::File::create(tmp.path().join("sub_dir/file.png")) {
panic!("Could not create sub_dir/file.png: {}", err); panic!("Could not create sub_dir/file.png: {}", err);
} }
if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir_exists")) { if let Err(err) = fs::create_dir(tmp.path().join("sub_dir_exists")) {
panic!("Could not create sub_dir_exists: {}", err); panic!("Could not create sub_dir_exists: {}", err);
} }
if let Err(err) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) { if let Err(err) = fs::File::create(tmp.path().join("sub_dir_exists/file.txt")) {
panic!("Could not create sub_dir_exists/file.txt: {}", err); panic!("Could not create sub_dir_exists/file.txt: {}", err);
} }
if let Err(err) = symlink( if let Err(err) = symlink(tmp.path().join("file.png"), tmp.path().join("symlink.png")) {
&tmp.path().join("file.png"),
&tmp.path().join("symlink.png"),
) {
panic!("Could not symlink file.png: {}", err); panic!("Could not symlink file.png: {}", err);
} }
// Create output dir // Create output dir
if let Err(err) = fs::create_dir(&tmp.path().join("output")) { if let Err(err) = fs::create_dir(tmp.path().join("output")) {
panic!("Could not create output: {}", err); panic!("Could not create output: {}", err);
} }
if let Err(err) = fs::create_dir(&tmp.path().join("output/sub_dir_exists")) { if let Err(err) = fs::create_dir(tmp.path().join("output/sub_dir_exists")) {
panic!("Could not create output/sub_dir_exists: {}", err); panic!("Could not create output/sub_dir_exists: {}", err);
} }
@ -253,22 +250,22 @@ mod tests {
} }
// Check if the correct files where created // Check if the correct files where created
if !(&tmp.path().join("output/file.txt")).exists() { if !tmp.path().join("output/file.txt").exists() {
panic!("output/file.txt should exist") panic!("output/file.txt should exist")
} }
if (&tmp.path().join("output/file.md")).exists() { if tmp.path().join("output/file.md").exists() {
panic!("output/file.md should not exist") panic!("output/file.md should not exist")
} }
if !(&tmp.path().join("output/file.png")).exists() { if !tmp.path().join("output/file.png").exists() {
panic!("output/file.png should exist") panic!("output/file.png should exist")
} }
if !(&tmp.path().join("output/sub_dir/file.png")).exists() { if !tmp.path().join("output/sub_dir/file.png").exists() {
panic!("output/sub_dir/file.png should exist") panic!("output/sub_dir/file.png should exist")
} }
if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() { if !tmp.path().join("output/sub_dir_exists/file.txt").exists() {
panic!("output/sub_dir/file.png should exist") panic!("output/sub_dir/file.png should exist")
} }
if !(&tmp.path().join("output/symlink.png")).exists() { if !tmp.path().join("output/symlink.png").exists() {
panic!("output/symlink.png should exist") panic!("output/symlink.png should exist")
} }
} }

View File

@ -90,7 +90,7 @@ fn relative_command_path() {
.set("output.html", toml::value::Table::new()) .set("output.html", toml::value::Table::new())
.unwrap(); .unwrap();
config.set("output.myrenderer.command", cmd_path).unwrap(); config.set("output.myrenderer.command", cmd_path).unwrap();
let md = MDBook::init(&temp.path()) let md = MDBook::init(temp.path())
.with_config(config) .with_config(config)
.build() .build()
.unwrap(); .unwrap();

View File

@ -112,12 +112,12 @@ fn recursive_copy<A: AsRef<Path>, B: AsRef<Path>>(from: A, to: B) -> Result<()>
let from = from.as_ref(); let from = from.as_ref();
let to = to.as_ref(); let to = to.as_ref();
for entry in WalkDir::new(&from) { for entry in WalkDir::new(from) {
let entry = entry.with_context(|| "Unable to inspect directory entry")?; let entry = entry.with_context(|| "Unable to inspect directory entry")?;
let original_location = entry.path(); let original_location = entry.path();
let relative = original_location let relative = original_location
.strip_prefix(&from) .strip_prefix(from)
.expect("`original_location` is inside the `from` directory"); .expect("`original_location` is inside the `from` directory");
let new_location = to.join(relative); let new_location = to.join(relative);
@ -126,7 +126,7 @@ fn recursive_copy<A: AsRef<Path>, B: AsRef<Path>>(from: A, to: B) -> Result<()>
fs::create_dir_all(parent).with_context(|| "Couldn't create directory")?; fs::create_dir_all(parent).with_context(|| "Couldn't create directory")?;
} }
fs::copy(&original_location, &new_location) fs::copy(original_location, &new_location)
.with_context(|| "Unable to copy file contents")?; .with_context(|| "Unable to copy file contents")?;
} }
} }

View File

@ -275,7 +275,7 @@ fn root_index_html() -> Result<Document> {
.with_context(|| "Book building failed")?; .with_context(|| "Book building failed")?;
let index_page = temp.path().join("book").join("index.html"); let index_page = temp.path().join("book").join("index.html");
let html = fs::read_to_string(&index_page).with_context(|| "Unable to read index.html")?; let html = fs::read_to_string(index_page).with_context(|| "Unable to read index.html")?;
Ok(Document::from(html.as_str())) Ok(Document::from(html.as_str()))
} }
@ -412,7 +412,7 @@ fn recursive_includes_are_capped() {
let content = &["Around the world, around the world let content = &["Around the world, around the world
Around the world, around the world Around the world, around the world
Around the world, around the world"]; Around the world, around the world"];
assert_contains_strings(&recursive, content); assert_contains_strings(recursive, content);
} }
#[test] #[test]
@ -462,7 +462,7 @@ fn by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index() {
let second_index = temp.path().join("book").join("second").join("index.html"); let second_index = temp.path().join("book").join("second").join("index.html");
let unexpected_strings = vec!["Second README"]; let unexpected_strings = vec!["Second README"];
assert_doesnt_contain_strings(&second_index, &unexpected_strings); assert_doesnt_contain_strings(second_index, &unexpected_strings);
} }
#[test] #[test]
@ -803,7 +803,7 @@ mod search {
let src = read_book_index(temp.path()); let src = read_book_index(temp.path());
let dest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/searchindex_fixture.json"); let dest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/searchindex_fixture.json");
let dest = File::create(&dest).unwrap(); let dest = File::create(dest).unwrap();
serde_json::to_writer_pretty(dest, &src).unwrap(); serde_json::to_writer_pretty(dest, &src).unwrap();
src src