diff --git a/src/main.rs b/src/main.rs index 2c6c1b68aa..1ef8f30db2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::process::ExitCode; use std::sync::mpsc::channel; use std::time::Instant; @@ -183,9 +183,7 @@ fn inner_main() -> Result { set_up_logging(cli.verbose)?; - // TODO(charlie): Can we avoid this cast? - let paths: Vec<&Path> = cli.files.iter().map(PathBuf::as_path).collect(); - let mut settings = Settings::from_paths(paths)?; + let mut settings = Settings::from_paths(&cli.files); if !cli.select.is_empty() { settings.select(cli.select); } diff --git a/src/pyproject.rs b/src/pyproject.rs index 29643936a6..ee39f39f55 100644 --- a/src/pyproject.rs +++ b/src/pyproject.rs @@ -8,7 +8,7 @@ use serde::Deserialize; use crate::checks::CheckCode; use crate::fs; -pub fn load_config<'a>(paths: impl IntoIterator) -> Result<(PathBuf, Config)> { +pub fn load_config(paths: &[PathBuf]) -> (PathBuf, Config) { match find_project_root(paths) { Some(project_root) => match find_pyproject_toml(&project_root) { Some(path) => { @@ -19,18 +19,18 @@ pub fn load_config<'a>(paths: impl IntoIterator) -> Result<(Pat .tool .and_then(|tool| tool.ruff) .unwrap_or_default(); - Ok((project_root, config)) + (project_root, config) } Err(e) => { println!("Failed to load pyproject.toml: {:?}", e); println!("Falling back to default configuration..."); - Ok(Default::default()) + Default::default() } } } - None => Ok(Default::default()), + None => Default::default(), }, - None => Ok(Default::default()), + None => Default::default(), } } @@ -70,8 +70,8 @@ fn find_user_pyproject_toml() -> Option { dirs::home_dir().map(|path| path.join(".ruff")) } -fn find_project_root<'a>(sources: impl IntoIterator) -> Option { - if let Some(prefix) = common_path_all(sources) { +fn find_project_root(sources: &[PathBuf]) -> Option { + if let Some(prefix) = common_path_all(sources.iter().map(PathBuf::as_path)) { for directory in prefix.ancestors() { if directory.join(".git").is_dir() { return Some(directory.to_path_buf()); @@ -90,7 +90,7 @@ fn find_project_root<'a>(sources: impl IntoIterator) -> Option< #[cfg(test)] mod tests { - use std::path::Path; + use std::path::{Path, PathBuf}; use anyhow::Result; @@ -238,8 +238,9 @@ other-attribute = 1 #[test] fn find_and_parse_pyproject_toml() -> Result<()> { - let project_root = find_project_root([Path::new("resources/test/fixtures/__init__.py")]) - .expect("Unable to find project root."); + let project_root = + find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")]) + .expect("Unable to find project root."); assert_eq!(project_root, Path::new("resources/test/fixtures")); let path = find_pyproject_toml(&project_root).expect("Unable to find pyproject.toml."); diff --git a/src/settings.rs b/src/settings.rs index e21cd39b15..efcf6cb152 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,8 +1,7 @@ use std::collections::BTreeSet; use std::hash::{Hash, Hasher}; -use std::path::Path; +use std::path::PathBuf; -use anyhow::Result; use glob::Pattern; use crate::checks::CheckCode; @@ -25,8 +24,8 @@ impl Hash for Settings { } impl Settings { - pub fn from_paths<'a>(paths: impl IntoIterator) -> Result { - let (project_root, config) = load_config(paths)?; + pub fn from_paths(paths: &[PathBuf]) -> Self { + let (project_root, config) = load_config(paths); let mut settings = Settings { line_length: config.line_length.unwrap_or(88), exclude: config @@ -89,7 +88,7 @@ impl Settings { if let Some(ignore) = &config.ignore { settings.ignore(ignore); } - Ok(settings) + settings } pub fn select(&mut self, codes: Vec) {