From 5e123fcbcea4f813808380bc285eee5c73b1c84b Mon Sep 17 00:00:00 2001 From: David Knaack Date: Fri, 27 Jan 2023 10:33:24 +0100 Subject: [PATCH] chore: fix clippy warnings for rust 1.67 (#4855) --- src/configure.rs | 8 ++------ src/context.rs | 6 +++--- src/logger.rs | 5 ++--- src/modules/cmd_duration.rs | 8 ++++---- src/modules/env_var.rs | 4 ++-- src/modules/git_status.rs | 7 ++++--- src/modules/hg_branch.rs | 2 +- src/modules/nix_shell.rs | 4 ++-- src/modules/nodejs.rs | 10 ++++------ src/modules/time.rs | 5 ++--- src/print.rs | 2 +- 11 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/configure.rs b/src/configure.rs index 3cb209f6..d266e0b7 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -130,9 +130,7 @@ pub fn print_configuration(use_default: bool, paths: &[String]) { fn extract_toml_paths(mut config: toml::Value, paths: &[String]) -> toml::Value { // Extract all the requested sections into a new configuration. let mut subset = toml::value::Table::new(); - let config = if let Some(config) = config.as_table_mut() { - config - } else { + let Some(config) = config.as_table_mut() else { // This function doesn't make any sense if the root is not a table. return toml::Value::Table(subset); }; @@ -156,9 +154,7 @@ fn extract_toml_paths(mut config: toml::Value, paths: &[String]) -> toml::Value } // Extract the value to move. - let value = if let Some(value) = source_cursor.remove(end) { - value - } else { + let Some(value) = source_cursor.remove(end) else { // We didn't find a value for this path, so move on to the next path. continue 'paths; }; diff --git a/src/context.rs b/src/context.rs index 0a20ea7c..a17f9dd0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -250,9 +250,9 @@ impl<'a> Context<'a> { } /// Will lazily get repo root and branch when a module requests it. - pub fn get_repo(&self) -> Result<&Repo, git::discover::Error> { + pub fn get_repo(&self) -> Result<&Repo, Box> { self.repo - .get_or_try_init(|| -> Result { + .get_or_try_init(|| -> Result> { // custom open options let mut git_open_opts_map = git_sec::trust::Mapping::::default(); @@ -286,7 +286,7 @@ impl<'a> Context<'a> { Ok(repo) => repo, Err(e) => { log::debug!("Failed to find git repo: {e}"); - return Err(e); + return Err(Box::new(e)); } }; diff --git a/src/logger.rs b/src/logger.rs index 7e86c10b..9ab75cb1 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -35,10 +35,9 @@ pub fn get_log_dir() -> PathBuf { /// Deletes all log files in the log directory that were modified more than 24 hours ago. pub fn cleanup_log_files>(path: P) { let log_dir = path.as_ref(); - let log_files = match fs::read_dir(log_dir) { - Ok(log_files) => log_files, + let Ok(log_files) = fs::read_dir(log_dir) else { // Avoid noisily handling errors in this cleanup function. - Err(_) => return, + return }; for file in log_files { diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 9d42439b..e12126f2 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -52,9 +52,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { } #[cfg(not(feature = "notify"))] -fn undistract_me<'a, 'b>( +fn undistract_me<'a>( module: Module<'a>, - _config: &'b CmdDurationConfig, + _config: &CmdDurationConfig, _context: &'a Context, _elapsed: u128, ) -> Module<'a> { @@ -62,9 +62,9 @@ fn undistract_me<'a, 'b>( } #[cfg(feature = "notify")] -fn undistract_me<'a, 'b>( +fn undistract_me<'a>( module: Module<'a>, - config: &'b CmdDurationConfig, + config: &CmdDurationConfig, context: &'a Context, elapsed: u128, ) -> Module<'a> { diff --git a/src/modules/env_var.rs b/src/modules/env_var.rs index 2ba09f30..2d994f62 100644 --- a/src/modules/env_var.rs +++ b/src/modules/env_var.rs @@ -26,7 +26,7 @@ pub fn module<'a>(name: Option<&str>, context: &'a Context) -> Option }; let mod_name = match name { - Some(name) => format!("env_var.{}", name), + Some(name) => format!("env_var.{name}"), None => "env_var".to_owned(), }; @@ -78,7 +78,7 @@ fn filter_config(config: &toml::Value) -> Option { table .iter() .filter(|(_key, val)| !val.is_table()) - .map(|(key, val)| (key.to_owned(), val.to_owned())) + .map(|(key, val)| (key.clone(), val.clone())) .collect::() }) .filter(|table| !table.is_empty()) diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 92aeb770..10fb0409 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -426,9 +426,10 @@ fn git_status_wsl(context: &Context, conf: &GitStatusConfig) -> Option { // Get foreign starship to use WSL config // https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/ - let wslenv = env::var("WSLENV") - .map(|e| e + ":STARSHIP_CONFIG/wp") - .unwrap_or_else(|_| "STARSHIP_CONFIG/wp".to_string()); + let wslenv = env::var("WSLENV").map_or_else( + |_| "STARSHIP_CONFIG/wp".to_string(), + |e| e + ":STARSHIP_CONFIG/wp", + ); let out = match create_command(starship_exe) .map(|mut c| { diff --git a/src/modules/hg_branch.rs b/src/modules/hg_branch.rs index cb2adecd..259cf3bb 100644 --- a/src/modules/hg_branch.rs +++ b/src/modules/hg_branch.rs @@ -41,7 +41,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let topic_graphemes = if let Ok(topic) = get_hg_topic_name(repo_root) { truncate_text(&topic, len, config.truncation_symbol) } else { - String::from("") + String::new() }; let parsed = StringFormatter::new(config.format).and_then(|formatter| { diff --git a/src/modules/nix_shell.rs b/src/modules/nix_shell.rs index f8974401..7913b95b 100644 --- a/src/modules/nix_shell.rs +++ b/src/modules/nix_shell.rs @@ -12,8 +12,8 @@ enum NixShellType { } impl NixShellType { - fn detect_shell_type(use_heuristic: bool, context: &Context) -> Option { - use NixShellType::*; + fn detect_shell_type(use_heuristic: bool, context: &Context) -> Option { + use NixShellType::{Impure, Pure, Unknown}; let shell_type = context.get_env("IN_NIX_SHELL"); match shell_type.as_deref() { diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 0a4e3c5b..295dc71e 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -93,13 +93,11 @@ fn get_engines_version(context: &Context) -> Option { } fn check_engines_version(nodejs_version: Option<&str>, engines_version: Option) -> bool { - let (nodejs_version, engines_version) = match (nodejs_version, engines_version) { - (Some(nv), Some(ev)) => (nv, ev), - _ => return true, + let (Some(nodejs_version), Some(engines_version)) = (nodejs_version, engines_version) else { + return true; }; - let r = match VersionReq::parse(&engines_version) { - Ok(r) => r, - Err(_e) => return true, + let Ok(r) = VersionReq::parse(&engines_version) else { + return true; }; let re = Regex::new(r"\d+\.\d+\.\d+").unwrap(); let version = re diff --git a/src/modules/time.rs b/src/modules/time.rs index a8874ea1..a5e6277c 100644 --- a/src/modules/time.rs +++ b/src/modules/time.rs @@ -80,9 +80,8 @@ fn create_offset_time_string( ); if utc_time_offset_in_hours < 24_f32 && utc_time_offset_in_hours > -24_f32 { let utc_offset_in_seconds: i32 = (utc_time_offset_in_hours * 3600_f32) as i32; - let timezone_offset = match FixedOffset::east_opt(utc_offset_in_seconds) { - Some(offset) => offset, - None => return Err("Invalid offset"), + let Some(timezone_offset) = FixedOffset::east_opt(utc_offset_in_seconds) else { + return Err("Invalid offset") }; log::trace!("Target timezone offset is {}", timezone_offset); diff --git a/src/print.rs b/src/print.rs index 6ff16d61..f34c8fe5 100644 --- a/src/print.rs +++ b/src/print.rs @@ -334,7 +334,7 @@ fn handle_module<'a>( for (child, config) in context .config .get_config(&[module]) - .and_then(|config| config.as_table().map(|t| t.iter())) + .and_then(|config| config.as_table().map(toml::map::Map::iter)) .into_iter() .flatten() {