diff --git a/src/cli.rs b/src/cli.rs index 14506f4fd7..76da787592 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use anyhow::Result; use clap::{command, Parser}; use regex::Regex; use rustc_hash::FxHashMap; @@ -146,7 +147,7 @@ pub fn extract_log_level(cli: &Cli) -> LogLevel { pub fn collect_per_file_ignores( pairs: Vec, project_root: Option<&PathBuf>, -) -> Vec { +) -> Result> { let mut per_file_ignores: FxHashMap> = FxHashMap::default(); for pair in pairs { per_file_ignores diff --git a/src/fs.rs b/src/fs.rs index 6e02ed3ff2..e487c86056 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -203,7 +203,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "foo", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -211,7 +211,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "bar", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -221,7 +221,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "baz.py", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -229,7 +229,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "foo/bar", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -239,7 +239,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "foo/bar/baz.py", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -249,7 +249,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "foo/bar/*.py", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(is_excluded(file_path, file_basename, exclude.iter())); @@ -259,7 +259,7 @@ mod tests { let exclude = vec![FilePattern::from_user( "baz", Some(&project_root.to_path_buf()), - )]; + )?]; let (file_path, file_basename) = extract_path_names(&path)?; assert!(!is_excluded(file_path, file_basename, exclude.iter())); diff --git a/src/main.rs b/src/main.rs index 59069081c9..61c3b6eb68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,12 +219,12 @@ fn inner_main() -> Result { .exclude .iter() .map(|path| FilePattern::from_user(path, project_root.as_ref())) - .collect(); + .collect::>()?; let extend_exclude: Vec = cli .extend_exclude .iter() .map(|path| FilePattern::from_user(path, project_root.as_ref())) - .collect(); + .collect::>()?; let mut configuration = Configuration::from_pyproject(pyproject.as_ref(), project_root.as_ref())?; @@ -236,7 +236,7 @@ fn inner_main() -> Result { } if !cli.per_file_ignores.is_empty() { configuration.per_file_ignores = - collect_per_file_ignores(cli.per_file_ignores, project_root.as_ref()); + collect_per_file_ignores(cli.per_file_ignores, project_root.as_ref())?; } if !cli.select.is_empty() { configuration.select = cli.select; diff --git a/src/settings/configuration.rs b/src/settings/configuration.rs index f4e9db7606..78522d7df3 100644 --- a/src/settings/configuration.rs +++ b/src/settings/configuration.rs @@ -111,13 +111,14 @@ impl Configuration { .map(|path| FilePattern::from_user(path, project_root)) .collect() }) + .transpose()? .unwrap_or_else(|| DEFAULT_EXCLUDE.clone()), extend_exclude: options .extend_exclude .unwrap_or_default() .iter() .map(|path| FilePattern::from_user(path, project_root)) - .collect(), + .collect::>()?, extend_ignore: options.extend_ignore.unwrap_or_default(), select: options .select @@ -159,6 +160,7 @@ impl Configuration { }) .collect() }) + .transpose()? .unwrap_or_default(), show_source: options.show_source.unwrap_or_default(), // Plugins diff --git a/src/settings/types.rs b/src/settings/types.rs index 2b79269f06..cde000a9c2 100644 --- a/src/settings/types.rs +++ b/src/settings/types.rs @@ -51,21 +51,21 @@ pub enum FilePattern { } impl FilePattern { - pub fn from_user(pattern: &str, project_root: Option<&PathBuf>) -> Self { + pub fn from_user(pattern: &str, project_root: Option<&PathBuf>) -> Result { let path = Path::new(pattern); let absolute_path = match project_root { Some(project_root) => fs::normalize_path_to(path, project_root), None => fs::normalize_path(path), }; - let absolute = Pattern::new(&absolute_path.to_string_lossy()).expect("Invalid pattern."); + let absolute = Pattern::new(&absolute_path.to_string_lossy())?; let basename = if !pattern.contains(std::path::MAIN_SEPARATOR) { - Some(Pattern::new(pattern).expect("Invalid pattern.")) + Some(Pattern::new(pattern)?) } else { None }; - FilePattern::Complex(absolute, basename) + Ok(FilePattern::Complex(absolute, basename)) } } @@ -80,10 +80,10 @@ impl PerFileIgnore { pattern: &str, prefixes: &[CheckCodePrefix], project_root: Option<&PathBuf>, - ) -> Self { - let pattern = FilePattern::from_user(pattern, project_root); + ) -> Result { + let pattern = FilePattern::from_user(pattern, project_root)?; let codes = BTreeSet::from_iter(prefixes.iter().flat_map(|prefix| prefix.codes())); - Self { pattern, codes } + Ok(Self { pattern, codes }) } }