Rename Autofix to Fix (#7657)

**Summary** Mostly mechanical symbol rename and search-and-replace, with
small changes to the markdown docs to read better
This commit is contained in:
konsti 2023-09-28 12:53:05 +02:00 committed by GitHub
parent 8028de8956
commit 1e173f7909
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
231 changed files with 943 additions and 960 deletions

View file

@ -88,7 +88,7 @@ pub struct CheckCommand {
show_source: bool,
#[clap(long, overrides_with("show_source"), hide = true)]
no_show_source: bool,
/// Show an enumeration of all autofixed lint violations.
/// Show an enumeration of all fixed lint violations.
/// Use `--no-show-fixes` to disable.
#[arg(long, overrides_with("no_show_fixes"))]
show_fixes: bool,
@ -202,7 +202,7 @@ pub struct CheckCommand {
help_heading = "File selection"
)]
pub extend_exclude: Option<Vec<FilePattern>>,
/// List of rule codes to treat as eligible for autofix. Only applicable when autofix itself is enabled (e.g., via `--fix`).
/// List of rule codes to treat as eligible for fix. Only applicable when fix itself is enabled (e.g., via `--fix`).
#[arg(
long,
value_delimiter = ',',
@ -212,7 +212,7 @@ pub struct CheckCommand {
hide_possible_values = true
)]
pub fixable: Option<Vec<RuleSelector>>,
/// List of rule codes to treat as ineligible for autofix. Only applicable when autofix itself is enabled (e.g., via `--fix`).
/// List of rule codes to treat as ineligible for fix. Only applicable when fix itself is enabled (e.g., via `--fix`).
#[arg(
long,
value_delimiter = ',',
@ -288,7 +288,7 @@ pub struct CheckCommand {
conflicts_with = "exit_non_zero_on_fix"
)]
pub exit_zero: bool,
/// Exit with a non-zero status code if any files were modified via autofix, even if no lint violations remain.
/// Exit with a non-zero status code if any files were modified via fix, even if no lint violations remain.
#[arg(long, help_heading = "Miscellaneous", conflicts_with = "exit_zero")]
pub exit_non_zero_on_fix: bool,
/// Show counts for every rule with at least one violation.

View file

@ -35,7 +35,7 @@ pub(crate) fn check(
overrides: &CliOverrides,
cache: flags::Cache,
noqa: flags::Noqa,
autofix: flags::FixMode,
fix_mode: flags::FixMode,
) -> Result<Diagnostics> {
// Collect all the Python files to check.
let start = Instant::now();
@ -119,7 +119,7 @@ pub(crate) fn check(
}
});
lint_path(path, package, &settings.linter, cache, noqa, autofix).map_err(|e| {
lint_path(path, package, &settings.linter, cache, noqa, fix_mode).map_err(|e| {
(Some(path.to_owned()), {
let mut error = e.to_string();
for cause in e.chain() {
@ -198,10 +198,10 @@ fn lint_path(
settings: &LinterSettings,
cache: Option<&Cache>,
noqa: flags::Noqa,
autofix: flags::FixMode,
fix_mode: flags::FixMode,
) -> Result<Diagnostics> {
let result = catch_unwind(|| {
crate::diagnostics::lint_path(path, package, settings, cache, noqa, autofix)
crate::diagnostics::lint_path(path, package, settings, cache, noqa, fix_mode)
});
match result {

View file

@ -16,7 +16,7 @@ pub(crate) fn check_stdin(
pyproject_config: &PyprojectConfig,
overrides: &CliOverrides,
noqa: flags::Noqa,
autofix: flags::FixMode,
fix_mode: flags::FixMode,
) -> Result<Diagnostics> {
if let Some(filename) = filename {
if !python_file_at_path(filename, pyproject_config, overrides)? {
@ -33,7 +33,7 @@ pub(crate) fn check_stdin(
stdin,
&pyproject_config.settings,
noqa,
autofix,
fix_mode,
)?;
diagnostics.messages.sort_unstable();
Ok(diagnostics)

View file

@ -5,7 +5,7 @@ use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
use strum::IntoEnumIterator;
use ruff_diagnostics::AutofixKind;
use ruff_diagnostics::FixKind;
use ruff_linter::registry::{Linter, Rule, RuleNamespace};
use crate::args::HelpFormat;
@ -17,7 +17,7 @@ struct Explanation<'a> {
linter: &'a str,
summary: &'a str,
message_formats: &'a [&'a str],
autofix: String,
fix: String,
explanation: Option<&'a str>,
preview: bool,
}
@ -26,14 +26,14 @@ impl<'a> Explanation<'a> {
fn from_rule(rule: &'a Rule) -> Self {
let code = rule.noqa_code().to_string();
let (linter, _) = Linter::parse_code(&code).unwrap();
let autofix = rule.autofixable().to_string();
let fix = rule.fixable().to_string();
Self {
name: rule.as_ref(),
code,
linter: linter.name(),
summary: rule.message_formats()[0],
message_formats: rule.message_formats(),
autofix,
fix,
explanation: rule.explanation(),
preview: rule.is_preview(),
}
@ -51,9 +51,9 @@ fn format_rule_text(rule: Rule) -> String {
output.push('\n');
output.push('\n');
let autofix = rule.autofixable();
if matches!(autofix, AutofixKind::Always | AutofixKind::Sometimes) {
output.push_str(&autofix.to_string());
let fix_kind = rule.fixable();
if matches!(fix_kind, FixKind::Always | FixKind::Sometimes) {
output.push_str(&fix_kind.to_string());
output.push('\n');
output.push('\n');
}

View file

@ -147,7 +147,7 @@ pub(crate) fn lint_path(
settings: &LinterSettings,
cache: Option<&Cache>,
noqa: flags::Noqa,
autofix: flags::FixMode,
fix_mode: flags::FixMode,
) -> Result<Diagnostics> {
// Check the cache.
// TODO(charlie): `fixer::Mode::Apply` and `fixer::Mode::Diff` both have
@ -156,7 +156,7 @@ pub(crate) fn lint_path(
// 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 caching = match cache {
Some(cache) if noqa.into() && autofix.is_generate() => {
Some(cache) if noqa.into() && fix_mode.is_generate() => {
let relative_path = cache
.relative_path(path)
.expect("wrong package cache for file");
@ -220,7 +220,7 @@ pub(crate) fn lint_path(
error: parse_error,
},
fixed,
) = if matches!(autofix, flags::FixMode::Apply | flags::FixMode::Diff) {
) = if matches!(fix_mode, flags::FixMode::Apply | flags::FixMode::Diff) {
if let Ok(FixerResult {
result,
transformed,
@ -228,7 +228,7 @@ pub(crate) fn lint_path(
}) = lint_fix(path, package, noqa, settings, &source_kind, source_type)
{
if !fixed.is_empty() {
match autofix {
match fix_mode {
flags::FixMode::Apply => match transformed.as_ref() {
SourceKind::Python(transformed) => {
write(path, transformed.as_bytes())?;
@ -301,7 +301,7 @@ pub(crate) fn lint_path(
}
(result, fixed)
} else {
// If we fail to autofix, lint the original source code.
// If we fail to fix, lint the original source code.
let result = lint_only(path, package, settings, noqa, &source_kind, source_type);
let fixed = FxHashMap::default();
(result, fixed)
@ -369,7 +369,7 @@ pub(crate) fn lint_stdin(
contents: String,
settings: &Settings,
noqa: flags::Noqa,
autofix: flags::FixMode,
fix_mode: flags::FixMode,
) -> Result<Diagnostics> {
// TODO(charlie): Support `pyproject.toml`.
let SourceType::Python(source_type) = path.map(SourceType::from).unwrap_or_default() else {
@ -392,7 +392,7 @@ pub(crate) fn lint_stdin(
error: parse_error,
},
fixed,
) = if matches!(autofix, flags::FixMode::Apply | flags::FixMode::Diff) {
) = if matches!(fix_mode, flags::FixMode::Apply | flags::FixMode::Diff) {
if let Ok(FixerResult {
result,
transformed,
@ -405,7 +405,7 @@ pub(crate) fn lint_stdin(
&source_kind,
source_type,
) {
match autofix {
match fix_mode {
flags::FixMode::Apply => {
// Write the contents to stdout, regardless of whether any errors were fixed.
io::stdout().write_all(transformed.source_code().as_bytes())?;
@ -434,7 +434,7 @@ pub(crate) fn lint_stdin(
(result, fixed)
} else {
// If we fail to autofix, lint the original source code.
// If we fail to fix, lint the original source code.
let result = lint_only(
path.unwrap_or_else(|| Path::new("-")),
package,
@ -446,7 +446,7 @@ pub(crate) fn lint_stdin(
let fixed = FxHashMap::default();
// Write the contents to stdout anyway.
if autofix.is_apply() {
if fix_mode.is_apply() {
io::stdout().write_all(source_kind.source_code().as_bytes())?;
}

View file

@ -234,13 +234,13 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
..
} = pyproject_config.settings;
// Autofix rules are as follows:
// Fix rules are as follows:
// - By default, generate all fixes, but don't apply them to the filesystem.
// - If `--fix` or `--fix-only` is set, always apply fixes to the filesystem (or
// print them to stdout, if we're reading from stdin).
// - If `--diff` or `--fix-only` are set, don't print any violations (only
// fixes).
let autofix = if cli.diff {
let fix_mode = if cli.diff {
flags::FixMode::Diff
} else if fix || fix_only {
flags::FixMode::Apply
@ -275,7 +275,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
}
if cli.add_noqa {
if !autofix.is_generate() {
if !fix_mode.is_generate() {
warn_user!("--fix is incompatible with --add-noqa.");
}
let modifications =
@ -290,7 +290,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
return Ok(ExitStatus::Success);
}
let printer = Printer::new(output_format, log_level, autofix, printer_flags);
let printer = Printer::new(output_format, log_level, fix_mode, printer_flags);
if cli.watch {
if output_format != SerializationFormat::Text {
@ -317,7 +317,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
&overrides,
cache.into(),
noqa.into(),
autofix,
fix_mode,
)?;
printer.write_continuously(&mut writer, &messages)?;
@ -349,7 +349,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
&overrides,
cache.into(),
noqa.into(),
autofix,
fix_mode,
)?;
printer.write_continuously(&mut writer, &messages)?;
}
@ -366,7 +366,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
&pyproject_config,
&overrides,
noqa.into(),
autofix,
fix_mode,
)?
} else {
commands::check::check(
@ -375,14 +375,14 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
&overrides,
cache.into(),
noqa.into(),
autofix,
fix_mode,
)?
};
// Always try to print violations (the printer itself may suppress output),
// unless we're writing fixes via stdin (in which case, the transformed
// source code goes to stdout).
if !(is_stdin && matches!(autofix, flags::FixMode::Apply | flags::FixMode::Diff)) {
if !(is_stdin && matches!(fix_mode, flags::FixMode::Apply | flags::FixMode::Diff)) {
if cli.statistics {
printer.write_statistics(&diagnostics, &mut writer)?;
} else {

View file

@ -72,7 +72,7 @@ impl From<Rule> for SerializeRuleAsCode {
pub(crate) struct Printer {
format: SerializationFormat,
log_level: LogLevel,
autofix_level: flags::FixMode,
fix_mode: flags::FixMode,
flags: Flags,
}
@ -80,13 +80,13 @@ impl Printer {
pub(crate) const fn new(
format: SerializationFormat,
log_level: LogLevel,
autofix_level: flags::FixMode,
fix_mode: flags::FixMode,
flags: Flags,
) -> Self {
Self {
format,
log_level,
autofix_level,
fix_mode,
flags,
}
}
@ -118,7 +118,7 @@ impl Printer {
writeln!(writer, "Found {remaining} error{s}.")?;
}
if show_fix_status(self.autofix_level) {
if show_fix_status(self.fix_mode) {
let num_fixable = diagnostics
.messages
.iter()
@ -140,7 +140,7 @@ impl Printer {
.sum::<usize>();
if fixed > 0 {
let s = if fixed == 1 { "" } else { "s" };
if self.autofix_level.is_apply() {
if self.fix_mode.is_apply() {
writeln!(writer, "Fixed {fixed} error{s}.")?;
} else {
writeln!(writer, "Would fix {fixed} error{s}.")?;
@ -191,7 +191,7 @@ impl Printer {
}
SerializationFormat::Text => {
TextEmitter::default()
.with_show_fix_status(show_fix_status(self.autofix_level))
.with_show_fix_status(show_fix_status(self.fix_mode))
.with_show_fix_diff(self.flags.intersects(Flags::SHOW_FIX_DIFF))
.with_show_source(self.flags.intersects(Flags::SHOW_SOURCE))
.emit(writer, &diagnostics.messages, &context)?;
@ -209,7 +209,7 @@ impl Printer {
SerializationFormat::Grouped => {
GroupedEmitter::default()
.with_show_source(self.flags.intersects(Flags::SHOW_SOURCE))
.with_show_fix_status(show_fix_status(self.autofix_level))
.with_show_fix_status(show_fix_status(self.fix_mode))
.emit(writer, &diagnostics.messages, &context)?;
if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
@ -366,7 +366,7 @@ impl Printer {
let context = EmitterContext::new(&diagnostics.notebook_indexes);
TextEmitter::default()
.with_show_fix_status(show_fix_status(self.autofix_level))
.with_show_fix_status(show_fix_status(self.fix_mode))
.with_show_source(self.flags.intersects(Flags::SHOW_SOURCE))
.emit(writer, &diagnostics.messages, &context)?;
}
@ -390,13 +390,13 @@ fn num_digits(n: usize) -> usize {
}
/// Return `true` if the [`Printer`] should indicate that a rule is fixable.
const fn show_fix_status(autofix_level: flags::FixMode) -> bool {
const fn show_fix_status(fix_mode: flags::FixMode) -> bool {
// If we're in application mode, avoid indicating that a rule is fixable.
// If the specific violation were truly fixable, it would've been fixed in
// this pass! (We're occasionally unable to determine whether a specific
// violation is fixable without trying to fix it, so if autofix is not
// violation is fixable without trying to fix it, so if fix is not
// enabled, we may inadvertently indicate that a rule is fixable.)
!autofix_level.is_apply()
!fix_mode.is_apply()
}
fn print_fix_summary(writer: &mut dyn Write, fixed: &FxHashMap<String, FixTable>) -> Result<()> {

View file

@ -136,7 +136,7 @@ fn stdin_json() {
}
#[test]
fn stdin_autofix() {
fn stdin_fix() {
let args = ["--fix"];
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
@ -154,7 +154,7 @@ fn stdin_autofix() {
}
#[test]
fn stdin_autofix_when_not_fixable_should_still_print_contents() {
fn stdin_fix_when_not_fixable_should_still_print_contents() {
let args = ["--fix"];
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
@ -173,7 +173,7 @@ fn stdin_autofix_when_not_fixable_should_still_print_contents() {
}
#[test]
fn stdin_autofix_when_no_issues_should_still_print_contents() {
fn stdin_fix_when_no_issues_should_still_print_contents() {
let args = ["--fix"];
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)

View file

@ -13,7 +13,7 @@ exit_code: 0
Derived from the **Pyflakes** linter.
Autofix is sometimes available.
Fix is sometimes available.
## What it does
Checks for unused imports.