From 452486a7823d6515ac82b6a473fa1bfc84ef820e Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 2 May 2021 20:40:01 -0700 Subject: [PATCH] Placate clippy type: reform --- .github/workflows/build.yaml | 6 ++-- bin/gen/src/changelog.rs | 5 +-- bin/gen/src/table.rs | 3 +- book/src/changelog.md | 1 - justfile | 2 +- rustfmt.toml | 2 +- src/bench.rs | 4 ++- src/bytes.rs | 2 +- src/file_error.rs | 8 ++--- src/file_path.rs | 6 +--- src/infohash.rs | 6 ++-- src/lib.rs | 14 ++++++++ src/magnet_link.rs | 12 +++---- src/magnet_link_parse_error.rs | 2 +- src/md5_digest.rs | 4 +-- src/shell.rs | 16 ++++----- src/subcommand/torrent/create.rs | 48 +++++++++----------------- src/subcommand/torrent/link.rs | 29 ++++++++-------- src/subcommand/torrent/piece_length.rs | 6 ++-- src/subcommand/torrent/verify.rs | 28 +++++++-------- src/test_env_builder.rs | 9 +++-- src/torrent_summary.rs | 4 +-- src/verifier.rs | 8 ++--- 23 files changed, 106 insertions(+), 119 deletions(-) delete mode 120000 book/src/changelog.md diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 96385b1..eecb73d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -79,13 +79,13 @@ jobs: cargo clippy --version - name: Build - run: cargo build --all --verbose + run: cargo build --all - name: Test - run: cargo test --all --verbose + run: cargo test --all - name: Clippy - run: cargo clippy --all + run: cargo clippy --all-targets --all-features - name: Lint if: matrix.os == 'macos-latest' diff --git a/bin/gen/src/changelog.rs b/bin/gen/src/changelog.rs index a82c4b4..bdcb180 100644 --- a/bin/gen/src/changelog.rs +++ b/bin/gen/src/changelog.rs @@ -99,10 +99,7 @@ impl Changelog { #[throws] pub(crate) fn render(&self, book: bool) -> String { - let mut lines: Vec = Vec::new(); - - lines.push("Changelog".into()); - lines.push("=========".into()); + let mut lines: Vec = vec!["Changelog".into(), "=========".into()]; for release in &self.releases { lines.push("".into()); diff --git a/bin/gen/src/table.rs b/bin/gen/src/table.rs index 924e36c..18d91a8 100644 --- a/bin/gen/src/table.rs +++ b/bin/gen/src/table.rs @@ -12,8 +12,7 @@ impl Table { impl Display for Table { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - let mut rows = Vec::new(); - rows.push(R::header().to_vec()); + let mut rows = vec![R::header().to_vec()]; for row in &self.rows { rows.push(row.entries()); diff --git a/book/src/changelog.md b/book/src/changelog.md deleted file mode 120000 index d4bb88d..0000000 --- a/book/src/changelog.md +++ /dev/null @@ -1 +0,0 @@ -../../target/gen/book/changelog.md \ No newline at end of file diff --git a/justfile b/justfile index 439f714..6e11aa2 100644 --- a/justfile +++ b/justfile @@ -33,7 +33,7 @@ test: cargo test --all clippy: - cargo clippy --all + cargo clippy --all-targets --all-features fmt: cargo +nightly fmt --all diff --git a/rustfmt.toml b/rustfmt.toml index 847db9d..d6bb890 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -5,8 +5,8 @@ error_on_unformatted = true format_code_in_doc_comments = true format_macro_bodies = true format_strings = true +imports_granularity = "Crate" max_width = 100 -merge_imports = true newline_style = "Unix" normalize_comments = true reorder_impl_items = true diff --git a/src/bench.rs b/src/bench.rs index 722de62..a163ea3 100644 --- a/src/bench.rs +++ b/src/bench.rs @@ -1,3 +1,5 @@ +#![allow(clippy::unwrap_used)] + use crate::common::*; use std::io::BufWriter; @@ -39,7 +41,7 @@ impl Bench for HasherBench { while written < TEMPFILE_BYTES { rand::thread_rng().fill_bytes(&mut bytes); - writer.write(&bytes).unwrap(); + writer.write_all(&bytes).unwrap(); written += bytes.len().into_u64(); } diff --git a/src/bytes.rs b/src/bytes.rs index e735ac5..66e12f1 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -214,7 +214,7 @@ mod tests { ("1kib", KI), ("1KiB", KI), ("12kib", 12 * KI), - ("1.5mib", 1 * MI + 512 * KI), + ("1.5mib", MI + 512 * KI), ]; for (text, value) in CASES { diff --git a/src/file_error.rs b/src/file_error.rs index 0403f97..28074e0 100644 --- a/src/file_error.rs +++ b/src/file_error.rs @@ -22,11 +22,11 @@ impl FileError { let metadata = match path.metadata() { Ok(metadata) => metadata, Err(error) => { - if error.kind() == io::ErrorKind::NotFound { - return Err(FileError::Missing); + return Err(if error.kind() == io::ErrorKind::NotFound { + FileError::Missing } else { - return Err(FileError::Io(error)); - } + FileError::Io(error) + }) } }; diff --git a/src/file_path.rs b/src/file_path.rs index 1be1878..fea8e69 100644 --- a/src/file_path.rs +++ b/src/file_path.rs @@ -56,11 +56,7 @@ impl FilePath { #[cfg(test)] pub(crate) fn from_components(components: &[&str]) -> FilePath { - let components: Vec = components - .iter() - .cloned() - .map(|component| component.to_owned()) - .collect(); + let components: Vec = components.iter().cloned().map(ToOwned::to_owned).collect(); assert!(!components.is_empty()); FilePath { components } } diff --git a/src/infohash.rs b/src/infohash.rs index d773f2c..c2d5c73 100644 --- a/src/infohash.rs +++ b/src/infohash.rs @@ -62,9 +62,9 @@ impl From for Infohash { } } -impl Into for Infohash { - fn into(self) -> Sha1Digest { - self.inner +impl From for Sha1Digest { + fn from(infohash: Infohash) -> Sha1Digest { + infohash.inner } } diff --git a/src/lib.rs b/src/lib.rs index 80551de..25b15be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![deny(clippy::all, clippy::pedantic, clippy::restriction)] #![allow( clippy::blanket_clippy_restriction_lints, + clippy::create_dir, clippy::else_if_without_else, clippy::enum_glob_use, clippy::float_arithmetic, @@ -13,6 +14,7 @@ clippy::map_unwrap_or, clippy::missing_docs_in_private_items, clippy::missing_inline_in_public_items, + clippy::module_name_repetitions, clippy::needless_lifetimes, clippy::needless_pass_by_value, clippy::non_ascii_literal, @@ -24,6 +26,18 @@ clippy::wildcard_enum_match_arm, clippy::wildcard_imports )] +#![cfg_attr( + any(test), + allow( + clippy::blacklisted_name, + clippy::expect_fun_call, + clippy::expect_used, + clippy::panic, + clippy::panic_in_result_fn, + clippy::unwrap_in_result, + clippy::unwrap_used + ) +)] #[cfg(test)] #[macro_use] diff --git a/src/magnet_link.rs b/src/magnet_link.rs index 35ed1d7..e7acd1f 100644 --- a/src/magnet_link.rs +++ b/src/magnet_link.rs @@ -86,7 +86,7 @@ impl MagnetLink { } fn parse(text: &str) -> Result { - let url = Url::parse(&text).context(magnet_link_parse_error::URL)?; + let url = Url::parse(&text).context(magnet_link_parse_error::Url)?; if url.scheme() != "magnet" { return Err(MagnetLinkParseError::Scheme { @@ -105,7 +105,7 @@ impl MagnetLink { } let buf = hex::decode(infohash).context(magnet_link_parse_error::HexParse { - text: infohash.to_string(), + text: infohash.to_owned(), })?; link = Some(MagnetLink::with_infohash( @@ -270,7 +270,7 @@ mod tests { assert_matches!(e, Error::MagnetLinkParse { text, - source: MagnetLinkParseError::URL { .. }, + source: MagnetLinkParseError::Url { .. }, } if text == link); } @@ -307,7 +307,7 @@ mod tests { text, source: MagnetLinkParseError::HexParse { text: ih, - source: _, + .. }} if text == link && infohash == ih); } @@ -335,7 +335,7 @@ mod tests { text, source: MagnetLinkParseError::TrackerAddress { text: addr, - source: _, + .. }, } if text == link && addr == bad_addr); } @@ -352,7 +352,7 @@ mod tests { text, source: MagnetLinkParseError::PeerAddress { text: addr, - source: _, + .. } } if text == link && addr == bad_addr ); diff --git a/src/magnet_link_parse_error.rs b/src/magnet_link_parse_error.rs index 9196e9b..27d9a61 100644 --- a/src/magnet_link_parse_error.rs +++ b/src/magnet_link_parse_error.rs @@ -28,5 +28,5 @@ pub(crate) enum MagnetLinkParseError { source: url::ParseError, }, #[snafu(display("Failed to parse URL: {}", source))] - URL { source: url::ParseError }, + Url { source: url::ParseError }, } diff --git a/src/md5_digest.rs b/src/md5_digest.rs index 515345e..f2a64c9 100644 --- a/src/md5_digest.rs +++ b/src/md5_digest.rs @@ -14,9 +14,9 @@ impl Md5Digest { let mut bytes: [u8; 16] = [0; 16]; - for n in 0..16 { + for (n, byte) in bytes.iter_mut().enumerate() { let i = n * 2; - bytes[n] = u8::from_str_radix(&hex[i..i + 2], 16).unwrap(); + *byte = u8::from_str_radix(&hex[i..i + 2], 16).unwrap(); } Self { bytes } diff --git a/src/shell.rs b/src/shell.rs index f43e037..8f6a783 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -44,14 +44,14 @@ impl Shell { } } -impl Into for Shell { - fn into(self) -> clap::Shell { - match self { - Self::Bash => clap::Shell::Bash, - Self::Fish => clap::Shell::Fish, - Self::Zsh => clap::Shell::Zsh, - Self::Powershell => clap::Shell::PowerShell, - Self::Elvish => clap::Shell::Elvish, +impl From for clap::Shell { + fn from(shell: Shell) -> Self { + match shell { + Shell::Bash => clap::Shell::Bash, + Shell::Fish => clap::Shell::Fish, + Shell::Zsh => clap::Shell::Zsh, + Shell::Powershell => clap::Shell::PowerShell, + Shell::Elvish => clap::Shell::Elvish, } } } diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index 49b7634..294c4b5 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -387,7 +387,7 @@ impl Create { let metainfo = Metainfo { comment: self.comment, - encoding: Some(consts::ENCODING_UTF8.to_string()), + encoding: Some(consts::ENCODING_UTF8.to_owned()), announce: self.announce.map(|url| url.to_string()), announce_list: if announce_list.is_empty() { None @@ -1832,7 +1832,7 @@ Content Size 9 bytes assert_matches!( metainfo.info.mode, - Mode::Multiple { files } if files.len() == 0 + Mode::Multiple { files } if files.is_empty() ); assert_eq!(metainfo.info.pieces, PieceList::new()); Ok(()) @@ -2044,7 +2044,7 @@ Content Size 9 bytes } #[test] - fn skip_hidden_attribute_dir_contents() -> Result<()> { + fn skip_hidden_attribute_dir_contents() { let mut env = test_env! { args: [ "torrent", @@ -2065,7 +2065,7 @@ Content Size 9 bytes #[cfg(target_os = "windows")] { env.write("foo/bar/baz", "baz"); - let path = env.resolve("foo/bar")?; + let path = env.resolve("foo/bar").unwrap(); Command::new("attrib") .arg("+h") .arg(&path) @@ -2080,7 +2080,6 @@ Content Size 9 bytes Mode::Multiple { files } if files.is_empty() ); assert_eq!(metainfo.info.pieces, PieceList::new()); - Ok(()) } #[test] @@ -2886,7 +2885,7 @@ Content Size 9 bytes } #[test] - fn create_messages_path() -> Result<()> { + fn create_messages_path() { let mut env = test_env! { args: [ "torrent", @@ -2901,20 +2900,17 @@ Content Size 9 bytes } }; - let want = format!( - "[1/3] \u{1F9FF} Searching `foo` for files…\n[2/3] \u{1F9EE} Hashing pieces…\n[3/3] \ - \u{1F4BE} Writing metainfo to `foo.torrent`…\n\u{2728}\u{2728} Done! \u{2728}\u{2728}\n", - ); + let want = "[1/3] \u{1F9FF} Searching `foo` for files…\n[2/3] \u{1F9EE} Hashing \ + pieces…\n[3/3] \u{1F4BE} Writing metainfo to `foo.torrent`…\n\u{2728}\u{2728} \ + Done! \u{2728}\u{2728}\n"; env.assert_ok(); assert_eq!(env.err(), want); - - Ok(()) } #[test] - fn create_messages_subdir() -> Result<()> { + fn create_messages_subdir() { let mut env = test_env! { args: [ "torrent", @@ -2940,12 +2936,10 @@ Content Size 9 bytes env.assert_ok(); assert_eq!(env.err(), want); - - Ok(()) } #[test] - fn create_messages_dot() -> Result<()> { + fn create_messages_dot() { let mut env = test_env! { args: [ "torrent", @@ -2975,12 +2969,10 @@ Content Size 9 bytes ); assert_eq!(env.err(), want); - - Ok(()) } #[test] - fn create_messages_dot_dot() -> Result<()> { + fn create_messages_dot_dot() { let mut env = test_env! { args: [ "torrent", @@ -3011,12 +3003,10 @@ Content Size 9 bytes ); assert_eq!(env.err(), want); - - Ok(()) } #[test] - fn create_messages_absolute() -> Result<()> { + fn create_messages_absolute() { let dir = TempDir::new().unwrap(); let input = dir.path().join("foo"); @@ -3051,12 +3041,10 @@ Content Size 9 bytes ); assert_eq!(env.err(), want); - - Ok(()) } #[test] - fn create_messages_stdio() -> Result<()> { + fn create_messages_stdio() { let dir = TempDir::new().unwrap(); let input = dir.path().join("foo"); @@ -3097,15 +3085,11 @@ Content Size 9 bytes } ); - let want = format!( - "[1/3] \u{1F9FF} Creating single-file torrent from standard input…\n[2/3] \u{1F9EE} Hashing \ - pieces…\n[3/3] \u{1F4BE} Writing metainfo to standard output…\n\u{2728}\u{2728} Done! \ - \u{2728}\u{2728}\n", - ); + let want = "[1/3] \u{1F9FF} Creating single-file torrent from standard input…\n[2/3] \ + \u{1F9EE} Hashing pieces…\n[3/3] \u{1F4BE} Writing metainfo to standard \ + output…\n\u{2728}\u{2728} Done! \u{2728}\u{2728}\n"; assert_eq!(env.err(), want); - - Ok(()) } #[test] diff --git a/src/subcommand/torrent/link.rs b/src/subcommand/torrent/link.rs index ac13e16..8130687 100644 --- a/src/subcommand/torrent/link.rs +++ b/src/subcommand/torrent/link.rs @@ -158,6 +158,8 @@ mod tests { #[test] fn no_announce_flag() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -172,8 +174,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -184,6 +184,8 @@ mod tests { #[test] fn no_announce_positional() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -197,8 +199,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -209,6 +209,8 @@ mod tests { #[test] fn with_announce() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -226,8 +228,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -241,6 +241,8 @@ mod tests { #[test] fn unique_trackers() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -259,8 +261,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -271,8 +271,11 @@ mod tests { ), ); } + #[test] fn with_peer() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -292,8 +295,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -307,6 +308,8 @@ mod tests { #[test] fn with_indices() { + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -328,8 +331,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( @@ -343,6 +344,8 @@ mod tests { #[test] fn infohash_correct_with_nonstandard_info_dict() { + const INFO: &str = "d1:ai0e6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + let mut env = test_env! { args: [ "torrent", @@ -357,8 +360,6 @@ mod tests { env.assert_ok(); - const INFO: &str = "d1:ai0e6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; - let infohash = Sha1Digest::from_data(INFO.as_bytes()); assert_eq!( diff --git a/src/subcommand/torrent/piece_length.rs b/src/subcommand/torrent/piece_length.rs index 073af49..7d3e45b 100644 --- a/src/subcommand/torrent/piece_length.rs +++ b/src/subcommand/torrent/piece_length.rs @@ -11,14 +11,12 @@ pub(crate) struct PieceLength {} #[allow(clippy::unused_self)] impl PieceLength { pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { - let mut rows: Vec<(String, String, String, String)> = Vec::new(); - - rows.push(( + let mut rows: Vec<(String, String, String, String)> = vec![( "Content".into(), "Piece Length".into(), "Count".into(), "Piece List Size".into(), - )); + )]; for i in 14..51 { let content_size = Bytes::from(1u64 << i); diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 68576d9..59972c1 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -462,6 +462,17 @@ mod tests { #[test] fn output_color() -> Result<()> { + fn error(path: &str, message: &str) -> String { + let style = Style::active(); + format!( + "{}{}:{} {}", + style.message().prefix(), + path, + style.message().suffix(), + message, + ) + } + let mut create_env = test_env! { args: [ "torrent", @@ -519,17 +530,6 @@ mod tests { let style = Style::active(); - fn error(path: &str, message: &str) -> String { - let style = Style::active(); - format!( - "{}{}:{} {}", - style.message().prefix(), - path, - style.message().suffix(), - message, - ) - } - let want = [ &format!( "{} \u{1F4BE} {}", @@ -671,10 +671,8 @@ mod tests { verify_env.assert_ok(); - let want = format!( - "[1/2] \u{1F4BE} Loading metainfo from standard input…\n[2/2] \u{1F9EE} Verifying pieces \ - from `foo`…\n\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}\n", - ); + let want = "[1/2] \u{1F4BE} Loading metainfo from standard input…\n[2/2] \u{1F9EE} Verifying \ + pieces from `foo`…\n\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}\n"; assert_eq!(verify_env.err(), want); diff --git a/src/test_env_builder.rs b/src/test_env_builder.rs index 58759ff..ab8aeb5 100644 --- a/src/test_env_builder.rs +++ b/src/test_env_builder.rs @@ -66,11 +66,10 @@ impl TestEnvBuilder { let tempdir = self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap()); - let current_dir = if let Some(current_dir) = self.current_dir { - tempdir.path().join(current_dir) - } else { - tempdir.path().to_owned() - }; + let current_dir = self.current_dir.map_or_else( + || tempdir.path().to_owned(), + |current_dir| tempdir.path().join(current_dir), + ); let out_stream = OutputStream::new( Box::new(out.clone()), diff --git a/src/torrent_summary.rs b/src/torrent_summary.rs index 43366f7..5bd863f 100644 --- a/src/torrent_summary.rs +++ b/src/torrent_summary.rs @@ -177,7 +177,7 @@ impl TorrentSummary { fn torrent_summary_data(&self) -> TorrentSummaryJson { let (file_count, files) = match &self.metainfo.info.mode { - Mode::Single { .. } => (1, vec![self.metainfo.info.name.to_string()]), + Mode::Single { .. } => (1, vec![self.metainfo.info.name.to_owned()]), Mode::Multiple { files } => ( files.len(), files @@ -197,7 +197,7 @@ impl TorrentSummary { }; TorrentSummaryJson { - name: self.metainfo.info.name.to_string(), + name: self.metainfo.info.name.to_owned(), comment: self.metainfo.comment.clone(), creation_date: self.metainfo.creation_date, created_by: self.metainfo.created_by.clone(), diff --git a/src/verifier.rs b/src/verifier.rs index f7affe2..227977a 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -36,17 +36,17 @@ impl<'a> Verifier<'a> { base: &'a Path, progress_bar: Option, ) -> Result { - Self::new(metainfo, base, progress_bar)?.verify_metainfo() + Ok(Self::new(metainfo, base, progress_bar)?.verify_metainfo()) } - fn verify_metainfo(mut self) -> Result { + fn verify_metainfo(mut self) -> Status { match &self.metainfo.info.mode { Mode::Single { length, md5sum } => { self.hash(&self.base).ok(); let error = FileError::verify(&self.base, *length, *md5sum).err(); let pieces = self.finish(); - Ok(Status::single(pieces, error)) + Status::single(pieces, error) } Mode::Multiple { files } => { let mut status = Vec::new(); @@ -65,7 +65,7 @@ impl<'a> Verifier<'a> { let pieces = self.finish(); - Ok(Status::multiple(pieces, status)) + Status::multiple(pieces, status) } } }