diff --git a/crates/ruff/src/checkers/ast.rs b/crates/ruff/src/checkers/ast.rs index b0229b3c7b..42a03a9bed 100644 --- a/crates/ruff/src/checkers/ast.rs +++ b/crates/ruff/src/checkers/ast.rs @@ -187,7 +187,7 @@ impl<'a> Checker<'a> { /// Return `true` if a patch should be generated under the given autofix /// `Mode`. pub fn patch(&self, code: &Rule) -> bool { - matches!(self.autofix, flags::Autofix::Enabled) && self.settings.rules.should_fix(code) + self.autofix.is_enabled() && self.settings.rules.should_fix(code) } /// Return `true` if the `Expr` is a reference to `typing.${target}`. diff --git a/crates/ruff/src/checkers/noqa.rs b/crates/ruff/src/checkers/noqa.rs index 005d96abdb..2c0f10a938 100644 --- a/crates/ruff/src/checkers/noqa.rs +++ b/crates/ruff/src/checkers/noqa.rs @@ -148,8 +148,7 @@ pub fn check_noqa( UnusedNOQA { codes: None }, Range::new(Location::new(row + 1, start), Location::new(row + 1, end)), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.rule()) + if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::deletion( Location::new(row + 1, start - spaces), @@ -217,8 +216,7 @@ pub fn check_noqa( }, Range::new(Location::new(row + 1, start), Location::new(row + 1, end)), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.rule()) + if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule()) { if valid_codes.is_empty() { diagnostic.amend(Fix::deletion( diff --git a/crates/ruff/src/checkers/physical_lines.rs b/crates/ruff/src/checkers/physical_lines.rs index f141ad9e8d..eb6c030d83 100644 --- a/crates/ruff/src/checkers/physical_lines.rs +++ b/crates/ruff/src/checkers/physical_lines.rs @@ -42,10 +42,10 @@ pub fn check_physical_lines( let enforce_mixed_spaces_and_tabs = settings.rules.enabled(&Rule::MixedSpacesAndTabs); let enforce_bidirectional_unicode = settings.rules.enabled(&Rule::BidirectionalUnicode); - let fix_unnecessary_coding_comment = matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::UTF8EncodingDeclaration); - let fix_shebang_whitespace = matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::ShebangWhitespace); + let fix_unnecessary_coding_comment = + autofix.is_enabled() && settings.rules.should_fix(&Rule::UTF8EncodingDeclaration); + let fix_shebang_whitespace = + autofix.is_enabled() && settings.rules.should_fix(&Rule::ShebangWhitespace); let mut commented_lines_iter = commented_lines.iter().peekable(); let mut doc_lines_iter = doc_lines.iter().peekable(); @@ -145,8 +145,7 @@ pub fn check_physical_lines( if let Some(diagnostic) = no_newline_at_end_of_file( stylist, contents, - matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::NoNewLineAtEndOfFile), + autofix.is_enabled() && settings.rules.should_fix(&Rule::NoNewLineAtEndOfFile), ) { diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/checkers/tokens.rs b/crates/ruff/src/checkers/tokens.rs index 6fdfc8ceb9..377d1f6d31 100644 --- a/crates/ruff/src/checkers/tokens.rs +++ b/crates/ruff/src/checkers/tokens.rs @@ -108,8 +108,7 @@ pub fn check_tokens( locator, *start, *end, - matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::InvalidEscapeSequence), + autofix.is_enabled() && settings.rules.should_fix(&Rule::InvalidEscapeSequence), )); } } diff --git a/crates/ruff/src/rules/eradicate/rules.rs b/crates/ruff/src/rules/eradicate/rules.rs index 9826a87f9c..a5292b7b4d 100644 --- a/crates/ruff/src/rules/eradicate/rules.rs +++ b/crates/ruff/src/rules/eradicate/rules.rs @@ -60,9 +60,7 @@ pub fn commented_out_code( // Verify that the comment is on its own line, and that it contains code. if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) { let mut diagnostic = Diagnostic::new(CommentedOutCode, Range::new(start, end)); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::CommentedOutCode) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::CommentedOutCode) { diagnostic.amend(Fix::deletion(location, end_location)); } Some(diagnostic) diff --git a/crates/ruff/src/rules/flake8_commas/rules.rs b/crates/ruff/src/rules/flake8_commas/rules.rs index b4c8a3524d..a7725e3b83 100644 --- a/crates/ruff/src/rules/flake8_commas/rules.rs +++ b/crates/ruff/src/rules/flake8_commas/rules.rs @@ -257,9 +257,7 @@ pub fn trailing_commas( end_location: comma.2, }, ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::TrailingCommaProhibited) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::TrailingCommaProhibited) { diagnostic.amend(Fix::deletion(comma.0, comma.2)); } diagnostics.push(diagnostic); @@ -303,9 +301,7 @@ pub fn trailing_commas( end_location: missing_comma.2, }, ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::TrailingCommaMissing) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::TrailingCommaMissing) { diagnostic.amend(Fix::insertion(",".to_owned(), missing_comma.2)); } diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/flake8_quotes/rules.rs b/crates/ruff/src/rules/flake8_quotes/rules.rs index 3643058ebc..b887ee3486 100644 --- a/crates/ruff/src/rules/flake8_quotes/rules.rs +++ b/crates/ruff/src/rules/flake8_quotes/rules.rs @@ -280,9 +280,7 @@ fn docstring( }, Range::new(start, end), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::BadQuotesDocstring) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesDocstring) { let quote_count = if trivia.is_multiline { 3 } else { 1 }; let string_contents = &trivia.raw_text[quote_count..trivia.raw_text.len() - quote_count]; let quote = good_docstring("es_settings.docstring_quotes).repeat(quote_count); @@ -357,9 +355,7 @@ fn strings( Range::new(*start, *end), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::BadQuotesMultilineString) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesMultilineString) { let string_contents = &trivia.raw_text[3..trivia.raw_text.len() - 3]; let quote = good_multiline("es_settings.multiline_quotes); let mut fixed_contents = String::with_capacity( @@ -389,7 +385,7 @@ fn strings( { let mut diagnostic = Diagnostic::new(AvoidableEscapedQuote, Range::new(*start, *end)); - if matches!(autofix, flags::Autofix::Enabled) + if autofix.is_enabled() && settings.rules.should_fix(&Rule::AvoidableEscapedQuote) { let quote = bad_single("es_settings.inline_quotes); @@ -450,9 +446,7 @@ fn strings( }, Range::new(*start, *end), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::BadQuotesInlineString) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesInlineString) { let quote = good_single("es_settings.inline_quotes); let mut fixed_contents = String::with_capacity(trivia.prefix.len() + string_contents.len() + 2); diff --git a/crates/ruff/src/rules/isort/rules/add_required_imports.rs b/crates/ruff/src/rules/isort/rules/add_required_imports.rs index beeb65c674..7d6bc1a1ff 100644 --- a/crates/ruff/src/rules/isort/rules/add_required_imports.rs +++ b/crates/ruff/src/rules/isort/rules/add_required_imports.rs @@ -166,9 +166,7 @@ fn add_required_import( MissingRequiredImport(required_import.clone()), Range::new(Location::default(), Location::default()), ); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::MissingRequiredImport) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::MissingRequiredImport) { // Determine the location at which the import should be inserted. let splice = helpers::find_splice_location(python_ast, locator); diff --git a/crates/ruff/src/rules/isort/rules/organize_imports.rs b/crates/ruff/src/rules/isort/rules/organize_imports.rs index 09b7453e65..a4c718660f 100644 --- a/crates/ruff/src/rules/isort/rules/organize_imports.rs +++ b/crates/ruff/src/rules/isort/rules/organize_imports.rs @@ -153,9 +153,7 @@ pub fn organize_imports( None } else { let mut diagnostic = Diagnostic::new(UnsortedImports, range); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.rule()) - { + if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( indent(&expected, indentation), range.location, diff --git a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs index 8d193a6378..96c82aa077 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -105,9 +105,7 @@ pub fn compound_statements( Tok::Newline => { if let Some((start, end)) = semi { let mut diagnostic = Diagnostic::new(UselessSemicolon, Range::new(start, end)); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::UselessSemicolon) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::UselessSemicolon) { diagnostic.amend(Fix::deletion(start, end)); }; diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs b/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs index b64dd8f497..4128e7cf27 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs @@ -137,9 +137,7 @@ pub fn extraneous_parentheses( }; let mut diagnostic = Diagnostic::new(ExtraneousParentheses, Range::new(*start, *end)); - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(&Rule::ExtraneousParentheses) - { + if autofix.is_enabled() && settings.rules.should_fix(&Rule::ExtraneousParentheses) { let contents = locator.slice(&Range::new(*start, *end)); diagnostic.amend(Fix::replacement( contents[1..contents.len() - 1].to_string(), diff --git a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs index 27f4e89b65..14d1aaef46 100644 --- a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs +++ b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs @@ -1730,8 +1730,7 @@ pub fn ambiguous_unicode_character( Range::new(location, end_location), ); if settings.rules.enabled(diagnostic.kind.rule()) { - if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.rule()) + if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( representant.to_string(), diff --git a/crates/ruff/src/settings/flags.rs b/crates/ruff/src/settings/flags.rs index e6c6a604bc..6f34f7004f 100644 --- a/crates/ruff/src/settings/flags.rs +++ b/crates/ruff/src/settings/flags.rs @@ -6,6 +6,12 @@ pub enum Autofix { Disabled, } +impl Autofix { + pub const fn is_enabled(self) -> bool { + matches!(self, Self::Enabled) + } +} + impl From for Autofix { fn from(value: bool) -> Self { if value {