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
|
/// Return `true` if a patch should be generated under the given autofix
|
||||||
/// `Mode`.
|
/// `Mode`.
|
||||||
pub fn patch(&self, code: &Rule) -> bool {
|
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}`.
|
/// Return `true` if the `Expr` is a reference to `typing.${target}`.
|
||||||
|
|
|
||||||
|
|
@ -148,8 +148,7 @@ pub fn check_noqa(
|
||||||
UnusedNOQA { codes: None },
|
UnusedNOQA { codes: None },
|
||||||
Range::new(Location::new(row + 1, start), Location::new(row + 1, end)),
|
Range::new(Location::new(row + 1, start), Location::new(row + 1, end)),
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule())
|
||||||
&& settings.rules.should_fix(diagnostic.kind.rule())
|
|
||||||
{
|
{
|
||||||
diagnostic.amend(Fix::deletion(
|
diagnostic.amend(Fix::deletion(
|
||||||
Location::new(row + 1, start - spaces),
|
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)),
|
Range::new(Location::new(row + 1, start), Location::new(row + 1, end)),
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule())
|
||||||
&& settings.rules.should_fix(diagnostic.kind.rule())
|
|
||||||
{
|
{
|
||||||
if valid_codes.is_empty() {
|
if valid_codes.is_empty() {
|
||||||
diagnostic.amend(Fix::deletion(
|
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_mixed_spaces_and_tabs = settings.rules.enabled(&Rule::MixedSpacesAndTabs);
|
||||||
let enforce_bidirectional_unicode = settings.rules.enabled(&Rule::BidirectionalUnicode);
|
let enforce_bidirectional_unicode = settings.rules.enabled(&Rule::BidirectionalUnicode);
|
||||||
|
|
||||||
let fix_unnecessary_coding_comment = matches!(autofix, flags::Autofix::Enabled)
|
let fix_unnecessary_coding_comment =
|
||||||
&& settings.rules.should_fix(&Rule::UTF8EncodingDeclaration);
|
autofix.is_enabled() && settings.rules.should_fix(&Rule::UTF8EncodingDeclaration);
|
||||||
let fix_shebang_whitespace = matches!(autofix, flags::Autofix::Enabled)
|
let fix_shebang_whitespace =
|
||||||
&& settings.rules.should_fix(&Rule::ShebangWhitespace);
|
autofix.is_enabled() && settings.rules.should_fix(&Rule::ShebangWhitespace);
|
||||||
|
|
||||||
let mut commented_lines_iter = commented_lines.iter().peekable();
|
let mut commented_lines_iter = commented_lines.iter().peekable();
|
||||||
let mut doc_lines_iter = doc_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(
|
if let Some(diagnostic) = no_newline_at_end_of_file(
|
||||||
stylist,
|
stylist,
|
||||||
contents,
|
contents,
|
||||||
matches!(autofix, flags::Autofix::Enabled)
|
autofix.is_enabled() && settings.rules.should_fix(&Rule::NoNewLineAtEndOfFile),
|
||||||
&& settings.rules.should_fix(&Rule::NoNewLineAtEndOfFile),
|
|
||||||
) {
|
) {
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,8 +108,7 @@ pub fn check_tokens(
|
||||||
locator,
|
locator,
|
||||||
*start,
|
*start,
|
||||||
*end,
|
*end,
|
||||||
matches!(autofix, flags::Autofix::Enabled)
|
autofix.is_enabled() && settings.rules.should_fix(&Rule::InvalidEscapeSequence),
|
||||||
&& 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.
|
// 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[..]) {
|
if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
|
||||||
let mut diagnostic = Diagnostic::new(CommentedOutCode, Range::new(start, end));
|
let mut diagnostic = Diagnostic::new(CommentedOutCode, Range::new(start, end));
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::CommentedOutCode) {
|
||||||
&& settings.rules.should_fix(&Rule::CommentedOutCode)
|
|
||||||
{
|
|
||||||
diagnostic.amend(Fix::deletion(location, end_location));
|
diagnostic.amend(Fix::deletion(location, end_location));
|
||||||
}
|
}
|
||||||
Some(diagnostic)
|
Some(diagnostic)
|
||||||
|
|
|
||||||
|
|
@ -257,9 +257,7 @@ pub fn trailing_commas(
|
||||||
end_location: comma.2,
|
end_location: comma.2,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::TrailingCommaProhibited) {
|
||||||
&& settings.rules.should_fix(&Rule::TrailingCommaProhibited)
|
|
||||||
{
|
|
||||||
diagnostic.amend(Fix::deletion(comma.0, comma.2));
|
diagnostic.amend(Fix::deletion(comma.0, comma.2));
|
||||||
}
|
}
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
|
|
@ -303,9 +301,7 @@ pub fn trailing_commas(
|
||||||
end_location: missing_comma.2,
|
end_location: missing_comma.2,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::TrailingCommaMissing) {
|
||||||
&& settings.rules.should_fix(&Rule::TrailingCommaMissing)
|
|
||||||
{
|
|
||||||
diagnostic.amend(Fix::insertion(",".to_owned(), missing_comma.2));
|
diagnostic.amend(Fix::insertion(",".to_owned(), missing_comma.2));
|
||||||
}
|
}
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
|
|
|
||||||
|
|
@ -280,9 +280,7 @@ fn docstring(
|
||||||
},
|
},
|
||||||
Range::new(start, end),
|
Range::new(start, end),
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesDocstring) {
|
||||||
&& settings.rules.should_fix(&Rule::BadQuotesDocstring)
|
|
||||||
{
|
|
||||||
let quote_count = if trivia.is_multiline { 3 } else { 1 };
|
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 string_contents = &trivia.raw_text[quote_count..trivia.raw_text.len() - quote_count];
|
||||||
let quote = good_docstring("es_settings.docstring_quotes).repeat(quote_count);
|
let quote = good_docstring("es_settings.docstring_quotes).repeat(quote_count);
|
||||||
|
|
@ -357,9 +355,7 @@ fn strings(
|
||||||
Range::new(*start, *end),
|
Range::new(*start, *end),
|
||||||
);
|
);
|
||||||
|
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesMultilineString) {
|
||||||
&& settings.rules.should_fix(&Rule::BadQuotesMultilineString)
|
|
||||||
{
|
|
||||||
let string_contents = &trivia.raw_text[3..trivia.raw_text.len() - 3];
|
let string_contents = &trivia.raw_text[3..trivia.raw_text.len() - 3];
|
||||||
let quote = good_multiline("es_settings.multiline_quotes);
|
let quote = good_multiline("es_settings.multiline_quotes);
|
||||||
let mut fixed_contents = String::with_capacity(
|
let mut fixed_contents = String::with_capacity(
|
||||||
|
|
@ -389,7 +385,7 @@ fn strings(
|
||||||
{
|
{
|
||||||
let mut diagnostic =
|
let mut diagnostic =
|
||||||
Diagnostic::new(AvoidableEscapedQuote, Range::new(*start, *end));
|
Diagnostic::new(AvoidableEscapedQuote, Range::new(*start, *end));
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled()
|
||||||
&& settings.rules.should_fix(&Rule::AvoidableEscapedQuote)
|
&& settings.rules.should_fix(&Rule::AvoidableEscapedQuote)
|
||||||
{
|
{
|
||||||
let quote = bad_single("es_settings.inline_quotes);
|
let quote = bad_single("es_settings.inline_quotes);
|
||||||
|
|
@ -450,9 +446,7 @@ fn strings(
|
||||||
},
|
},
|
||||||
Range::new(*start, *end),
|
Range::new(*start, *end),
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::BadQuotesInlineString) {
|
||||||
&& settings.rules.should_fix(&Rule::BadQuotesInlineString)
|
|
||||||
{
|
|
||||||
let quote = good_single("es_settings.inline_quotes);
|
let quote = good_single("es_settings.inline_quotes);
|
||||||
let mut fixed_contents =
|
let mut fixed_contents =
|
||||||
String::with_capacity(trivia.prefix.len() + string_contents.len() + 2);
|
String::with_capacity(trivia.prefix.len() + string_contents.len() + 2);
|
||||||
|
|
|
||||||
|
|
@ -166,9 +166,7 @@ fn add_required_import(
|
||||||
MissingRequiredImport(required_import.clone()),
|
MissingRequiredImport(required_import.clone()),
|
||||||
Range::new(Location::default(), Location::default()),
|
Range::new(Location::default(), Location::default()),
|
||||||
);
|
);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::MissingRequiredImport) {
|
||||||
&& settings.rules.should_fix(&Rule::MissingRequiredImport)
|
|
||||||
{
|
|
||||||
// Determine the location at which the import should be inserted.
|
// Determine the location at which the import should be inserted.
|
||||||
let splice = helpers::find_splice_location(python_ast, locator);
|
let splice = helpers::find_splice_location(python_ast, locator);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,9 +153,7 @@ pub fn organize_imports(
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let mut diagnostic = Diagnostic::new(UnsortedImports, range);
|
let mut diagnostic = Diagnostic::new(UnsortedImports, range);
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule()) {
|
||||||
&& settings.rules.should_fix(diagnostic.kind.rule())
|
|
||||||
{
|
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
indent(&expected, indentation),
|
indent(&expected, indentation),
|
||||||
range.location,
|
range.location,
|
||||||
|
|
|
||||||
|
|
@ -105,9 +105,7 @@ pub fn compound_statements(
|
||||||
Tok::Newline => {
|
Tok::Newline => {
|
||||||
if let Some((start, end)) = semi {
|
if let Some((start, end)) = semi {
|
||||||
let mut diagnostic = Diagnostic::new(UselessSemicolon, Range::new(start, end));
|
let mut diagnostic = Diagnostic::new(UselessSemicolon, Range::new(start, end));
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::UselessSemicolon) {
|
||||||
&& settings.rules.should_fix(&Rule::UselessSemicolon)
|
|
||||||
{
|
|
||||||
diagnostic.amend(Fix::deletion(start, end));
|
diagnostic.amend(Fix::deletion(start, end));
|
||||||
};
|
};
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
|
|
|
||||||
|
|
@ -137,9 +137,7 @@ pub fn extraneous_parentheses(
|
||||||
};
|
};
|
||||||
let mut diagnostic =
|
let mut diagnostic =
|
||||||
Diagnostic::new(ExtraneousParentheses, Range::new(*start, *end));
|
Diagnostic::new(ExtraneousParentheses, Range::new(*start, *end));
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(&Rule::ExtraneousParentheses) {
|
||||||
&& settings.rules.should_fix(&Rule::ExtraneousParentheses)
|
|
||||||
{
|
|
||||||
let contents = locator.slice(&Range::new(*start, *end));
|
let contents = locator.slice(&Range::new(*start, *end));
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
contents[1..contents.len() - 1].to_string(),
|
contents[1..contents.len() - 1].to_string(),
|
||||||
|
|
|
||||||
|
|
@ -1730,8 +1730,7 @@ pub fn ambiguous_unicode_character(
|
||||||
Range::new(location, end_location),
|
Range::new(location, end_location),
|
||||||
);
|
);
|
||||||
if settings.rules.enabled(diagnostic.kind.rule()) {
|
if settings.rules.enabled(diagnostic.kind.rule()) {
|
||||||
if matches!(autofix, flags::Autofix::Enabled)
|
if autofix.is_enabled() && settings.rules.should_fix(diagnostic.kind.rule())
|
||||||
&& settings.rules.should_fix(diagnostic.kind.rule())
|
|
||||||
{
|
{
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
representant.to_string(),
|
representant.to_string(),
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,12 @@ pub enum Autofix {
|
||||||
Disabled,
|
Disabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Autofix {
|
||||||
|
pub const fn is_enabled(self) -> bool {
|
||||||
|
matches!(self, Self::Enabled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<bool> for Autofix {
|
impl From<bool> for Autofix {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue