From 96b8ce18c831d1b5821fb349a3d4817096642dc5 Mon Sep 17 00:00:00 2001 From: Zanie Date: Mon, 2 Oct 2023 20:38:27 -0500 Subject: [PATCH] Refactor `fix_file` implementation to use `Fix.applies` https://github.com/astral-sh/ruff/pull/5119#discussion_r1265224831 --- crates/ruff_diagnostics/src/fix.rs | 4 ++-- crates/ruff_linter/src/fix/mod.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/ruff_diagnostics/src/fix.rs b/crates/ruff_diagnostics/src/fix.rs index 8db635392f86d..6c623087a6141 100644 --- a/crates/ruff_diagnostics/src/fix.rs +++ b/crates/ruff_diagnostics/src/fix.rs @@ -166,8 +166,8 @@ impl Fix { self } - /// Return [`true`] if this [`Fix`] should be applied with a given [`Applicability`]. - pub fn is_applied(&self, applicability: Applicability) -> bool { + /// Return [`true`] if this [`Fix`] should be applied with at a given [`Applicability`]. + pub fn applies(&self, applicability: Applicability) -> bool { self.applicability >= applicability } } diff --git a/crates/ruff_linter/src/fix/mod.rs b/crates/ruff_linter/src/fix/mod.rs index aa626d8992649..49e9ce90c3778 100644 --- a/crates/ruff_linter/src/fix/mod.rs +++ b/crates/ruff_linter/src/fix/mod.rs @@ -24,25 +24,25 @@ pub(crate) struct FixResult { pub(crate) source_map: SourceMap, } -/// Auto-fix errors in a file, and write the fixed source code to disk. +/// Fix errors in a file, and write the fixed source code to disk. pub(crate) fn fix_file( diagnostics: &[Diagnostic], locator: &Locator, fix_suggested: SuggestedFixes, ) -> Option { + let required_applicability = if fix_suggested.is_apply() { + Applicability::Suggested + } else { + Applicability::Automatic + }; + let mut with_fixes = diagnostics .iter() - .filter(|diag| { - let Some(ref fix) = diag.fix else { - return false; - }; - - // change this - if matches!(fix_suggested, SuggestedFixes::Apply) { - fix.applicability() >= Applicability::Suggested - } else { - fix.applicability() == Applicability::Automatic - } + .filter(|diagnostic| { + diagnostic + .fix + .as_ref() + .map_or(false, |fix| fix.applies(required_applicability)) }) .peekable();