chore: Avoid `collect`

This commit is contained in:
Dmitry Dygalo 2022-09-14 10:32:30 +02:00
parent b7e2a4b9a9
commit a711569c2c
No known key found for this signature in database
GPG Key ID: 0D78E60518FE18BB
3 changed files with 16 additions and 17 deletions

View File

@ -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<ExitCode> {
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);
}

View File

@ -8,7 +8,7 @@ use serde::Deserialize;
use crate::checks::CheckCode;
use crate::fs;
pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> 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<Item = &'a Path>) -> 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<PathBuf> {
dirs::home_dir().map(|path| path.join(".ruff"))
}
fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<PathBuf> {
if let Some(prefix) = common_path_all(sources) {
fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> {
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<Item = &'a Path>) -> 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.");

View File

@ -1,6 +1,6 @@
use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::path::PathBuf;
use anyhow::Result;
use glob::Pattern;
@ -25,8 +25,8 @@ impl Hash for Settings {
}
impl Settings {
pub fn from_paths<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<Self> {
let (project_root, config) = load_config(paths)?;
pub fn from_paths(paths: &[PathBuf]) -> Result<Self> {
let (project_root, config) = load_config(paths);
let mut settings = Settings {
line_length: config.line_length.unwrap_or(88),
exclude: config