mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
DRY up usages of matches with fixer Mode (#470)
This commit is contained in:
parent
e7472eac1c
commit
f63a87737a
15 changed files with 53 additions and 57 deletions
|
@ -13,6 +13,17 @@ pub enum Mode {
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mode {
|
||||||
|
/// Return `true` if a patch should be generated under the given `Mode`.
|
||||||
|
pub fn enabled(&self) -> bool {
|
||||||
|
match &self {
|
||||||
|
Mode::Generate => true,
|
||||||
|
Mode::Apply => true,
|
||||||
|
Mode::None => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<bool> for Mode {
|
impl From<bool> for Mode {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
match value {
|
match value {
|
||||||
|
|
|
@ -2070,8 +2070,7 @@ impl<'a> Checker<'a> {
|
||||||
let child = self.parents[defined_by];
|
let child = self.parents[defined_by];
|
||||||
let parent = defined_in.map(|defined_in| self.parents[defined_in]);
|
let parent = defined_in.map(|defined_in| self.parents[defined_in]);
|
||||||
|
|
||||||
let fix = if matches!(self.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
|
let fix = if self.autofix.enabled() {
|
||||||
{
|
|
||||||
let deleted: Vec<&Stmt> = self
|
let deleted: Vec<&Stmt> = self
|
||||||
.deletions
|
.deletions
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -166,7 +166,7 @@ pub fn check_lines(
|
||||||
end_location: Location::new(row + 1, end + 1),
|
end_location: Location::new(row + 1, end + 1),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if autofix.enabled() {
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(row + 1, start + 1),
|
Location::new(row + 1, start + 1),
|
||||||
Location::new(row + 1, lines[row].chars().count() + 1),
|
Location::new(row + 1, lines[row].chars().count() + 1),
|
||||||
|
@ -194,7 +194,7 @@ pub fn check_lines(
|
||||||
end_location: Location::new(row + 1, end + 1),
|
end_location: Location::new(row + 1, end + 1),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if autofix.enabled() {
|
||||||
if valid_codes.is_empty() {
|
if valid_codes.is_empty() {
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(row + 1, start + 1),
|
Location::new(row + 1, start + 1),
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind};
|
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckKind};
|
use crate::checks::{Check, CheckKind};
|
||||||
|
@ -44,7 +43,7 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: &Optio
|
||||||
} = &test.node
|
} = &test.node
|
||||||
{
|
{
|
||||||
let mut check = Check::new(CheckKind::DoNotAssertFalse, Range::from_located(test));
|
let mut check = Check::new(CheckKind::DoNotAssertFalse, Range::from_located(test));
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
let mut generator = SourceGenerator::new();
|
let mut generator = SourceGenerator::new();
|
||||||
if let Ok(()) = generator.unparse_stmt(&assertion_error(msg)) {
|
if let Ok(()) = generator.unparse_stmt(&assertion_error(msg)) {
|
||||||
if let Ok(content) = generator.generate() {
|
if let Ok(content) = generator.generate() {
|
||||||
|
|
|
@ -5,7 +5,6 @@ use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKi
|
||||||
|
|
||||||
use crate::ast::helpers;
|
use crate::ast::helpers;
|
||||||
use crate::ast::types::{CheckLocator, Range};
|
use crate::ast::types::{CheckLocator, Range};
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckCode, CheckKind};
|
use crate::checks::{Check, CheckCode, CheckKind};
|
||||||
|
@ -50,7 +49,7 @@ pub fn duplicate_handler_exceptions(
|
||||||
),
|
),
|
||||||
checker.locate_check(Range::from_located(expr)),
|
checker.locate_check(Range::from_located(expr)),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// TODO(charlie): If we have a single element, remove the tuple.
|
// TODO(charlie): If we have a single element, remove the tuple.
|
||||||
let mut generator = SourceGenerator::new();
|
let mut generator = SourceGenerator::new();
|
||||||
if let Ok(()) = generator.unparse_expr(&type_pattern(unique_elts), 0) {
|
if let Ok(()) = generator.unparse_expr(&type_pattern(unique_elts), 0) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_ast::{Expr, Stmt, StmtKind};
|
use rustpython_ast::{Expr, Stmt, StmtKind};
|
||||||
|
|
||||||
use crate::autofix::{fixer, helpers};
|
use crate::autofix::helpers;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::CheckCode;
|
use crate::checks::CheckCode;
|
||||||
use crate::flake8_print::checks;
|
use crate::flake8_print::checks;
|
||||||
|
@ -13,7 +13,7 @@ pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
|
||||||
checker.settings.enabled.contains(&CheckCode::T201),
|
checker.settings.enabled.contains(&CheckCode::T201),
|
||||||
checker.settings.enabled.contains(&CheckCode::T203),
|
checker.settings.enabled.contains(&CheckCode::T203),
|
||||||
) {
|
) {
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
let context = checker.binding_context();
|
let context = checker.binding_context();
|
||||||
if matches!(
|
if matches!(
|
||||||
checker.parents[context.defined_by].node,
|
checker.parents[context.defined_by].node,
|
||||||
|
|
|
@ -6,7 +6,6 @@ use regex::Regex;
|
||||||
use rustpython_ast::{Arg, Constant, ExprKind, Location, StmtKind};
|
use rustpython_ast::{Arg, Constant, ExprKind, Location, StmtKind};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckCode, CheckKind};
|
use crate::checks::{Check, CheckCode, CheckKind};
|
||||||
|
@ -179,7 +178,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
|
||||||
CheckKind::NoBlankLineBeforeFunction(blank_lines_before),
|
CheckKind::NoBlankLineBeforeFunction(blank_lines_before),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete the blank line before the docstring.
|
// Delete the blank line before the docstring.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(docstring.location.row() - blank_lines_before, 1),
|
Location::new(docstring.location.row() - blank_lines_before, 1),
|
||||||
|
@ -217,7 +216,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
|
||||||
CheckKind::NoBlankLineAfterFunction(blank_lines_after),
|
CheckKind::NoBlankLineAfterFunction(blank_lines_after),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete the blank line after the docstring.
|
// Delete the blank line after the docstring.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(
|
Location::new(
|
||||||
|
@ -266,8 +265,7 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||||
CheckKind::NoBlankLineBeforeClass(blank_lines_before),
|
CheckKind::NoBlankLineBeforeClass(blank_lines_before),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
|
if checker.autofix.enabled() {
|
||||||
{
|
|
||||||
// Delete the blank line before the class.
|
// Delete the blank line before the class.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(docstring.location.row() - blank_lines_before, 1),
|
Location::new(docstring.location.row() - blank_lines_before, 1),
|
||||||
|
@ -283,8 +281,7 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||||
CheckKind::OneBlankLineBeforeClass(blank_lines_before),
|
CheckKind::OneBlankLineBeforeClass(blank_lines_before),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
|
if checker.autofix.enabled() {
|
||||||
{
|
|
||||||
// Insert one blank line before the class.
|
// Insert one blank line before the class.
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -316,7 +313,7 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||||
CheckKind::OneBlankLineAfterClass(blank_lines_after),
|
CheckKind::OneBlankLineAfterClass(blank_lines_after),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Insert a blank line before the class (replacing any existing lines).
|
// Insert a blank line before the class (replacing any existing lines).
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -358,7 +355,7 @@ pub fn blank_after_summary(checker: &mut Checker, definition: &Definition) {
|
||||||
CheckKind::BlankLineAfterSummary,
|
CheckKind::BlankLineAfterSummary,
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Insert one blank line after the summary (replacing any existing lines).
|
// Insert one blank line after the summary (replacing any existing lines).
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -417,7 +414,7 @@ pub fn indent(checker: &mut Checker, definition: &Definition) {
|
||||||
end_location: Location::new(docstring.location.row() + i, 1),
|
end_location: Location::new(docstring.location.row() + i, 1),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
helpers::clean(&docstring_indent),
|
helpers::clean(&docstring_indent),
|
||||||
Location::new(docstring.location.row() + i, 1),
|
Location::new(docstring.location.row() + i, 1),
|
||||||
|
@ -466,8 +463,7 @@ pub fn indent(checker: &mut Checker, definition: &Definition) {
|
||||||
end_location: Location::new(docstring.location.row() + i, 1),
|
end_location: Location::new(docstring.location.row() + i, 1),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
|
if checker.autofix.enabled() {
|
||||||
{
|
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
helpers::clean(&docstring_indent),
|
helpers::clean(&docstring_indent),
|
||||||
Location::new(docstring.location.row() + i, 1),
|
Location::new(docstring.location.row() + i, 1),
|
||||||
|
@ -494,7 +490,7 @@ pub fn indent(checker: &mut Checker, definition: &Definition) {
|
||||||
end_location: Location::new(docstring.location.row() + i, 1),
|
end_location: Location::new(docstring.location.row() + i, 1),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
helpers::clean(&docstring_indent),
|
helpers::clean(&docstring_indent),
|
||||||
Location::new(docstring.location.row() + i, 1),
|
Location::new(docstring.location.row() + i, 1),
|
||||||
|
@ -532,8 +528,7 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, definition: &Definiti
|
||||||
CheckKind::NewLineAfterLastParagraph,
|
CheckKind::NewLineAfterLastParagraph,
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
|
if checker.autofix.enabled() {
|
||||||
{
|
|
||||||
// Insert a newline just before the end-quote(s).
|
// Insert a newline just before the end-quote(s).
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"\n{}",
|
"\n{}",
|
||||||
|
@ -576,7 +571,7 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, definition: &Definition)
|
||||||
CheckKind::NoSurroundingWhitespace,
|
CheckKind::NoSurroundingWhitespace,
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
if let Some(first_line) = checker
|
if let Some(first_line) = checker
|
||||||
.locator
|
.locator
|
||||||
.slice_source_code_range(&Range::from_located(docstring))
|
.slice_source_code_range(&Range::from_located(docstring))
|
||||||
|
@ -911,7 +906,7 @@ fn blanks_and_section_underline(
|
||||||
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Add a dashed line (of the appropriate length) under the section header.
|
// Add a dashed line (of the appropriate length) under the section header.
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"{}{}\n",
|
"{}{}\n",
|
||||||
|
@ -945,7 +940,7 @@ fn blanks_and_section_underline(
|
||||||
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Add a dashed line (of the appropriate length) under the section header.
|
// Add a dashed line (of the appropriate length) under the section header.
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"{}{}\n",
|
"{}{}\n",
|
||||||
|
@ -967,7 +962,7 @@ fn blanks_and_section_underline(
|
||||||
),
|
),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete any blank lines between the header and content.
|
// Delete any blank lines between the header and content.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(docstring.location.row() + context.original_index + 1, 1),
|
Location::new(docstring.location.row() + context.original_index + 1, 1),
|
||||||
|
@ -990,7 +985,7 @@ fn blanks_and_section_underline(
|
||||||
CheckKind::SectionUnderlineAfterName(context.section_name.to_string()),
|
CheckKind::SectionUnderlineAfterName(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete any blank lines between the header and the underline.
|
// Delete any blank lines between the header and the underline.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(docstring.location.row() + context.original_index + 1, 1),
|
Location::new(docstring.location.row() + context.original_index + 1, 1),
|
||||||
|
@ -1021,7 +1016,7 @@ fn blanks_and_section_underline(
|
||||||
),
|
),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Replace the existing underline with a line of the appropriate length.
|
// Replace the existing underline with a line of the appropriate length.
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"{}{}\n",
|
"{}{}\n",
|
||||||
|
@ -1059,7 +1054,7 @@ fn blanks_and_section_underline(
|
||||||
CheckKind::SectionUnderlineNotOverIndented(context.section_name.to_string()),
|
CheckKind::SectionUnderlineNotOverIndented(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Replace the existing indentation with whitespace of the appropriate length.
|
// Replace the existing indentation with whitespace of the appropriate length.
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
helpers::clean(&indentation),
|
helpers::clean(&indentation),
|
||||||
|
@ -1108,7 +1103,7 @@ fn blanks_and_section_underline(
|
||||||
),
|
),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete any blank lines between the header and content.
|
// Delete any blank lines between the header and content.
|
||||||
check.amend(Fix::deletion(
|
check.amend(Fix::deletion(
|
||||||
Location::new(
|
Location::new(
|
||||||
|
@ -1167,7 +1162,7 @@ fn common_section(
|
||||||
CheckKind::CapitalizeSectionName(context.section_name.to_string()),
|
CheckKind::CapitalizeSectionName(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Replace the section title with the capitalized variant. This requires
|
// Replace the section title with the capitalized variant. This requires
|
||||||
// locating the start and end of the section name.
|
// locating the start and end of the section name.
|
||||||
if let Some(index) = context.line.find(&context.section_name) {
|
if let Some(index) = context.line.find(&context.section_name) {
|
||||||
|
@ -1200,7 +1195,7 @@ fn common_section(
|
||||||
CheckKind::SectionNotOverIndented(context.section_name.to_string()),
|
CheckKind::SectionNotOverIndented(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Replace the existing indentation with whitespace of the appropriate length.
|
// Replace the existing indentation with whitespace of the appropriate length.
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
helpers::clean(&indentation),
|
helpers::clean(&indentation),
|
||||||
|
@ -1227,7 +1222,7 @@ fn common_section(
|
||||||
CheckKind::BlankLineAfterLastSection(context.section_name.to_string()),
|
CheckKind::BlankLineAfterLastSection(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Add a newline after the section.
|
// Add a newline after the section.
|
||||||
check.amend(Fix::insertion(
|
check.amend(Fix::insertion(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -1248,7 +1243,7 @@ fn common_section(
|
||||||
CheckKind::BlankLineAfterSection(context.section_name.to_string()),
|
CheckKind::BlankLineAfterSection(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Add a newline after the section.
|
// Add a newline after the section.
|
||||||
check.amend(Fix::insertion(
|
check.amend(Fix::insertion(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -1272,7 +1267,7 @@ fn common_section(
|
||||||
CheckKind::BlankLineBeforeSection(context.section_name.to_string()),
|
CheckKind::BlankLineBeforeSection(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Add a blank line before the section.
|
// Add a blank line before the section.
|
||||||
check.amend(Fix::insertion(
|
check.amend(Fix::insertion(
|
||||||
"\n".to_string(),
|
"\n".to_string(),
|
||||||
|
@ -1432,7 +1427,7 @@ fn numpy_section(checker: &mut Checker, definition: &Definition, context: &Secti
|
||||||
CheckKind::NewLineAfterSectionName(context.section_name.to_string()),
|
CheckKind::NewLineAfterSectionName(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Delete the suffix. This requires locating the end of the section name.
|
// Delete the suffix. This requires locating the end of the section name.
|
||||||
if let Some(index) = context.line.find(&context.section_name) {
|
if let Some(index) = context.line.find(&context.section_name) {
|
||||||
// Map from bytes to characters.
|
// Map from bytes to characters.
|
||||||
|
@ -1481,7 +1476,7 @@ fn google_section(checker: &mut Checker, definition: &Definition, context: &Sect
|
||||||
CheckKind::SectionNameEndsInColon(context.section_name.to_string()),
|
CheckKind::SectionNameEndsInColon(context.section_name.to_string()),
|
||||||
Range::from_located(docstring),
|
Range::from_located(docstring),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
// Replace the suffix. This requires locating the end of the section name.
|
// Replace the suffix. This requires locating the end of the section name.
|
||||||
if let Some(index) = context.line.find(&context.section_name) {
|
if let Some(index) = context.line.find(&context.section_name) {
|
||||||
// Map from bytes to characters.
|
// Map from bytes to characters.
|
||||||
|
|
|
@ -4,7 +4,6 @@ use once_cell::sync::Lazy;
|
||||||
use rustpython_ast::{Expr, ExprKind};
|
use rustpython_ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckKind};
|
use crate::checks::{Check, CheckKind};
|
||||||
|
@ -38,7 +37,7 @@ pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) {
|
||||||
CheckKind::DeprecatedUnittestAlias(attr.to_string(), target.to_string()),
|
CheckKind::DeprecatedUnittestAlias(attr.to_string(), target.to_string()),
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
format!("self.{}", target),
|
format!("self.{}", target),
|
||||||
expr.location,
|
expr.location,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustpython_ast::{Expr, Stmt};
|
use rustpython_ast::{Expr, Stmt};
|
||||||
|
|
||||||
use crate::ast::helpers;
|
use crate::ast::helpers;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::pyupgrade;
|
use crate::pyupgrade;
|
||||||
use crate::pyupgrade::checks;
|
use crate::pyupgrade::checks;
|
||||||
|
@ -17,7 +16,7 @@ pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Exp
|
||||||
.map(|index| checker.parents[*index])
|
.map(|index| checker.parents[*index])
|
||||||
.collect();
|
.collect();
|
||||||
if let Some(mut check) = checks::super_args(scope, &parents, expr, func, args) {
|
if let Some(mut check) = checks::super_args(scope, &parents, expr, func, args) {
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
if let Some(fix) =
|
if let Some(fix) =
|
||||||
pyupgrade::fixes::remove_super_arguments(&mut checker.locator, expr)
|
pyupgrade::fixes::remove_super_arguments(&mut checker.locator, expr)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustpython_ast::Expr;
|
use rustpython_ast::Expr;
|
||||||
|
|
||||||
use crate::ast::types::{CheckLocator, Range};
|
use crate::ast::types::{CheckLocator, Range};
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::CheckKind;
|
use crate::checks::CheckKind;
|
||||||
|
@ -11,7 +10,7 @@ pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args:
|
||||||
if let Some(mut check) =
|
if let Some(mut check) =
|
||||||
checks::type_of_primitive(func, args, checker.locate_check(Range::from_located(expr)))
|
checks::type_of_primitive(func, args, checker.locate_check(Range::from_located(expr)))
|
||||||
{
|
{
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
if let CheckKind::TypeOfPrimitive(primitive) = &check.kind {
|
if let CheckKind::TypeOfPrimitive(primitive) = &check.kind {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
primitive.builtin(),
|
primitive.builtin(),
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustpython_ast::Expr;
|
use rustpython_ast::Expr;
|
||||||
|
|
||||||
use crate::ast::types::{CheckLocator, Range};
|
use crate::ast::types::{CheckLocator, Range};
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::pyupgrade::checks;
|
use crate::pyupgrade::checks;
|
||||||
|
@ -10,7 +9,7 @@ pub fn unnecessary_abspath(checker: &mut Checker, expr: &Expr, func: &Expr, args
|
||||||
if let Some(mut check) =
|
if let Some(mut check) =
|
||||||
checks::unnecessary_abspath(func, args, checker.locate_check(Range::from_located(expr)))
|
checks::unnecessary_abspath(func, args, checker.locate_check(Range::from_located(expr)))
|
||||||
{
|
{
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
"__file__".to_string(),
|
"__file__".to_string(),
|
||||||
expr.location,
|
expr.location,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustpython_ast::Expr;
|
use rustpython_ast::Expr;
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckKind};
|
use crate::checks::{Check, CheckKind};
|
||||||
|
@ -14,7 +13,7 @@ pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr, id: &str) {
|
||||||
CheckKind::UsePEP585Annotation(id.to_string()),
|
CheckKind::UsePEP585Annotation(id.to_string()),
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
);
|
);
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
check.amend(Fix::replacement(
|
check.amend(Fix::replacement(
|
||||||
id.to_lowercase(),
|
id.to_lowercase(),
|
||||||
expr.location,
|
expr.location,
|
||||||
|
|
|
@ -2,7 +2,6 @@ use rustpython_ast::{Constant, Expr, ExprKind, Operator};
|
||||||
|
|
||||||
use crate::ast::helpers::match_name_or_attr;
|
use crate::ast::helpers::match_name_or_attr;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::autofix::Fix;
|
use crate::autofix::Fix;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::checks::{Check, CheckKind};
|
use crate::checks::{Check, CheckKind};
|
||||||
|
@ -46,7 +45,7 @@ fn union(elts: &[Expr]) -> Expr {
|
||||||
pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) {
|
pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) {
|
||||||
if match_name_or_attr(value, "Optional") {
|
if match_name_or_attr(value, "Optional") {
|
||||||
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));
|
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
let mut generator = SourceGenerator::new();
|
let mut generator = SourceGenerator::new();
|
||||||
if let Ok(()) = generator.unparse_expr(&optional(slice), 0) {
|
if let Ok(()) = generator.unparse_expr(&optional(slice), 0) {
|
||||||
if let Ok(content) = generator.generate() {
|
if let Ok(content) = generator.generate() {
|
||||||
|
@ -61,7 +60,7 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s
|
||||||
checker.add_check(check);
|
checker.add_check(check);
|
||||||
} else if match_name_or_attr(value, "Union") {
|
} else if match_name_or_attr(value, "Union") {
|
||||||
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));
|
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
match &slice.node {
|
match &slice.node {
|
||||||
ExprKind::Slice { .. } => {
|
ExprKind::Slice { .. } => {
|
||||||
// Invalid type annotation.
|
// Invalid type annotation.
|
||||||
|
|
|
@ -2,7 +2,7 @@ use log::error;
|
||||||
use rustpython_ast::{Expr, Stmt};
|
use rustpython_ast::{Expr, Stmt};
|
||||||
|
|
||||||
use crate::ast::types::{CheckLocator, Range};
|
use crate::ast::types::{CheckLocator, Range};
|
||||||
use crate::autofix::{fixer, helpers};
|
use crate::autofix::helpers;
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::pyupgrade::checks;
|
use crate::pyupgrade::checks;
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr,
|
||||||
value,
|
value,
|
||||||
checker.locate_check(Range::from_located(stmt)),
|
checker.locate_check(Range::from_located(stmt)),
|
||||||
) {
|
) {
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
let context = checker.binding_context();
|
let context = checker.binding_context();
|
||||||
let deleted: Vec<&Stmt> = checker
|
let deleted: Vec<&Stmt> = checker
|
||||||
.deletions
|
.deletions
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use rustpython_ast::{Expr, Keyword, Stmt};
|
use rustpython_ast::{Expr, Keyword, Stmt};
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
|
||||||
use crate::check_ast::Checker;
|
use crate::check_ast::Checker;
|
||||||
use crate::pyupgrade;
|
use crate::pyupgrade;
|
||||||
use crate::pyupgrade::checks;
|
use crate::pyupgrade::checks;
|
||||||
|
@ -14,7 +13,7 @@ pub fn useless_object_inheritance(
|
||||||
) {
|
) {
|
||||||
let scope = checker.current_scope();
|
let scope = checker.current_scope();
|
||||||
if let Some(mut check) = checks::useless_object_inheritance(name, bases, scope) {
|
if let Some(mut check) = checks::useless_object_inheritance(name, bases, scope) {
|
||||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
if checker.autofix.enabled() {
|
||||||
if let Some(fix) = pyupgrade::fixes::remove_class_def_base(
|
if let Some(fix) = pyupgrade::fixes::remove_class_def_base(
|
||||||
&mut checker.locator,
|
&mut checker.locator,
|
||||||
&stmt.location,
|
&stmt.location,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue