From 297ec2c2d2b5affcf76d18e424ac9493b3ea2743 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 19 Sep 2023 23:16:20 +0200 Subject: [PATCH] Use `Settings` where `AllSettings` isn't required (#7518) --- crates/ruff_cli/src/cache.rs | 10 +++--- crates/ruff_cli/src/commands/check.rs | 6 ++-- crates/ruff_cli/src/diagnostics.rs | 47 +++++---------------------- 3 files changed, 17 insertions(+), 46 deletions(-) diff --git a/crates/ruff_cli/src/cache.rs b/crates/ruff_cli/src/cache.rs index 1c10a25618..c5cae6dbb0 100644 --- a/crates/ruff_cli/src/cache.rs +++ b/crates/ruff_cli/src/cache.rs @@ -349,7 +349,7 @@ mod tests { use std::time::SystemTime; use itertools::Itertools; - use ruff::settings::{flags, AllSettings}; + use ruff::settings::{flags, AllSettings, Settings}; use ruff_cache::CACHE_DIR_NAME; use crate::cache::RelativePathBuf; @@ -371,10 +371,10 @@ mod tests { let _ = fs::remove_dir_all(&cache_dir); cache::init(&cache_dir).unwrap(); - let settings = AllSettings::default(); + let settings = Settings::default(); let package_root = fs::canonicalize(package_root).unwrap(); - let cache = Cache::open(&cache_dir, package_root.clone(), &settings.lib); + let cache = Cache::open(&cache_dir, package_root.clone(), &settings); assert_eq!(cache.new_files.lock().unwrap().len(), 0); let mut paths = Vec::new(); @@ -426,7 +426,7 @@ mod tests { cache.store().unwrap(); - let cache = Cache::open(&cache_dir, package_root.clone(), &settings.lib); + let cache = Cache::open(&cache_dir, package_root.clone(), &settings); assert_ne!(cache.package.files.len(), 0); parse_errors.sort(); @@ -710,7 +710,7 @@ mod tests { lint_path( &self.package_root.join(path), Some(&self.package_root), - &self.settings, + &self.settings.lib, Some(cache), flags::Noqa::Enabled, flags::FixMode::Generate, diff --git a/crates/ruff_cli/src/commands/check.rs b/crates/ruff_cli/src/commands/check.rs index 493e4117f0..b450b49e43 100644 --- a/crates/ruff_cli/src/commands/check.rs +++ b/crates/ruff_cli/src/commands/check.rs @@ -15,7 +15,7 @@ use rustc_hash::FxHashMap; use ruff::message::Message; use ruff::registry::Rule; -use ruff::settings::{flags, AllSettings}; +use ruff::settings::{flags, Settings}; use ruff::{fs, warn_user_once, IOError}; use ruff_diagnostics::Diagnostic; use ruff_python_ast::imports::ImportMap; @@ -111,7 +111,7 @@ pub(crate) fn check( .and_then(|parent| package_roots.get(parent)) .and_then(|package| *package); - let settings = resolver.resolve_all(path, pyproject_config); + let settings = resolver.resolve(path, pyproject_config); let cache_root = package.unwrap_or_else(|| path.parent().unwrap_or(path)); let cache = caches.as_ref().and_then(|caches| { @@ -199,7 +199,7 @@ pub(crate) fn check( fn lint_path( path: &Path, package: Option<&Path>, - settings: &AllSettings, + settings: &Settings, cache: Option<&Cache>, noqa: flags::Noqa, autofix: flags::FixMode, diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index 42c69ba58a..dc4e49e3e1 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -21,7 +21,7 @@ use ruff::logging::DisplayParseError; use ruff::message::Message; use ruff::pyproject_toml::lint_pyproject_toml; use ruff::registry::AsRule; -use ruff::settings::{flags, AllSettings, Settings}; +use ruff::settings::{flags, Settings}; use ruff::source_kind::SourceKind; use ruff::{fs, IOError, SyntaxError}; use ruff_diagnostics::Diagnostic; @@ -143,7 +143,7 @@ impl AddAssign for Diagnostics { pub(crate) fn lint_path( path: &Path, package: Option<&Path>, - settings: &AllSettings, + settings: &Settings, cache: Option<&Cache>, noqa: flags::Noqa, autofix: flags::FixMode, @@ -178,7 +178,6 @@ pub(crate) fn lint_path( let source_type = match SourceType::from(path) { SourceType::Toml(TomlSourceType::Pyproject) => { let messages = if settings - .lib .rules .iter_enabled() .any(|rule_code| rule_code.lint_source().is_pyproject_toml()) @@ -187,15 +186,11 @@ pub(crate) fn lint_path( match std::fs::read_to_string(path).map_err(SourceExtractionError::Io) { Ok(contents) => contents, Err(err) => { - return Ok(Diagnostics::from_source_error( - &err, - Some(path), - &settings.lib, - )); + return Ok(Diagnostics::from_source_error(&err, Some(path), settings)); } }; let source_file = SourceFileBuilder::new(path.to_string_lossy(), contents).finish(); - lint_pyproject_toml(source_file, &settings.lib) + lint_pyproject_toml(source_file, settings) } else { vec![] }; @@ -213,11 +208,7 @@ pub(crate) fn lint_path( Ok(Some(sources)) => sources, Ok(None) => return Ok(Diagnostics::default()), Err(err) => { - return Ok(Diagnostics::from_source_error( - &err, - Some(path), - &settings.lib, - )); + return Ok(Diagnostics::from_source_error(&err, Some(path), settings)); } }; @@ -233,14 +224,8 @@ pub(crate) fn lint_path( result, transformed, fixed, - }) = lint_fix( - path, - package, - noqa, - &settings.lib, - &source_kind, - source_type, - ) { + }) = lint_fix(path, package, noqa, settings, &source_kind, source_type) + { if !fixed.is_empty() { match autofix { flags::FixMode::Apply => match transformed.as_ref() { @@ -317,26 +302,12 @@ pub(crate) fn lint_path( (result, fixed) } else { // If we fail to autofix, lint the original source code. - let result = lint_only( - path, - package, - &settings.lib, - noqa, - &source_kind, - source_type, - ); + let result = lint_only(path, package, settings, noqa, &source_kind, source_type); let fixed = FxHashMap::default(); (result, fixed) } } else { - let result = lint_only( - path, - package, - &settings.lib, - noqa, - &source_kind, - source_type, - ); + let result = lint_only(path, package, settings, noqa, &source_kind, source_type); let fixed = FxHashMap::default(); (result, fixed) };