From 6989eb73ab1578550a7a64e864d22164997215c1 Mon Sep 17 00:00:00 2001 From: bemyak Date: Sat, 4 Jun 2022 21:29:33 +0300 Subject: [PATCH] Fix clippy lint warnings (#1888) --- components/config/src/config/mod.rs | 6 +++--- components/content/src/page.rs | 27 +++++---------------------- components/content/src/sorting.rs | 10 +++++----- components/content/src/utils.rs | 26 ++++++-------------------- components/utils/src/fs.rs | 10 +++++----- components/utils/src/templates.rs | 4 ++-- src/main.rs | 2 +- 7 files changed, 27 insertions(+), 58 deletions(-) diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index 0553d8fd..84506756 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -677,7 +677,7 @@ bar = "baz" "#; let theme = Theme::parse(theme_str).unwrap(); // We expect an error here - assert!(!config.add_theme_extra(&theme).is_ok()); + assert!(config.add_theme_extra(&theme).is_err()); } #[test] @@ -714,7 +714,7 @@ highlight_theme = "asdf" "#; let config = Config::parse(config); - assert_eq!(config.is_err(), true); + assert!(config.is_err()); } #[test] @@ -728,7 +728,7 @@ highlight_themes_css = [ "#; let config = Config::parse(config); - assert_eq!(config.is_err(), true); + assert!(config.is_err()); } // https://github.com/getzola/zola/issues/1687 diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 73aa76bf..dafe0688 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -563,11 +563,7 @@ And here's another. [^2] File::create(nested_path.join("graph.jpg")).unwrap(); File::create(nested_path.join("fail.png")).unwrap(); - let res = Page::from_file( - nested_path.join("index.md").as_path(), - &Config::default(), - &path.to_path_buf(), - ); + let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path); assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.file.parent, path.join("content").join("posts")); @@ -591,11 +587,7 @@ And here's another. [^2] File::create(nested_path.join("graph.jpg")).unwrap(); File::create(nested_path.join("fail.png")).unwrap(); - let res = Page::from_file( - nested_path.join("index.md").as_path(), - &Config::default(), - &path.to_path_buf(), - ); + let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path); assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.file.parent, path.join("content").join("posts")); @@ -619,11 +611,7 @@ And here's another. [^2] File::create(nested_path.join("graph.jpg")).unwrap(); File::create(nested_path.join("fail.png")).unwrap(); - let res = Page::from_file( - nested_path.join("index.md").as_path(), - &Config::default(), - &path.to_path_buf(), - ); + let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path); assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.file.parent, path.join("content").join("posts")); @@ -649,11 +637,7 @@ And here's another. [^2] File::create(nested_path.join("graph.jpg")).unwrap(); File::create(nested_path.join("fail.png")).unwrap(); - let res = Page::from_file( - nested_path.join("index.md").as_path(), - &Config::default(), - &path.to_path_buf(), - ); + let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path); assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.file.parent, path.join("content").join("posts")); @@ -682,8 +666,7 @@ And here's another. [^2] let mut config = Config::default(); config.ignored_content_globset = Some(gsb.build().unwrap()); - let res = - Page::from_file(nested_path.join("index.md").as_path(), &config, &path.to_path_buf()); + let res = Page::from_file(nested_path.join("index.md").as_path(), &config, path); assert!(res.is_ok()); let page = res.unwrap(); diff --git a/components/content/src/sorting.rs b/components/content/src/sorting.rs index d4369488..92b37ba5 100644 --- a/components/content/src/sorting.rs +++ b/components/content/src/sorting.rs @@ -75,7 +75,7 @@ mod tests { let page1 = create_page_with_date("2018-01-01", None); let page2 = create_page_with_date("2017-01-01", None); let page3 = create_page_with_date("2019-01-01", None); - let (pages, ignored_pages) = sort_pages(&vec![&page1, &page2, &page3], SortBy::Date); + let (pages, ignored_pages) = sort_pages(&[&page1, &page2, &page3], SortBy::Date); assert_eq!(pages[0], page3.file.path); assert_eq!(pages[1], page1.file.path); assert_eq!(pages[2], page2.file.path); @@ -87,7 +87,7 @@ mod tests { let page1 = create_page_with_date("2018-01-01", None); let page2 = create_page_with_date("2017-01-01", Some("2022-02-01")); let page3 = create_page_with_date("2019-01-01", None); - let (pages, ignored_pages) = sort_pages(&vec![&page1, &page2, &page3], SortBy::UpdateDate); + let (pages, ignored_pages) = sort_pages(&[&page1, &page2, &page3], SortBy::UpdateDate); assert_eq!(pages[0], page2.file.path); assert_eq!(pages[1], page3.file.path); assert_eq!(pages[2], page1.file.path); @@ -99,7 +99,7 @@ mod tests { let page1 = create_page_with_weight(2); let page2 = create_page_with_weight(3); let page3 = create_page_with_weight(1); - let (pages, ignored_pages) = sort_pages(&vec![&page1, &page2, &page3], SortBy::Weight); + let (pages, ignored_pages) = sort_pages(&[&page1, &page2, &page3], SortBy::Weight); // Should be sorted by weight assert_eq!(pages[0], page3.file.path); assert_eq!(pages[1], page1.file.path); @@ -123,7 +123,7 @@ mod tests { ]; let pages: Vec = titles.iter().map(|title| create_page_with_title(title)).collect(); let (sorted_pages, ignored_pages) = - sort_pages(&pages.iter().map(|p| p).collect::>(), SortBy::Title); + sort_pages(&pages.iter().collect::>(), SortBy::Title); // Should be sorted by title in lexical order let sorted_titles: Vec<_> = sorted_pages .iter() @@ -153,7 +153,7 @@ mod tests { fn can_find_ignored_pages() { let page1 = create_page_with_date("2018-01-01", None); let page2 = create_page_with_weight(1); - let (pages, ignored_pages) = sort_pages(&vec![&page1, &page2], SortBy::Date); + let (pages, ignored_pages) = sort_pages(&[&page1, &page2], SortBy::Date); assert_eq!(pages[0], page1.file.path); assert_eq!(ignored_pages.len(), 1); assert_eq!(ignored_pages[0], page2.file.path); diff --git a/components/content/src/utils.rs b/components/content/src/utils.rs index 3153571d..1b3614aa 100644 --- a/components/content/src/utils.rs +++ b/components/content/src/utils.rs @@ -83,18 +83,10 @@ mod tests { let assets = find_related_assets(path, &Config::default(), true); assert_eq!(assets.len(), 5); - assert_eq!( - assets.iter().filter(|p| p.extension().unwrap_or("".as_ref()) != "md").count(), - 5 - ); + assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 5); - for asset in - vec!["example.js", "graph.jpg", "fail.png", "subdir/example.js", "extensionless"] - { - assert!(assets - .iter() - .find(|p| p.strip_prefix(path).unwrap() == Path::new(asset)) - .is_some()) + for asset in ["example.js", "graph.jpg", "fail.png", "subdir/example.js", "extensionless"] { + assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset))) } } @@ -112,16 +104,10 @@ mod tests { File::create(path.join("subdir").join("example.js")).unwrap(); let assets = find_related_assets(path, &Config::default(), false); assert_eq!(assets.len(), 4); - assert_eq!( - assets.iter().filter(|p| p.extension().unwrap_or("".as_ref()) != "md").count(), - 4 - ); + assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 4); - for asset in vec!["example.js", "graph.jpg", "fail.png", "extensionless"] { - assert!(assets - .iter() - .find(|p| p.strip_prefix(path).unwrap() == Path::new(asset)) - .is_some()) + for asset in ["example.js", "graph.jpg", "fail.png", "extensionless"] { + assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset))) } } #[test] diff --git a/components/utils/src/fs.rs b/components/utils/src/fs.rs index afd6e44a..e6d30a14 100644 --- a/components/utils/src/fs.rs +++ b/components/utils/src/fs.rs @@ -186,7 +186,7 @@ mod tests { let src_file_path = src_dir.path().join("test.txt"); let dest_file_path = dest_dir.path().join(src_file_path.strip_prefix(&base_path).unwrap()); File::create(&src_file_path).unwrap(); - copy_file(&src_file_path, &dest_dir.path().to_path_buf(), &base_path, false).unwrap(); + copy_file(&src_file_path, dest_dir.path(), &base_path, false).unwrap(); assert_eq!( metadata(&src_file_path).and_then(|m| m.modified()).unwrap(), @@ -207,7 +207,7 @@ mod tests { let mut src_file = File::create(&src_file_path).unwrap(); src_file.write_all(b"file1").unwrap(); } - copy_file(&src_file_path, &dest_dir.path().to_path_buf(), &base_path, false).unwrap(); + copy_file(&src_file_path, dest_dir.path(), &base_path, false).unwrap(); { let mut dest_file = File::create(&dest_file_path).unwrap(); dest_file.write_all(b"file2").unwrap(); @@ -217,14 +217,14 @@ mod tests { filetime::set_file_mtime(&src_file_path, filetime::FileTime::from_unix_time(0, 0)).unwrap(); filetime::set_file_mtime(&dest_file_path, filetime::FileTime::from_unix_time(0, 0)) .unwrap(); - copy_file(&src_file_path, &dest_dir.path().to_path_buf(), &base_path, false).unwrap(); + copy_file(&src_file_path, dest_dir.path(), &base_path, false).unwrap(); assert_eq!(read_to_string(&src_file_path).unwrap(), "file1"); assert_eq!(read_to_string(&dest_file_path).unwrap(), "file2"); // Copy occurs if the timestamps are different while the filesizes are same. filetime::set_file_mtime(&dest_file_path, filetime::FileTime::from_unix_time(42, 42)) .unwrap(); - copy_file(&src_file_path, &dest_dir.path().to_path_buf(), &base_path, false).unwrap(); + copy_file(&src_file_path, dest_dir.path(), &base_path, false).unwrap(); assert_eq!(read_to_string(&src_file_path).unwrap(), "file1"); assert_eq!(read_to_string(&dest_file_path).unwrap(), "file1"); @@ -235,7 +235,7 @@ mod tests { } filetime::set_file_mtime(&dest_file_path, filetime::FileTime::from_unix_time(0, 0)) .unwrap(); - copy_file(&src_file_path, &dest_dir.path().to_path_buf(), &base_path, false).unwrap(); + copy_file(&src_file_path, dest_dir.path(), &base_path, false).unwrap(); assert_eq!(read_to_string(&src_file_path).unwrap(), "file1"); assert_eq!(read_to_string(&dest_file_path).unwrap(), "file1"); } diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index 25d508e8..62188a30 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -178,8 +178,8 @@ mod tests { #[test] fn template_fallback_is_successful() { let mut tera = Tera::parse("test-templates/*.html").unwrap(); - tera.add_raw_template(&"hyde/templates/index.html", "Hello").unwrap(); - tera.add_raw_template(&"hyde/templates/theme-only.html", "Hello").unwrap(); + tera.add_raw_template("hyde/templates/index.html", "Hello").unwrap(); + tera.add_raw_template("hyde/templates/theme-only.html", "Hello").unwrap(); // Check finding existing template assert_eq!(check_template_fallbacks("index.html", &tera, &None), Some("index.html")); diff --git a/src/main.rs b/src/main.rs index 6710020f..22fa101c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ mod prompt; fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) { let root_dir = dir .ancestors() - .find_map(|a| if a.join(&config_path).exists() { Some(a) } else { None }) + .find(|a| a.join(&config_path).exists()) .unwrap_or_else(|| panic!("could not find directory containing config file")); // if we got here we found root_dir so config file should exist so we can unwrap safely