Skip to content

Commit

Permalink
Refactor fix_file implementation to use Fix.applies
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 3, 2023
1 parent d004ddc commit 96b8ce1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions crates/ruff_diagnostics/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
24 changes: 12 additions & 12 deletions crates/ruff_linter/src/fix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FixResult> {
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();

Expand Down

0 comments on commit 96b8ce1

Please sign in to comment.