mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
Add Diagnostic.try_amend()
to simplify error handling (#3701)
This commit is contained in:
parent
1bac206995
commit
dc4d7619ee
28 changed files with 192 additions and 300 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2131,6 +2131,8 @@ dependencies = [
|
||||||
name = "ruff_diagnostics"
|
name = "ruff_diagnostics"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"log",
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
"rustpython-parser",
|
"rustpython-parser",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -3797,21 +3797,12 @@ where
|
||||||
name_range,
|
name_range,
|
||||||
);
|
);
|
||||||
if self.patch(Rule::UnusedVariable) {
|
if self.patch(Rule::UnusedVariable) {
|
||||||
match pyflakes::fixes::remove_exception_handler_assignment(
|
diagnostic.try_amend(|| {
|
||||||
excepthandler,
|
pyflakes::fixes::remove_exception_handler_assignment(
|
||||||
self.locator,
|
excepthandler,
|
||||||
) {
|
self.locator,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Failed to remove exception handler \
|
|
||||||
assignment: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
self.diagnostics.push(diagnostic);
|
self.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Violation};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Violation};
|
||||||
|
@ -668,12 +667,9 @@ pub fn definition(
|
||||||
helpers::identifier_range(stmt, checker.locator),
|
helpers::identifier_range(stmt, checker.locator),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::add_return_annotation(checker.locator, stmt, "None") {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::add_return_annotation(checker.locator, stmt, "None")
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -693,12 +689,9 @@ pub fn definition(
|
||||||
let return_type = SIMPLE_MAGIC_RETURN_TYPES.get(name);
|
let return_type = SIMPLE_MAGIC_RETURN_TYPES.get(name);
|
||||||
if let Some(return_type) = return_type {
|
if let Some(return_type) = return_type {
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::add_return_annotation(checker.locator, stmt, return_type) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::add_return_annotation(checker.locator, stmt, return_type)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -87,12 +86,9 @@ pub fn unnecessary_call_around_sorted(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, Keyword};
|
use rustpython_parser::ast::{Expr, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -91,12 +90,9 @@ pub fn unnecessary_collection_call(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Comprehension, Expr, ExprKind};
|
use rustpython_parser::ast::{Comprehension, Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -68,12 +67,9 @@ fn add_diagnostic(checker: &mut Checker, expr: &Expr) {
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,13 +115,13 @@ pub fn unnecessary_double_cast_or_process(
|
||||||
{
|
{
|
||||||
let mut diagnostic = create_diagnostic(inner, outer, Range::from(expr));
|
let mut diagnostic = create_diagnostic(inner, outer, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
if let Ok(fix) = fixes::fix_unnecessary_double_cast_or_process(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_double_cast_or_process(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
) {
|
expr,
|
||||||
diagnostic.amend(fix);
|
)
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -60,17 +59,14 @@ pub fn unnecessary_generator_dict(
|
||||||
ExprKind::Tuple { elts, .. } if elts.len() == 2 => {
|
ExprKind::Tuple { elts, .. } if elts.len() == 2 => {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_generator_dict(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_generator_dict(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
parent,
|
expr,
|
||||||
) {
|
parent,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -60,12 +59,9 @@ pub fn unnecessary_generator_list(
|
||||||
if let ExprKind::GeneratorExp { .. } = argument {
|
if let ExprKind::GeneratorExp { .. } = argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorList, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorList, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -61,17 +60,9 @@ pub fn unnecessary_generator_set(
|
||||||
if let ExprKind::GeneratorExp { .. } = argument {
|
if let ExprKind::GeneratorExp { .. } = argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_generator_set(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr, parent)
|
||||||
checker.stylist,
|
});
|
||||||
expr,
|
|
||||||
parent,
|
|
||||||
) {
|
|
||||||
Ok(fix) => {
|
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -53,12 +52,8 @@ pub fn unnecessary_list_call(checker: &mut Checker, expr: &Expr, func: &Expr, ar
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListCall, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryListCall, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr) {
|
diagnostic
|
||||||
Ok(fix) => {
|
.try_amend(|| fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr));
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -66,13 +65,9 @@ pub fn unnecessary_list_comprehension_dict(
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionDict, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionDict, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
|
diagnostic.try_amend(|| {
|
||||||
{
|
fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
|
||||||
Ok(fix) => {
|
});
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -58,16 +57,13 @@ pub fn unnecessary_list_comprehension_set(
|
||||||
if let ExprKind::ListComp { .. } = &argument {
|
if let ExprKind::ListComp { .. } = &argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionSet, Range::from(expr));
|
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionSet, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_list_comprehension_set(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_list_comprehension_set(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
) {
|
expr,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -81,12 +80,9 @@ pub fn unnecessary_literal_dict(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -75,12 +74,9 @@ pub fn unnecessary_literal_set(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(fix) => {
|
fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -92,16 +91,9 @@ pub fn unnecessary_literal_within_list_call(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_literal_within_list_call(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_literal_within_list_call(checker.locator, checker.stylist, expr)
|
||||||
checker.stylist,
|
});
|
||||||
expr,
|
|
||||||
) {
|
|
||||||
Ok(fix) => {
|
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
||||||
|
@ -93,16 +92,9 @@ pub fn unnecessary_literal_within_tuple_call(
|
||||||
Range::from(expr),
|
Range::from(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_literal_within_tuple_call(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_literal_within_tuple_call(checker.locator, checker.stylist, expr)
|
||||||
checker.stylist,
|
});
|
||||||
expr,
|
|
||||||
) {
|
|
||||||
Ok(fix) => {
|
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
|
@ -108,18 +107,15 @@ pub fn unnecessary_map(
|
||||||
if args.len() == 2 && matches!(&args[0].node, ExprKind::Lambda { .. }) {
|
if args.len() == 2 && matches!(&args[0].node, ExprKind::Lambda { .. }) {
|
||||||
let mut diagnostic = create_diagnostic("generator", Range::from(expr));
|
let mut diagnostic = create_diagnostic("generator", Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_map(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_map(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
parent,
|
expr,
|
||||||
"generator",
|
parent,
|
||||||
) {
|
"generator",
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -140,18 +136,15 @@ pub fn unnecessary_map(
|
||||||
if let ExprKind::Lambda { .. } = argument {
|
if let ExprKind::Lambda { .. } = argument {
|
||||||
let mut diagnostic = create_diagnostic(id, Range::from(expr));
|
let mut diagnostic = create_diagnostic(id, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_map(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_map(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
parent,
|
expr,
|
||||||
id,
|
parent,
|
||||||
) {
|
id,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -173,18 +166,15 @@ pub fn unnecessary_map(
|
||||||
{
|
{
|
||||||
let mut diagnostic = create_diagnostic(id, Range::from(expr));
|
let mut diagnostic = create_diagnostic(id, Range::from(expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_map(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_map(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
parent,
|
expr,
|
||||||
id,
|
parent,
|
||||||
) {
|
id,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,21 +192,16 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) {
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
match delete_stmt(
|
diagnostic.try_amend(|| {
|
||||||
pass_stmt,
|
delete_stmt(
|
||||||
None,
|
pass_stmt,
|
||||||
&[],
|
None,
|
||||||
checker.locator,
|
&[],
|
||||||
checker.indexer,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.indexer,
|
||||||
) {
|
checker.stylist,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to delete `pass` statement: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
|
@ -353,16 +348,13 @@ pub fn unnecessary_comprehension_any_all(
|
||||||
let mut diagnostic =
|
let mut diagnostic =
|
||||||
Diagnostic::new(UnnecessaryComprehensionAnyAll, Range::from(&args[0]));
|
Diagnostic::new(UnnecessaryComprehensionAnyAll, Range::from(&args[0]));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fixes::fix_unnecessary_comprehension_any_all(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::fix_unnecessary_comprehension_any_all(
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
expr,
|
checker.stylist,
|
||||||
) {
|
expr,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -435,9 +435,8 @@ pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:
|
||||||
let mut diagnostic =
|
let mut diagnostic =
|
||||||
Diagnostic::new(PytestCompositeAssertion { fixable }, Range::from(stmt));
|
Diagnostic::new(PytestCompositeAssertion { fixable }, Range::from(stmt));
|
||||||
if fixable && checker.patch(diagnostic.kind.rule()) {
|
if fixable && checker.patch(diagnostic.kind.rule()) {
|
||||||
if let Ok(fix) = fix_composite_condition(stmt, checker.locator, checker.stylist) {
|
diagnostic
|
||||||
diagnostic.amend(fix);
|
.try_amend(|| fix_composite_condition(stmt, checker.locator, checker.stylist));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Arguments, Expr, ExprKind, Keyword, Location, Stmt, StmtKind};
|
use rustpython_parser::ast::{Arguments, Expr, ExprKind, Keyword, Location, Stmt, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
||||||
|
@ -317,19 +316,18 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
|
||||||
Range::from(scope_keyword),
|
Range::from(scope_keyword),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match fix_extraneous_scope_function(
|
let location = diagnostic.location;
|
||||||
checker.locator,
|
let end_location = diagnostic.end_location;
|
||||||
decorator.location,
|
diagnostic.try_amend(|| {
|
||||||
diagnostic.location,
|
fix_extraneous_scope_function(
|
||||||
diagnostic.end_location,
|
checker.locator,
|
||||||
args,
|
decorator.location,
|
||||||
keywords,
|
location,
|
||||||
) {
|
end_location,
|
||||||
Ok(fix) => {
|
args,
|
||||||
diagnostic.amend(fix);
|
keywords,
|
||||||
}
|
)
|
||||||
Err(e) => error!("Failed to generate fix: {e}"),
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
|
||||||
use log::error;
|
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, KeywordData};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, KeywordData};
|
||||||
|
|
||||||
|
@ -312,17 +311,14 @@ pub(crate) fn percent_format_extra_named_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match remove_unused_format_arguments_from_dict(
|
diagnostic.try_amend(|| {
|
||||||
&missing,
|
remove_unused_format_arguments_from_dict(
|
||||||
right,
|
&missing,
|
||||||
checker.locator,
|
right,
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
) {
|
checker.stylist,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to remove unused format arguments: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -478,17 +474,14 @@ pub(crate) fn string_dot_format_extra_named_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match remove_unused_keyword_arguments_from_format_call(
|
diagnostic.try_amend(|| {
|
||||||
&missing,
|
remove_unused_keyword_arguments_from_format_call(
|
||||||
location,
|
&missing,
|
||||||
checker.locator,
|
location,
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
) {
|
checker.stylist,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to remove unused keyword arguments: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -522,18 +515,15 @@ pub(crate) fn string_dot_format_extra_positional_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
match remove_unused_positional_arguments_from_format_call(
|
diagnostic.try_amend(|| {
|
||||||
&missing,
|
remove_unused_positional_arguments_from_format_call(
|
||||||
location,
|
&missing,
|
||||||
checker.locator,
|
location,
|
||||||
checker.stylist,
|
checker.locator,
|
||||||
&summary.format_string,
|
checker.stylist,
|
||||||
) {
|
&summary.format_string,
|
||||||
Ok(fix) => {
|
)
|
||||||
diagnostic.amend(fix);
|
});
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to remove unused positional arguments: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,12 +58,8 @@ pub fn remove_class_def_base(
|
||||||
expr_end: Location,
|
expr_end: Location,
|
||||||
bases: &[Expr],
|
bases: &[Expr],
|
||||||
keywords: &[Keyword],
|
keywords: &[Keyword],
|
||||||
) -> Option<Fix> {
|
) -> Result<Fix> {
|
||||||
if let Ok(fix) = remove_argument(locator, stmt_at, expr_at, expr_end, bases, keywords, true) {
|
remove_argument(locator, stmt_at, expr_at, expr_end, bases, keywords, true)
|
||||||
Some(fix)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate a fix to remove arguments from a `super` call.
|
/// Generate a fix to remove arguments from a `super` call.
|
||||||
|
|
|
@ -332,16 +332,17 @@ pub fn deprecated_mock_import(checker: &mut Checker, stmt: &Stmt) {
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
if let Some(indent) = indentation(checker.locator, stmt) {
|
if let Some(indent) = indentation(checker.locator, stmt) {
|
||||||
match format_import_from(stmt, indent, checker.locator, checker.stylist) {
|
diagnostic.try_amend(|| {
|
||||||
Ok(content) => {
|
format_import_from(stmt, indent, checker.locator, checker.stylist).map(
|
||||||
diagnostic.amend(Fix::replacement(
|
|content| {
|
||||||
content,
|
Fix::replacement(
|
||||||
stmt.location,
|
content,
|
||||||
stmt.end_location.unwrap(),
|
stmt.location,
|
||||||
));
|
stmt.end_location.unwrap(),
|
||||||
}
|
)
|
||||||
Err(e) => error!("Failed to rewrite `mock` import: {e}"),
|
},
|
||||||
}
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use log::error;
|
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, Location};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, Location};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
|
@ -122,12 +121,7 @@ fn create_check(
|
||||||
mode_param.end_location.unwrap(),
|
mode_param.end_location.unwrap(),
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
match create_remove_param_fix(locator, expr, mode_param) {
|
diagnostic.try_amend(|| create_remove_param_fix(locator, expr, mode_param));
|
||||||
Ok(fix) => {
|
|
||||||
diagnostic.amend(fix);
|
|
||||||
}
|
|
||||||
Err(e) => error!("Failed to remove parameter: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
diagnostic
|
diagnostic
|
||||||
|
|
|
@ -63,20 +63,21 @@ pub fn useless_object_inheritance(
|
||||||
bases: &[Expr],
|
bases: &[Expr],
|
||||||
keywords: &[Keyword],
|
keywords: &[Keyword],
|
||||||
) {
|
) {
|
||||||
let Some(mut diagnostic) = rule(name, bases, checker.ctx.scope(), &checker.ctx.bindings) else {
|
if let Some(mut diagnostic) = rule(name, bases, checker.ctx.scope(), &checker.ctx.bindings) {
|
||||||
return;
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
};
|
let location = diagnostic.location;
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
let end_location = diagnostic.end_location;
|
||||||
if let Some(fix) = fixes::remove_class_def_base(
|
diagnostic.try_amend(|| {
|
||||||
checker.locator,
|
fixes::remove_class_def_base(
|
||||||
stmt.location,
|
checker.locator,
|
||||||
diagnostic.location,
|
stmt.location,
|
||||||
diagnostic.end_location,
|
location,
|
||||||
bases,
|
end_location,
|
||||||
keywords,
|
bases,
|
||||||
) {
|
keywords,
|
||||||
diagnostic.amend(fix);
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ rust-version = { workspace = true }
|
||||||
[lib]
|
[lib]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = { workspace = true }
|
||||||
|
log = { workspace = true }
|
||||||
ruff_python_ast = { path = "../ruff_python_ast" }
|
ruff_python_ast = { path = "../ruff_python_ast" }
|
||||||
|
|
||||||
rustpython-parser = { workspace = true }
|
rustpython-parser = { workspace = true }
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
use log::error;
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -45,6 +47,14 @@ impl Diagnostic {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_amend(&mut self, func: impl FnOnce() -> Result<Fix>) -> &mut Self {
|
||||||
|
match func() {
|
||||||
|
Ok(fix) => self.fix = Some(fix),
|
||||||
|
Err(err) => error!("Failed to create fix: {}", err),
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parent(&mut self, parent: Location) -> &mut Self {
|
pub fn parent(&mut self, parent: Location) -> &mut Self {
|
||||||
self.parent = Some(parent);
|
self.parent = Some(parent);
|
||||||
self
|
self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue