From f40f73dc8e26e6d8730344f4c6cbb47ee3d48342 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 31 Jul 2019 19:48:51 -0400 Subject: [PATCH] fix: Address longstanding linter errors --- ci/cargo-clippy.yml | 2 +- src/config.rs | 36 +++++++++++++++--------------------- src/context.rs | 20 +++++++++----------- src/init.rs | 3 +-- src/module.rs | 32 ++++++++++++++++---------------- src/modules/directory.rs | 8 ++++---- src/modules/git_branch.rs | 4 ++-- src/modules/git_status.rs | 8 ++++---- src/modules/golang.rs | 6 +++--- src/modules/package.rs | 4 ++-- src/modules/python.rs | 6 +++--- src/modules/username.rs | 4 ++-- src/segment.rs | 8 ++++---- 13 files changed, 66 insertions(+), 75 deletions(-) diff --git a/ci/cargo-clippy.yml b/ci/cargo-clippy.yml index 76680ceb..154acfe2 100644 --- a/ci/cargo-clippy.yml +++ b/ci/cargo-clippy.yml @@ -9,5 +9,5 @@ jobs: steps: - template: install-rust.yml - - script: cargo clippy -- -D warnings -D clippy::all -D clippy::nursery -D clippy::pedantic + - script: cargo clippy -- -D clippy::all displayName: Run clippy diff --git a/src/config.rs b/src/config.rs index 92073b5f..ba339468 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,30 +21,27 @@ pub trait Config { impl Config for Table { /// Initialize the Config struct fn initialize() -> Table { - if let Some(file_data) = Table::config_from_file() { + if let Some(file_data) = Self::config_from_file() { return file_data; } - Table::new() + Self::new() } /// Create a config from a starship configuration file fn config_from_file() -> Option { - let file_path = match env::var("STARSHIP_CONFIG") { - Ok(path) => { - // Use $STARSHIP_CONFIG as the config path if available - log::debug!("STARSHIP_CONFIG is set: \n{}", &path); - path - } - Err(_) => { - // Default to using ~/.config/starhip.toml - log::debug!("STARSHIP_CONFIG is not set"); - let config_path = home_dir()?.join(".config/starship.toml"); - let config_path_str = config_path.to_str()?.to_owned(); + let file_path = if let Ok(path) = env::var("STARSHIP_CONFIG") { + // Use $STARSHIP_CONFIG as the config path if available + log::debug!("STARSHIP_CONFIG is set: \n{}", &path); + path + } else { + // Default to using ~/.config/starhip.toml + log::debug!("STARSHIP_CONFIG is not set"); + let config_path = home_dir()?.join(".config/starship.toml"); + let config_path_str = config_path.to_str()?.to_owned(); - log::debug!("Using default config path: {}", config_path_str); - config_path_str - } + log::debug!("Using default config path: {}", config_path_str); + config_path_str }; let toml_content = match utils::read_file(&file_path) { @@ -65,10 +62,7 @@ impl Config for Table { /// Get the subset of the table for a module by its name fn get_module_config(&self, module_name: &str) -> Option<&toml::value::Table> { - let module_config = self - .get(module_name) - .map(toml::Value::as_table) - .unwrap_or(None); + let module_config = self.get(module_name).and_then(toml::Value::as_table); if module_config.is_some() { log::debug!( @@ -149,7 +143,7 @@ impl Config for Table { } } - +#[cfg(test)] mod tests { use super::*; diff --git a/src/context.rs b/src/context.rs index 3e3640ac..1cfbaa3a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -70,10 +70,10 @@ impl<'a> Context<'a> { let repository = Repository::discover(¤t_dir).ok(); let repo_root = repository .as_ref() - .and_then(|repo| repo.workdir().map(|repo| repo.to_path_buf())); + .and_then(|repo| repo.workdir().map(std::path::Path::to_path_buf)); let branch_name = repository .as_ref() - .and_then(|repo| get_current_branch(&repo)); + .and_then(|repo| get_current_branch(repo)); Context { config, @@ -102,9 +102,7 @@ impl<'a> Context<'a> { let config = self.config.get_module_config(name); // If the segment has "disabled" set to "true", don't show it - let disabled = config - .map(|table| table.get_as_bool("disabled")) - .unwrap_or(None); + let disabled = config.and_then(|table| table.get_as_bool("disabled")); if disabled == Some(true) { return None; @@ -135,17 +133,17 @@ pub struct ScanDir<'a> { } impl<'a> ScanDir<'a> { - pub fn set_files(mut self, files: &'a [&'a str]) -> Self { + pub const fn set_files(mut self, files: &'a [&'a str]) -> Self { self.files = files; self } - pub fn set_extensions(mut self, extensions: &'a [&'a str]) -> Self { + pub const fn set_extensions(mut self, extensions: &'a [&'a str]) -> Self { self.extensions = extensions; self } - pub fn set_folders(mut self, folders: &'a [&'a str]) -> Self { + pub const fn set_folders(mut self, folders: &'a [&'a str]) -> Self { self.folders = folders; self } @@ -155,9 +153,9 @@ impl<'a> ScanDir<'a> { pub fn scan(&mut self) -> bool { self.dir_files.iter().any(|path| { if path.is_dir() { - return path_has_name(&path, &self.folders); + path_has_name(path, self.folders) } else { - return path_has_name(&path, &self.files) || has_extension(&path, &self.extensions); + path_has_name(path, self.files) || has_extension(path, self.extensions) } }) } @@ -199,7 +197,7 @@ fn get_current_branch(repository: &Repository) -> Option { let head = repository.head().ok()?; let shorthand = head.shorthand(); - shorthand.map(|branch| branch.to_string()) + shorthand.map(std::string::ToString::to_string) } #[cfg(test)] diff --git a/src/init.rs b/src/init.rs index 5db23c2b..396ba72d 100644 --- a/src/init.rs +++ b/src/init.rs @@ -54,8 +54,7 @@ pub fn init(shell_name: &str) { } }; - if setup_script.is_some() { - let script = setup_script.unwrap(); + if let Some(script) = setup_script { print!("{}", script); } } diff --git a/src/module.rs b/src/module.rs index 526345b9..ade5d9e5 100644 --- a/src/module.rs +++ b/src/module.rs @@ -17,13 +17,13 @@ pub struct Module<'a> { style: Style, /// The prefix used to separate the current module from the previous one. - prefix: ModuleAffix, + prefix: Affix, /// The collection of segments that compose this module. segments: Vec, /// The suffix used to separate the current module from the next one. - suffix: ModuleAffix, + suffix: Affix, } impl<'a> Module<'a> { @@ -33,9 +33,9 @@ impl<'a> Module<'a> { config, name: name.to_string(), style: Style::default(), - prefix: ModuleAffix::default_prefix(name.to_string()), + prefix: Affix::default_prefix(name), segments: Vec::new(), - suffix: ModuleAffix::default_suffix(name.to_string()), + suffix: Affix::default_suffix(name), } } @@ -56,12 +56,12 @@ impl<'a> Module<'a> { } /// Get the module's prefix - pub fn get_prefix(&mut self) -> &mut ModuleAffix { + pub fn get_prefix(&mut self) -> &mut Affix { &mut self.prefix } /// Get the module's suffix - pub fn get_suffix(&mut self) -> &mut ModuleAffix { + pub fn get_suffix(&mut self) -> &mut Affix { &mut self.suffix } @@ -82,7 +82,7 @@ impl<'a> Module<'a> { let mut ansi_strings = self .segments .iter() - .map(|s| s.ansi_string()) + .map(Segment::ansi_string) .collect::>(); ansi_strings.insert(0, self.prefix.ansi_string()); @@ -119,7 +119,7 @@ impl<'a> fmt::Display for Module<'a> { } /// Module affixes are to be used for the prefix or suffix of a module. -pub struct ModuleAffix { +pub struct Affix { /// The affix's name, to be used in configuration and logging. name: String, @@ -130,17 +130,17 @@ pub struct ModuleAffix { value: String, } -impl ModuleAffix { - pub fn default_prefix(name: String) -> ModuleAffix { - ModuleAffix { +impl Affix { + pub fn default_prefix(name: &str) -> Self { + Self { name: format!("{}_prefix", name), style: Style::default(), value: "via ".to_string(), } } - pub fn default_suffix(name: String) -> ModuleAffix { - ModuleAffix { + pub fn default_suffix(name: &str) -> Self { + Self { name: format!("{}_suffix", name), style: Style::default(), value: " ".to_string(), @@ -150,7 +150,7 @@ impl ModuleAffix { /// Sets the style of the module. /// /// Accepts either `Color` or `Style`. - pub fn set_style(&mut self, style: T) -> &mut ModuleAffix + pub fn set_style(&mut self, style: T) -> &mut Self where T: Into