fix: Applied clippy warnings (#2153)

* Applied changes suggested by clippy

In general:

1. Prefer std::path::Path over std::path::PathBuf
2. Simplified code with string formating...

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>

* Fixed test

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>

* Fixed formatting

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>
This commit is contained in:
Hanif Ariffin 2021-01-16 20:26:52 +08:00 committed by GitHub
parent fb0381f7e0
commit 5722b17f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 22 deletions

View File

@ -225,7 +225,7 @@ impl DirContents {
Self::from_path_with_timeout(base, Duration::from_secs(30)) Self::from_path_with_timeout(base, Duration::from_secs(30))
} }
fn from_path_with_timeout(base: &PathBuf, timeout: Duration) -> Result<Self, std::io::Error> { fn from_path_with_timeout(base: &Path, timeout: Duration) -> Result<Self, std::io::Error> {
let start = Instant::now(); let start = Instant::now();
let mut folders: HashSet<PathBuf> = HashSet::new(); let mut folders: HashSet<PathBuf> = HashSet::new();

View File

@ -14,7 +14,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
enum ShellEditMode { enum ShellEditMode {
Normal, Normal,
Insert, Insert,
}; }
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert; const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
// TODO: extend config to more modes // TODO: extend config to more modes

View File

@ -299,7 +299,7 @@ fn get_latest_sdk_from_cli() -> Option<Version> {
fn parse_failed<T>() -> Option<T> { fn parse_failed<T>() -> Option<T> {
log::warn!("Unable to parse the output from `dotnet --list-sdks`."); log::warn!("Unable to parse the output from `dotnet --list-sdks`.");
None None
}; }
let latest_sdk = sdks_output let latest_sdk = sdks_output
.stdout .stdout
.lines() .lines()

View File

@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, Error, ErrorKind}; use std::io::{BufRead, BufReader, Error, ErrorKind};
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::{collections::HashMap, path::Path};
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
@ -14,7 +14,7 @@ type Project = String;
type Region = String; type Region = String;
type Active = String; type Active = String;
fn get_gcloud_account_from_config(current_config: &PathBuf) -> Option<Account> { fn get_gcloud_account_from_config(current_config: &Path) -> Option<Account> {
let file = File::open(&current_config).ok()?; let file = File::open(&current_config).ok()?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let lines = reader.lines().filter_map(Result::ok); let lines = reader.lines().filter_map(Result::ok);
@ -27,7 +27,7 @@ fn get_gcloud_account_from_config(current_config: &PathBuf) -> Option<Account> {
Some(account.to_string()) Some(account.to_string())
} }
fn get_gcloud_project_from_config(current_config: &PathBuf) -> Option<Project> { fn get_gcloud_project_from_config(current_config: &Path) -> Option<Project> {
let file = File::open(&current_config).ok()?; let file = File::open(&current_config).ok()?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let lines = reader.lines().filter_map(Result::ok); let lines = reader.lines().filter_map(Result::ok);
@ -40,7 +40,7 @@ fn get_gcloud_project_from_config(current_config: &PathBuf) -> Option<Project> {
Some(project.to_string()) Some(project.to_string())
} }
fn get_gcloud_region_from_config(current_config: &PathBuf) -> Option<Region> { fn get_gcloud_region_from_config(current_config: &Path) -> Option<Region> {
let file = File::open(&current_config).ok()?; let file = File::open(&current_config).ok()?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let lines = reader.lines().filter_map(Result::ok); let lines = reader.lines().filter_map(Result::ok);
@ -53,7 +53,7 @@ fn get_gcloud_region_from_config(current_config: &PathBuf) -> Option<Region> {
Some(region.to_string()) Some(region.to_string())
} }
fn get_active_config(context: &Context, config_root: &PathBuf) -> Option<String> { fn get_active_config(context: &Context, config_root: &Path) -> Option<String> {
let config_name = context.get_env("CLOUDSDK_ACTIVE_CONFIG_NAME").or_else(|| { let config_name = context.get_env("CLOUDSDK_ACTIVE_CONFIG_NAME").or_else(|| {
let path = config_root.join("active_config"); let path = config_root.join("active_config");
let file = File::open(&path).ok()?; let file = File::open(&path).ok()?;

View File

@ -1,5 +1,5 @@
use git2::RepositoryState; use git2::RepositoryState;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::git_state::GitStateConfig; use crate::configs::git_state::GitStateConfig;
@ -53,7 +53,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
/// During a git operation it will show: REBASING, BISECTING, MERGING, etc. /// During a git operation it will show: REBASING, BISECTING, MERGING, etc.
fn get_state_description<'a>( fn get_state_description<'a>(
state: RepositoryState, state: RepositoryState,
root: &'a std::path::PathBuf, root: &'a Path,
config: &GitStateConfig<'a>, config: &GitStateConfig<'a>,
) -> Option<StateDescription<'a>> { ) -> Option<StateDescription<'a>> {
match state { match state {
@ -104,7 +104,7 @@ fn get_state_description<'a>(
} }
} }
fn describe_rebase<'a>(root: &'a PathBuf, rebase_config: &'a str) -> StateDescription<'a> { fn describe_rebase<'a>(root: &'a Path, rebase_config: &'a str) -> StateDescription<'a> {
/* /*
* Sadly, libgit2 seems to have some issues with reading the state of * Sadly, libgit2 seems to have some issues with reading the state of
* interactive rebases. So, instead, we'll poke a few of the .git files * interactive rebases. So, instead, we'll poke a few of the .git files

View File

@ -8,7 +8,7 @@ use regex::Regex;
use semver::Version; use semver::Version;
use semver::VersionReq; use semver::VersionReq;
use serde_json as json; use serde_json as json;
use std::path::PathBuf; use std::path::Path;
/// Creates a module with the current Node.js version /// Creates a module with the current Node.js version
/// ///
@ -73,7 +73,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn get_engines_version(base_dir: &PathBuf) -> Option<String> { fn get_engines_version(base_dir: &Path) -> Option<String> {
let json_str = utils::read_file(base_dir.join("package.json")).ok()?; let json_str = utils::read_file(base_dir.join("package.json")).ok()?;
let package_json: json::Value = json::from_str(&json_str).ok()?; let package_json: json::Value = json::from_str(&json_str).ok()?;
let raw_version = package_json.get("engines")?.get("node")?.as_str()?; let raw_version = package_json.get("engines")?.get("node")?.as_str()?;

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::Path;
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::package::PackageConfig; use crate::configs::package::PackageConfig;
@ -172,7 +172,7 @@ fn extract_meson_version(file_contents: &str) -> Option<String> {
Some(formatted_version) Some(formatted_version)
} }
fn get_package_version(base_dir: &PathBuf, config: &PackageConfig) -> Option<String> { fn get_package_version(base_dir: &Path, config: &PackageConfig) -> Option<String> {
if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) { if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) {
extract_cargo_version(&cargo_toml) extract_cargo_version(&cargo_toml)
} else if let Ok(package_json) = utils::read_file(base_dir.join("package.json")) { } else if let Ok(package_json) = utils::read_file(base_dir.join("package.json")) {

View File

@ -42,7 +42,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"version" => format_php_version(&php_cmd_output.stdout).map(Ok), "version" => Some(Ok(format_php_version(&php_cmd_output.stdout))),
_ => None, _ => None,
}) })
.parse(None) .parse(None)
@ -62,11 +62,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
} }
fn format_php_version(php_version: &str) -> Option<String> { fn format_php_version(php_version: &str) -> String {
let mut formatted_version = String::with_capacity(php_version.len() + 1); format!("v{}", php_version)
formatted_version.push('v');
formatted_version.push_str(php_version);
Some(formatted_version)
} }
#[cfg(test)] #[cfg(test)]
@ -80,7 +77,7 @@ mod tests {
#[test] #[test]
fn test_format_php_version() { fn test_format_php_version() {
let input = "7.3.8"; let input = "7.3.8";
assert_eq!(format_php_version(input), Some("v7.3.8".to_string())); assert_eq!(format_php_version(input), "v7.3.8".to_string());
} }
#[test] #[test]