From afdb721612f1ec5307bbc0a5eccd186baadb2d44 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 5 Jul 2021 02:03:00 +0200 Subject: [PATCH] Fix lints --- lychee-lib/src/extract.rs | 4 ++-- lychee-lib/src/fs.rs | 43 +++++++++++++---------------------- lychee-lib/src/types/base.rs | 4 +++- lychee-lib/src/types/error.rs | 1 + 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lychee-lib/src/extract.rs b/lychee-lib/src/extract.rs index 039c039..309e63b 100644 --- a/lychee-lib/src/extract.rs +++ b/lychee-lib/src/extract.rs @@ -122,8 +122,8 @@ pub(crate) fn extract_links( } else if let Some(new_url) = base.as_ref().and_then(|u| u.join(&link)) { Request::new(Uri { inner: new_url }, input_content.input.clone()) } else if let Input::FsPath(root) = &input_content.input { - let link = fs::sanitize(link); - if link.starts_with("#") { + let link = fs::sanitize(&link); + if link.starts_with('#') { // Silently ignore anchors for now. continue; } diff --git a/lychee-lib/src/fs.rs b/lychee-lib/src/fs.rs index f7fd6a8..a5c90fb 100644 --- a/lychee-lib/src/fs.rs +++ b/lychee-lib/src/fs.rs @@ -3,7 +3,7 @@ use std::path::{Component, Path, PathBuf}; // Returns the base if it is a valid `PathBuf` fn get_base_dir(base: &Option) -> Option { - base.as_ref().and_then(|b| b.dir()) + base.as_ref().and_then(Base::dir) } /// Normalize a path, removing things like `.` and `..`. @@ -15,15 +15,14 @@ fn get_base_dir(base: &Option) -> Option { /// fail, or on Windows returns annoying device paths. This is a problem Cargo /// needs to improve on. /// -/// Taken from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61 +/// Taken from [`cargo`](https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61) pub(crate) fn normalize(path: &Path) -> PathBuf { let mut components = path.components().peekable(); - let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { + + let mut ret = components.peek().copied().map_or_else(PathBuf::new, |c| { components.next(); PathBuf::from(c.as_os_str()) - } else { - PathBuf::new() - }; + }); for component in components { match component { @@ -54,12 +53,11 @@ pub(crate) fn resolve(src: &Path, dst: &Path, base: &Option) -> Result PathBuf { /// A little helper function to remove the get parameters from a URL link. /// The link is not a URL but a String as that link may not have a base domain. -pub(crate) fn sanitize(link: String) -> String { +pub(crate) fn sanitize(link: &str) -> String { let path = match link.split_once('?') { Some((path, _params)) => path, - None => link.as_str(), + None => link, }; path.to_string() } @@ -92,27 +90,18 @@ mod test_fs_tree { #[test] fn test_sanitize() { - assert_eq!(sanitize("/".to_string()), "/".to_string()); + assert_eq!(sanitize("/"), "/".to_string()); + assert_eq!(sanitize("index.html?foo=bar"), "index.html".to_string()); + assert_eq!(sanitize("/index.html?foo=bar"), "/index.html".to_string()); assert_eq!( - sanitize("index.html?foo=bar".to_string()), - "index.html".to_string() - ); - assert_eq!( - sanitize("/index.html?foo=bar".to_string()), + sanitize("/index.html?foo=bar&baz=zorx?bla=blub"), "/index.html".to_string() ); assert_eq!( - sanitize("/index.html?foo=bar&baz=zorx?bla=blub".to_string()), - "/index.html".to_string() - ); - assert_eq!( - sanitize("https://example.org/index.html?foo=bar".to_string()), + sanitize("https://example.org/index.html?foo=bar"), "https://example.org/index.html".to_string() ); - assert_eq!( - sanitize("test.png?foo=bar".to_string()), - "test.png".to_string() - ); + assert_eq!(sanitize("test.png?foo=bar"), "test.png".to_string()); } // dummy root diff --git a/lychee-lib/src/types/base.rs b/lychee-lib/src/types/base.rs index 8b7f2f8..f38ec29 100644 --- a/lychee-lib/src/types/base.rs +++ b/lychee-lib/src/types/base.rs @@ -18,6 +18,7 @@ pub enum Base { impl Base { /// Join link with base url + #[must_use] pub fn join(&self, link: &str) -> Option { match self { Self::Remote(url) => url.join(link).ok(), @@ -26,10 +27,11 @@ impl Base { } /// Return the directory if the base is local + #[must_use] pub fn dir(&self) -> Option { match self { Self::Remote(_) => None, - Self::Local(d) => Some(d.to_path_buf()), + Self::Local(d) => Some(d.clone()), } } } diff --git a/lychee-lib/src/types/error.rs b/lychee-lib/src/types/error.rs index 60cafa0..7de4608 100644 --- a/lychee-lib/src/types/error.rs +++ b/lychee-lib/src/types/error.rs @@ -76,6 +76,7 @@ impl Hash for ErrorKind { Self::UrlParseError(s, e) => (s, e.type_id()).hash(state), Self::UnreachableEmailAddress(u) | Self::InsecureURL(u) => u.hash(state), Self::InvalidFileUri(u) => u.hash(state), + Self::InvalidFileUri(f) => f.hash(state), Self::InvalidPath(p) => p.hash(state), Self::UnreachableEmailAddress(u) => u.hash(state), Self::InvalidHeader(e) => e.to_string().hash(state),