From a787d6a964ede1e049f8861c7758c9d9c8a609f3 Mon Sep 17 00:00:00 2001 From: Thomas Gardner Date: Sat, 10 Oct 2020 21:29:27 +1100 Subject: [PATCH] Update clippy restrictions type: changed --- src/bytes.rs | 5 +---- src/env.rs | 29 ++++++++++++++--------------- src/error.rs | 4 +++- src/lib.rs | 2 ++ src/run.rs | 10 +++++++++- src/subcommand/torrent/verify.rs | 11 +++++------ 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index deb4788..e735ac5 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -66,10 +66,7 @@ impl FromStr for Bytes { fn from_str(text: &str) -> Result { #[allow(clippy::trivially_copy_pass_by_ref)] fn is_digit(c: &char) -> bool { - match c { - '0'..='9' | '.' => true, - _ => false, - } + matches!(c, '0'..='9' | '.') } let digits = text.chars().take_while(is_digit).collect::(); diff --git a/src/env.rs b/src/env.rs index 0c5f80f..9b02c04 100644 --- a/src/env.rs +++ b/src/env.rs @@ -9,11 +9,8 @@ pub(crate) struct Env { } impl Env { - pub(crate) fn main() -> Self { - let dir = match env::current_dir() { - Ok(dir) => dir, - Err(error) => panic!("Failed to get current directory: {}", error), - }; + pub(crate) fn main() -> Result { + let dir = env::current_dir().context(error::CurrentDirectoryGet)?; let style = env::var_os("NO_COLOR").is_none() && env::var_os("TERM").as_deref() != Some(OsStr::new("dumb")); @@ -21,30 +18,32 @@ impl Env { let out_stream = OutputStream::stdout(style); let err_stream = OutputStream::stderr(style); - Self::new( + Ok(Self::new( dir, env::args(), Box::new(io::stdin()), out_stream, err_stream, - ) + )) } - pub(crate) fn run(&mut self) -> Result<(), Error> { + pub(crate) fn run(&mut self) -> Result<()> { #[cfg(windows)] ansi_term::enable_ansi_support().ok(); Self::initialize_logging(); - let app = Arguments::clap(); + let app = { + let mut app = Arguments::clap(); - let width = env::var("IMDL_TERM_WIDTH") - .ok() - .and_then(|width| width.parse::().ok()); + let width = env::var("IMDL_TERM_WIDTH") + .ok() + .and_then(|width| width.parse::().ok()); + + if let Some(width) = width { + app = app.set_term_width(width) + } - let app = if let Some(width) = width { - app.set_term_width(width) - } else { app }; diff --git a/src/error.rs b/src/error.rs index e6c2130..219e226 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,10 +14,12 @@ pub(crate) enum Error { ByteSuffix { text: String, suffix: String }, #[snafu(display("{}", source))] Clap { source: clap::Error }, - #[snafu(display("Failed to invoke command `{}`: {}", command, source,))] + #[snafu(display("Failed to invoke command `{}`: {}", command, source))] CommandInvoke { command: String, source: io::Error }, #[snafu(display("Command `{}` returned bad exit status: {}", command, status))] CommandStatus { command: String, status: ExitStatus }, + #[snafu(display("Failed to get current directory: {}", source))] + CurrentDirectoryGet { source: io::Error }, #[snafu(display("Filename was not valid unicode: `{}`", filename.display()))] FilenameDecode { filename: PathBuf }, #[snafu(display("Path had no file name: `{}`", path.display()))] diff --git a/src/lib.rs b/src/lib.rs index b95ad5c..e0fd900 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![deny(clippy::all, clippy::pedantic, clippy::restriction)] #![allow( + clippy::blanket_clippy_restriction_lints, clippy::else_if_without_else, clippy::enum_glob_use, clippy::float_arithmetic, @@ -14,6 +15,7 @@ clippy::missing_inline_in_public_items, clippy::needless_pass_by_value, clippy::non_ascii_literal, + clippy::pattern_type_mismatch, clippy::shadow_reuse, clippy::struct_excessive_bools, clippy::too_many_lines, diff --git a/src/run.rs b/src/run.rs index 575cc86..9a0f2e7 100644 --- a/src/run.rs +++ b/src/run.rs @@ -18,5 +18,13 @@ use crate::common::*; /// be passed to `std::process::exit`, to exit the process and report its /// failure to the system. pub fn run() -> Result<(), i32> { - Env::main().status() + let mut env = match Env::main() { + Ok(env) => env, + Err(err) => { + eprintln!("{}", err); + return Err(EXIT_FAILURE); + } + }; + + env.status() } diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 64e80a3..68576d9 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -64,14 +64,13 @@ impl Verify { let metainfo = Metainfo::from_input(&input)?; - let content = if let Some(content) = &self.content { - content.clone() - } else { - match target { + let content = self.content.as_ref().map_or_else( + || match target { InputTarget::Path(path) => path.join("..").join(&metainfo.info.name).lexiclean(), InputTarget::Stdin => PathBuf::from(&metainfo.info.name), - } - }; + }, + PathBuf::clone, + ); let progress_bar = if env.err().is_styled_term() && !options.quiet { let style = ProgressStyle::default_bar()