From 3ed539d50ed6260358c97d2e00b49bad4abfa37e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 1 Mar 2023 22:28:13 -0500 Subject: [PATCH] Add a CLI flag to force-ignore noqa directives (#3296) --- crates/ruff/src/lib_wasm.rs | 2 +- crates/ruff/src/linter.rs | 10 +++++--- crates/ruff/src/rules/pandas_vet/mod.rs | 2 +- crates/ruff/src/rules/pyflakes/mod.rs | 2 +- crates/ruff/src/test.rs | 4 +-- crates/ruff_cli/src/args.rs | 8 ++++++ crates/ruff_cli/src/commands/run.rs | 3 ++- crates/ruff_cli/src/commands/run_stdin.rs | 4 ++- crates/ruff_cli/src/diagnostics.rs | 30 ++++++++++++++++++++--- crates/ruff_cli/src/main.rs | 5 ++++ 10 files changed, 55 insertions(+), 15 deletions(-) diff --git a/crates/ruff/src/lib_wasm.rs b/crates/ruff/src/lib_wasm.rs index fcbcb79a529ef..300922a8ac684 100644 --- a/crates/ruff/src/lib_wasm.rs +++ b/crates/ruff/src/lib_wasm.rs @@ -201,8 +201,8 @@ pub fn check(contents: &str, options: JsValue) -> Result { &indexer, &directives, &settings, - flags::Autofix::Enabled, flags::Noqa::Enabled, + flags::Autofix::Enabled, ); let messages: Vec = diagnostics diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index 7e7b3b607cc6b..799a53921b7b8 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -62,8 +62,8 @@ pub fn check_path( indexer: &Indexer, directives: &Directives, settings: &Settings, - autofix: flags::Autofix, noqa: flags::Noqa, + autofix: flags::Autofix, ) -> LinterResult> { // Aggregate all diagnostics. let mut diagnostics = vec![]; @@ -255,8 +255,8 @@ pub fn add_noqa_to_path(path: &Path, package: Option<&Path>, settings: &Settings &indexer, &directives, settings, - flags::Autofix::Disabled, flags::Noqa::Disabled, + flags::Autofix::Disabled, ); // Log any parse errors. @@ -287,6 +287,7 @@ pub fn lint_only( path: &Path, package: Option<&Path>, settings: &Settings, + noqa: flags::Noqa, autofix: flags::Autofix, ) -> LinterResult> { // Tokenize once. @@ -316,8 +317,8 @@ pub fn lint_only( &indexer, &directives, settings, + noqa, autofix, - flags::Noqa::Enabled, ); // Convert from diagnostics to messages. @@ -345,6 +346,7 @@ pub fn lint_fix<'a>( contents: &'a str, path: &Path, package: Option<&Path>, + noqa: flags::Noqa, settings: &Settings, ) -> Result<(LinterResult>, Cow<'a, str>, FixTable)> { let mut transformed = Cow::Borrowed(contents); @@ -387,8 +389,8 @@ pub fn lint_fix<'a>( &indexer, &directives, settings, + noqa, flags::Autofix::Enabled, - flags::Noqa::Enabled, ); if iterations == 0 { diff --git a/crates/ruff/src/rules/pandas_vet/mod.rs b/crates/ruff/src/rules/pandas_vet/mod.rs index 8e9f8a617157a..77b207e72ccb8 100644 --- a/crates/ruff/src/rules/pandas_vet/mod.rs +++ b/crates/ruff/src/rules/pandas_vet/mod.rs @@ -41,8 +41,8 @@ mod tests { &indexer, &directives, &settings, - flags::Autofix::Enabled, flags::Noqa::Enabled, + flags::Autofix::Enabled, ); let actual = diagnostics .iter() diff --git a/crates/ruff/src/rules/pyflakes/mod.rs b/crates/ruff/src/rules/pyflakes/mod.rs index 146c9505e9811..56d1e75ab2025 100644 --- a/crates/ruff/src/rules/pyflakes/mod.rs +++ b/crates/ruff/src/rules/pyflakes/mod.rs @@ -263,8 +263,8 @@ mod tests { &indexer, &directives, &settings, - flags::Autofix::Enabled, flags::Noqa::Enabled, + flags::Autofix::Enabled, ); diagnostics.sort_by_key(|diagnostic| diagnostic.location); let actual = diagnostics diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index fb582277142b0..c32580931965a 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -43,8 +43,8 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result> { &indexer, &directives, settings, - flags::Autofix::Enabled, flags::Noqa::Enabled, + flags::Autofix::Enabled, ); // Detect autofixes that don't converge after multiple iterations. @@ -76,8 +76,8 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result> { &indexer, &directives, settings, - flags::Autofix::Enabled, flags::Noqa::Enabled, + flags::Autofix::Enabled, ); if let Some((fixed_contents, _)) = fix_file(&diagnostics, &locator) { if iterations < max_iterations { diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 6fcd6e8c540e1..12db791a0c696 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -93,6 +93,9 @@ pub struct CheckArgs { fix_only: bool, #[clap(long, overrides_with("fix_only"), hide = true)] no_fix_only: bool, + /// Ignore any `# noqa` comments. + #[arg(long)] + ignore_noqa: bool, /// Output serialization format for violations. #[arg(long, value_enum, env = "RUFF_FORMAT")] pub format: Option, @@ -258,6 +261,7 @@ pub struct CheckArgs { conflicts_with = "show_files", conflicts_with = "show_settings", // Unsupported default-command arguments. + conflicts_with = "ignore_noqa", conflicts_with = "statistics", conflicts_with = "stdin_filename", conflicts_with = "watch", @@ -272,6 +276,7 @@ pub struct CheckArgs { // conflicts_with = "show_files", conflicts_with = "show_settings", // Unsupported default-command arguments. + conflicts_with = "ignore_noqa", conflicts_with = "statistics", conflicts_with = "stdin_filename", conflicts_with = "watch", @@ -285,6 +290,7 @@ pub struct CheckArgs { conflicts_with = "show_files", // conflicts_with = "show_settings", // Unsupported default-command arguments. + conflicts_with = "ignore_noqa", conflicts_with = "statistics", conflicts_with = "stdin_filename", conflicts_with = "watch", @@ -357,6 +363,7 @@ impl CheckArgs { exit_zero: self.exit_zero, exit_non_zero_on_fix: self.exit_non_zero_on_fix, files: self.files, + ignore_noqa: self.ignore_noqa, isolated: self.isolated, no_cache: self.no_cache, show_files: self.show_files, @@ -415,6 +422,7 @@ pub struct Arguments { pub exit_zero: bool, pub exit_non_zero_on_fix: bool, pub files: Vec, + pub ignore_noqa: bool, pub isolated: bool, pub no_cache: bool, pub show_files: bool, diff --git a/crates/ruff_cli/src/commands/run.rs b/crates/ruff_cli/src/commands/run.rs index 334c4b4093214..2786032cedb58 100644 --- a/crates/ruff_cli/src/commands/run.rs +++ b/crates/ruff_cli/src/commands/run.rs @@ -26,6 +26,7 @@ pub fn run( pyproject_strategy: &PyprojectDiscovery, overrides: &Overrides, cache: flags::Cache, + noqa: flags::Noqa, autofix: fix::FixMode, ) -> Result { // Collect all the Python files to check. @@ -84,7 +85,7 @@ pub fn run( .and_then(|parent| package_roots.get(parent)) .and_then(|package| *package); let settings = resolver.resolve_all(path, pyproject_strategy); - lint_path(path, package, settings, cache, autofix) + lint_path(path, package, settings, cache, noqa, autofix) .map_err(|e| (Some(path.to_owned()), e.to_string())) } Err(e) => Err(( diff --git a/crates/ruff_cli/src/commands/run_stdin.rs b/crates/ruff_cli/src/commands/run_stdin.rs index 298901c644537..f33db4b9202c5 100644 --- a/crates/ruff_cli/src/commands/run_stdin.rs +++ b/crates/ruff_cli/src/commands/run_stdin.rs @@ -4,6 +4,7 @@ use std::path::Path; use anyhow::Result; use ruff::resolver::PyprojectDiscovery; +use ruff::settings::flags; use ruff::{fix, packaging, resolver}; use crate::args::Overrides; @@ -21,6 +22,7 @@ pub fn run_stdin( filename: Option<&Path>, pyproject_strategy: &PyprojectDiscovery, overrides: &Overrides, + noqa: flags::Noqa, autofix: fix::FixMode, ) -> Result { if let Some(filename) = filename { @@ -33,7 +35,7 @@ pub fn run_stdin( .and_then(Path::parent) .and_then(|path| packaging::detect_package_root(path, &settings.lib.namespace_packages)); let stdin = read_from_stdin()?; - let mut diagnostics = lint_stdin(filename, package_root, &stdin, &settings.lib, autofix)?; + let mut diagnostics = lint_stdin(filename, package_root, &stdin, &settings.lib, noqa, autofix)?; diagnostics.messages.sort_unstable(); Ok(diagnostics) } diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index 240348679f80a..2b81a3d77a655 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -57,6 +57,7 @@ pub fn lint_path( package: Option<&Path>, settings: &AllSettings, cache: flags::Cache, + noqa: flags::Noqa, autofix: fix::FixMode, ) -> Result { // Check the cache. @@ -65,7 +66,9 @@ pub fn lint_path( // to cache `fixer::Mode::Apply`, since a file either has no fixes, or we'll // write the fixes to disk, thus invalidating the cache. But it's a bit hard // to reason about. We need to come up with a better solution here.) - let metadata = if cache.into() && matches!(autofix, fix::FixMode::None | fix::FixMode::Generate) + let metadata = if cache.into() + && noqa.into() + && matches!(autofix, fix::FixMode::None | fix::FixMode::Generate) { let metadata = path.metadata()?; if let Some(messages) = @@ -90,7 +93,8 @@ pub fn lint_path( }, fixed, ) = if matches!(autofix, fix::FixMode::Apply | fix::FixMode::Diff) { - if let Ok((result, transformed, fixed)) = lint_fix(&contents, path, package, &settings.lib) + if let Ok((result, transformed, fixed)) = + lint_fix(&contents, path, package, noqa, &settings.lib) { if !fixed.is_empty() { if matches!(autofix, fix::FixMode::Apply) { @@ -108,12 +112,26 @@ pub fn lint_path( (result, fixed) } else { // If we fail to autofix, lint the original source code. - let result = lint_only(&contents, path, package, &settings.lib, autofix.into()); + let result = lint_only( + &contents, + path, + package, + &settings.lib, + noqa, + autofix.into(), + ); let fixed = FxHashMap::default(); (result, fixed) } } else { - let result = lint_only(&contents, path, package, &settings.lib, autofix.into()); + let result = lint_only( + &contents, + path, + package, + &settings.lib, + noqa, + autofix.into(), + ); let fixed = FxHashMap::default(); (result, fixed) }; @@ -158,6 +176,7 @@ pub fn lint_stdin( package: Option<&Path>, contents: &str, settings: &Settings, + noqa: flags::Noqa, autofix: fix::FixMode, ) -> Result { // Lint the inputs. @@ -172,6 +191,7 @@ pub fn lint_stdin( contents, path.unwrap_or_else(|| Path::new("-")), package, + noqa, settings, ) { if matches!(autofix, fix::FixMode::Apply) { @@ -201,6 +221,7 @@ pub fn lint_stdin( path.unwrap_or_else(|| Path::new("-")), package, settings, + noqa, autofix.into(), ); let fixed = FxHashMap::default(); @@ -218,6 +239,7 @@ pub fn lint_stdin( path.unwrap_or_else(|| Path::new("-")), package, settings, + noqa, autofix.into(), ); let fixed = FxHashMap::default(); diff --git a/crates/ruff_cli/src/main.rs b/crates/ruff_cli/src/main.rs index 1b21cea384a4b..882ed775d35da 100644 --- a/crates/ruff_cli/src/main.rs +++ b/crates/ruff_cli/src/main.rs @@ -172,6 +172,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result { fix::FixMode::None }; let cache = !cli.no_cache; + let noqa = !cli.ignore_noqa; let mut printer_flags = PrinterFlags::empty(); if !(cli.diff || fix_only) { printer_flags |= PrinterFlags::SHOW_VIOLATIONS; @@ -222,6 +223,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result { &pyproject_strategy, &overrides, cache.into(), + noqa.into(), fix::FixMode::None, )?; printer.write_continuously(&messages)?; @@ -251,6 +253,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result { &pyproject_strategy, &overrides, cache.into(), + noqa.into(), fix::FixMode::None, )?; printer.write_continuously(&messages)?; @@ -268,6 +271,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result { cli.stdin_filename.map(fs::normalize_path).as_deref(), &pyproject_strategy, &overrides, + noqa.into(), autofix, )? } else { @@ -276,6 +280,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result { &pyproject_strategy, &overrides, cache.into(), + noqa.into(), autofix, )? };