mirror of https://github.com/astral-sh/ruff
Add Autofix::is_enabled() to remove repeative patterns (#3159)
This commit is contained in:
parent
e5c1f95545
commit
4357f2be0f
|
|
@ -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}`.
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -6,6 +6,12 @@ pub enum Autofix {
|
|||
Disabled,
|
||||
}
|
||||
|
||||
impl Autofix {
|
||||
pub const fn is_enabled(self) -> bool {
|
||||
matches!(self, Self::Enabled)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for Autofix {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
|
|
|
|||
Loading…
Reference in New Issue