mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:18 +00:00
Remove Fix::from(Edit)
and add deprecated replacement methods to Diagnostic
s (#4275)
This commit is contained in:
parent
0801f14046
commit
4d5a339d9e
51 changed files with 205 additions and 144 deletions
|
@ -10,7 +10,7 @@ use rustpython_parser::ast::{
|
||||||
ExprKind, KeywordData, Located, Operator, Pattern, PatternKind, Stmt, StmtKind, Suite,
|
ExprKind, KeywordData, Located, Operator, Pattern, PatternKind, Stmt, StmtKind, Suite,
|
||||||
};
|
};
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::{Diagnostic, Fix};
|
||||||
use ruff_python_ast::all::{extract_all_names, AllNamesFlags};
|
use ruff_python_ast::all::{extract_all_names, AllNamesFlags};
|
||||||
use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path};
|
use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path};
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
||||||
|
@ -4033,7 +4033,8 @@ where
|
||||||
name_range,
|
name_range,
|
||||||
);
|
);
|
||||||
if self.patch(Rule::UnusedVariable) {
|
if self.patch(Rule::UnusedVariable) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
pyflakes::fixes::remove_exception_handler_assignment(
|
pyflakes::fixes::remove_exception_handler_assignment(
|
||||||
excepthandler,
|
excepthandler,
|
||||||
self.locator,
|
self.locator,
|
||||||
|
@ -5323,8 +5324,8 @@ impl<'a> Checker<'a> {
|
||||||
if matches!(child.node, StmtKind::ImportFrom { .. }) {
|
if matches!(child.node, StmtKind::ImportFrom { .. }) {
|
||||||
diagnostic.set_parent(child.start());
|
diagnostic.set_parent(child.start());
|
||||||
}
|
}
|
||||||
if let Some(fix) = &fix {
|
if let Some(edit) = &fix {
|
||||||
diagnostic.set_fix(fix.clone());
|
diagnostic.set_fix(Fix::unspecified(edit.clone()));
|
||||||
}
|
}
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,7 +102,8 @@ pub fn check_noqa(
|
||||||
let mut diagnostic =
|
let mut diagnostic =
|
||||||
Diagnostic::new(UnusedNOQA { codes: None }, *noqa_range);
|
Diagnostic::new(UnusedNOQA { codes: None }, *noqa_range);
|
||||||
if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) {
|
if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) {
|
||||||
diagnostic.set_fix(delete_noqa(
|
#[allow(deprecated)]
|
||||||
|
diagnostic.set_fix_from_edit(delete_noqa(
|
||||||
*leading_spaces,
|
*leading_spaces,
|
||||||
*noqa_range,
|
*noqa_range,
|
||||||
*trailing_spaces,
|
*trailing_spaces,
|
||||||
|
@ -171,7 +172,8 @@ pub fn check_noqa(
|
||||||
);
|
);
|
||||||
if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) {
|
if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) {
|
||||||
if valid_codes.is_empty() {
|
if valid_codes.is_empty() {
|
||||||
diagnostic.set_fix(delete_noqa(
|
#[allow(deprecated)]
|
||||||
|
diagnostic.set_fix_from_edit(delete_noqa(
|
||||||
*leading_spaces,
|
*leading_spaces,
|
||||||
*range,
|
*range,
|
||||||
*trailing_spaces,
|
*trailing_spaces,
|
||||||
|
|
|
@ -666,7 +666,8 @@ 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()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::add_return_annotation(checker.locator, stmt, "None")
|
fixes::add_return_annotation(checker.locator, stmt, "None")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -688,7 +689,8 @@ 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()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::add_return_annotation(checker.locator, stmt, return_type)
|
fixes::add_return_annotation(checker.locator, stmt, return_type)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,8 @@ pub fn unnecessary_call_around_sorted(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,8 @@ pub fn unnecessary_collection_call(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,8 @@ fn add_diagnostic(checker: &mut Checker, expr: &Expr) {
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,8 @@ pub fn unnecessary_comprehension_any_all(
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryComprehensionAnyAll, args[0].range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryComprehensionAnyAll, args[0].range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_comprehension_any_all(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_comprehension_any_all(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,8 @@ pub fn unnecessary_double_cast_or_process(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_double_cast_or_process(
|
fixes::fix_unnecessary_double_cast_or_process(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
|
|
@ -58,7 +58,8 @@ 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, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_generator_dict(
|
fixes::fix_unnecessary_generator_dict(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
|
|
@ -58,7 +58,8 @@ pub fn unnecessary_generator_list(
|
||||||
if let ExprKind::GeneratorExp { .. } = argument {
|
if let ExprKind::GeneratorExp { .. } = argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorList, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorList, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,8 @@ pub fn unnecessary_generator_set(
|
||||||
if let ExprKind::GeneratorExp { .. } = argument {
|
if let ExprKind::GeneratorExp { .. } = argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr, parent)
|
fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr, parent)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,8 @@ pub fn unnecessary_list_call(checker: &mut Checker, expr: &Expr, func: &Expr, ar
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListCall, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryListCall, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,8 @@ pub fn unnecessary_list_comprehension_dict(
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionDict, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionDict, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,8 @@ pub fn unnecessary_list_comprehension_set(
|
||||||
if let ExprKind::ListComp { .. } = &argument {
|
if let ExprKind::ListComp { .. } = &argument {
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionSet, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryListComprehensionSet, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_list_comprehension_set(
|
fixes::fix_unnecessary_list_comprehension_set(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
|
|
@ -78,7 +78,8 @@ pub fn unnecessary_literal_dict(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,8 @@ pub fn unnecessary_literal_set(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,8 @@ pub fn unnecessary_literal_within_dict_call(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_literal_within_dict_call(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_literal_within_dict_call(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,8 @@ pub fn unnecessary_literal_within_list_call(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_literal_within_list_call(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_literal_within_list_call(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,8 @@ pub fn unnecessary_literal_within_tuple_call(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_literal_within_tuple_call(checker.locator, checker.stylist, expr)
|
fixes::fix_unnecessary_literal_within_tuple_call(checker.locator, checker.stylist, expr)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,7 +107,8 @@ 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", expr.range());
|
let mut diagnostic = create_diagnostic("generator", expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_map(
|
fixes::fix_unnecessary_map(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
@ -136,7 +137,8 @@ pub fn unnecessary_map(
|
||||||
if let ExprKind::Lambda { .. } = argument {
|
if let ExprKind::Lambda { .. } = argument {
|
||||||
let mut diagnostic = create_diagnostic(id, expr.range());
|
let mut diagnostic = create_diagnostic(id, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_map(
|
fixes::fix_unnecessary_map(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
@ -166,7 +168,8 @@ pub fn unnecessary_map(
|
||||||
{
|
{
|
||||||
let mut diagnostic = create_diagnostic(id, expr.range());
|
let mut diagnostic = create_diagnostic(id, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fixes::fix_unnecessary_map(
|
fixes::fix_unnecessary_map(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
|
|
|
@ -140,7 +140,8 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) {
|
||||||
pass_stmt.range().add_end(index),
|
pass_stmt.range().add_end(index),
|
||||||
)));
|
)));
|
||||||
} else {
|
} else {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
delete_stmt(
|
delete_stmt(
|
||||||
pass_stmt,
|
pass_stmt,
|
||||||
None,
|
None,
|
||||||
|
@ -208,7 +209,8 @@ pub fn duplicate_class_field_definition<'a, 'b>(
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(fix) => {
|
||||||
checker.deletions.insert(RefEquality(stmt));
|
checker.deletions.insert(RefEquality(stmt));
|
||||||
diagnostic.set_fix(fix);
|
#[allow(deprecated)]
|
||||||
|
diagnostic.set_fix_from_edit(fix);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to remove duplicate class definition: {}", err);
|
error!("Failed to remove duplicate class definition: {}", err);
|
||||||
|
|
|
@ -48,7 +48,8 @@ pub fn pass_in_class_body<'a>(checker: &mut Checker<'a>, parent: &'a Stmt, body:
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if fix.is_deletion() || fix.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(stmt));
|
checker.deletions.insert(RefEquality(stmt));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
#[allow(deprecated)]
|
||||||
|
diagnostic.set_fix_from_edit(fix);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to delete `pass` statement: {}", e);
|
error!("Failed to delete `pass` statement: {}", e);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
|
|
||||||
|
@ -23,7 +23,10 @@ impl AlwaysAutofixableViolation for QuotedAnnotationInStub {
|
||||||
pub fn quoted_annotation_in_stub(checker: &mut Checker, annotation: &str, range: TextRange) {
|
pub fn quoted_annotation_in_stub(checker: &mut Checker, annotation: &str, range: TextRange) {
|
||||||
let mut diagnostic = Diagnostic::new(QuotedAnnotationInStub, range);
|
let mut diagnostic = Diagnostic::new(QuotedAnnotationInStub, range);
|
||||||
if checker.patch(Rule::QuotedAnnotationInStub) {
|
if checker.patch(Rule::QuotedAnnotationInStub) {
|
||||||
diagnostic.set_fix(Edit::range_replacement(annotation.to_string(), range));
|
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
|
||||||
|
annotation.to_string(),
|
||||||
|
range,
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -426,8 +426,10 @@ pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:
|
||||||
&& !has_comments_in(stmt.range(), checker.locator);
|
&& !has_comments_in(stmt.range(), checker.locator);
|
||||||
let mut diagnostic = Diagnostic::new(PytestCompositeAssertion { fixable }, stmt.range());
|
let mut diagnostic = Diagnostic::new(PytestCompositeAssertion { fixable }, stmt.range());
|
||||||
if fixable && checker.patch(diagnostic.kind.rule()) {
|
if fixable && checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic
|
#[allow(deprecated)]
|
||||||
.try_set_fix(|| fix_composite_condition(stmt, checker.locator, checker.stylist));
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
|
fix_composite_condition(stmt, checker.locator, checker.stylist)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,7 +239,7 @@ fn has_abstractmethod_decorator(decorators: &[Expr], checker: &Checker) -> bool
|
||||||
fn pytest_fixture_parentheses(
|
fn pytest_fixture_parentheses(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
decorator: &Expr,
|
decorator: &Expr,
|
||||||
fix: Edit,
|
fix: Fix,
|
||||||
preferred: &str,
|
preferred: &str,
|
||||||
actual: &str,
|
actual: &str,
|
||||||
) {
|
) {
|
||||||
|
@ -283,7 +283,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
|
||||||
&& args.is_empty()
|
&& args.is_empty()
|
||||||
&& keywords.is_empty()
|
&& keywords.is_empty()
|
||||||
{
|
{
|
||||||
let fix = Edit::deletion(func.end(), decorator.end());
|
let fix = Fix::unspecified(Edit::deletion(func.end(), decorator.end()));
|
||||||
pytest_fixture_parentheses(checker, decorator, fix, "", "()");
|
pytest_fixture_parentheses(checker, decorator, fix, "", "()");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,7 +316,8 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
|
||||||
Diagnostic::new(PytestExtraneousScopeFunction, scope_keyword.range());
|
Diagnostic::new(PytestExtraneousScopeFunction, scope_keyword.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
let expr_range = diagnostic.range();
|
let expr_range = diagnostic.range();
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
fix_extraneous_scope_function(
|
fix_extraneous_scope_function(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
decorator.start(),
|
decorator.start(),
|
||||||
|
@ -338,7 +339,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
|
||||||
.enabled(Rule::PytestFixtureIncorrectParenthesesStyle)
|
.enabled(Rule::PytestFixtureIncorrectParenthesesStyle)
|
||||||
&& checker.settings.flake8_pytest_style.fixture_parentheses
|
&& checker.settings.flake8_pytest_style.fixture_parentheses
|
||||||
{
|
{
|
||||||
let fix = Edit::insertion("()".to_string(), decorator.end());
|
let fix = Fix::unspecified(Edit::insertion("()".to_string(), decorator.end()));
|
||||||
pytest_fixture_parentheses(checker, decorator, fix, "()", "");
|
pytest_fixture_parentheses(checker, decorator, fix, "()", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ fn pytest_mark_parentheses(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
decorator: &Expr,
|
decorator: &Expr,
|
||||||
call_path: &CallPath,
|
call_path: &CallPath,
|
||||||
fix: Edit,
|
fix: Fix,
|
||||||
preferred: &str,
|
preferred: &str,
|
||||||
actual: &str,
|
actual: &str,
|
||||||
) {
|
) {
|
||||||
|
@ -84,13 +84,13 @@ fn check_mark_parentheses(checker: &mut Checker, decorator: &Expr, call_path: &C
|
||||||
&& args.is_empty()
|
&& args.is_empty()
|
||||||
&& keywords.is_empty()
|
&& keywords.is_empty()
|
||||||
{
|
{
|
||||||
let fix = Edit::deletion(func.end(), decorator.end());
|
let fix = Fix::unspecified(Edit::deletion(func.end(), decorator.end()));
|
||||||
pytest_mark_parentheses(checker, decorator, call_path, fix, "", "()");
|
pytest_mark_parentheses(checker, decorator, call_path, fix, "", "()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if checker.settings.flake8_pytest_style.mark_parentheses {
|
if checker.settings.flake8_pytest_style.mark_parentheses {
|
||||||
let fix = Edit::insertion("()".to_string(), decorator.end());
|
let fix = Fix::unspecified(Edit::insertion("()".to_string(), decorator.end()));
|
||||||
pytest_mark_parentheses(checker, decorator, call_path, fix, "()", "");
|
pytest_mark_parentheses(checker, decorator, call_path, fix, "()", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -695,7 +695,7 @@ pub fn expr_or_true(checker: &mut Checker, expr: &Expr) {
|
||||||
edit.range(),
|
edit.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.set_fix(edit);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
@ -714,7 +714,7 @@ pub fn expr_and_false(checker: &mut Checker, expr: &Expr) {
|
||||||
edit.range(),
|
edit.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.set_fix(edit);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -297,14 +297,14 @@ pub fn nested_if_statements(
|
||||||
);
|
);
|
||||||
if fixable && checker.patch(diagnostic.kind.rule()) {
|
if fixable && checker.patch(diagnostic.kind.rule()) {
|
||||||
match fix_if::fix_nested_if_statements(checker.locator, checker.stylist, stmt) {
|
match fix_if::fix_nested_if_statements(checker.locator, checker.stylist, stmt) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix
|
if edit
|
||||||
.content()
|
.content()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.universal_newlines()
|
.universal_newlines()
|
||||||
.all(|line| line.width() <= checker.settings.line_length)
|
.all(|line| line.width() <= checker.settings.line_length)
|
||||||
{
|
{
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => error!("Failed to fix nested if: {err}"),
|
Err(err) => error!("Failed to fix nested if: {err}"),
|
||||||
|
|
|
@ -3,8 +3,8 @@ use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{Located, Stmt, StmtKind, Withitem};
|
use rustpython_parser::ast::{Located, Stmt, StmtKind, Withitem};
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
|
||||||
use ruff_diagnostics::{AutofixKind, Violation};
|
use ruff_diagnostics::{AutofixKind, Violation};
|
||||||
|
use ruff_diagnostics::{Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{first_colon_range, has_comments_in};
|
use ruff_python_ast::helpers::{first_colon_range, has_comments_in};
|
||||||
use ruff_python_ast::newlines::StrExt;
|
use ruff_python_ast::newlines::StrExt;
|
||||||
|
@ -111,14 +111,14 @@ pub fn multiple_with_statements(
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
with_stmt,
|
with_stmt,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix
|
if edit
|
||||||
.content()
|
.content()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.universal_newlines()
|
.universal_newlines()
|
||||||
.all(|line| line.width() <= checker.settings.line_length)
|
.all(|line| line.width() <= checker.settings.line_length)
|
||||||
{
|
{
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => error!("Failed to fix nested with: {err}"),
|
Err(err) => error!("Failed to fix nested with: {err}"),
|
||||||
|
|
|
@ -2,7 +2,7 @@ use rustpython_parser::ast::{Stmt, StmtKind};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation, CacheKey};
|
use ruff_macros::{derive_message_formats, violation, CacheKey};
|
||||||
use ruff_python_ast::helpers::{create_stmt, resolve_imported_module_path, unparse_stmt};
|
use ruff_python_ast::helpers::{create_stmt, resolve_imported_module_path, unparse_stmt};
|
||||||
use ruff_python_ast::source_code::Stylist;
|
use ruff_python_ast::source_code::Stylist;
|
||||||
|
@ -91,7 +91,7 @@ fn fix_banned_relative_import(
|
||||||
module: Option<&str>,
|
module: Option<&str>,
|
||||||
module_path: Option<&[String]>,
|
module_path: Option<&[String]>,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
) -> Option<Edit> {
|
) -> Option<Fix> {
|
||||||
// Only fix is the module path is known.
|
// Only fix is the module path is known.
|
||||||
let Some(module_path) = resolve_imported_module_path(level, module, module_path) else {
|
let Some(module_path) = resolve_imported_module_path(level, module, module_path) else {
|
||||||
return None;
|
return None;
|
||||||
|
@ -115,7 +115,10 @@ fn fix_banned_relative_import(
|
||||||
stylist,
|
stylist,
|
||||||
);
|
);
|
||||||
|
|
||||||
Some(Edit::range_replacement(content, stmt.range()))
|
Some(Fix::unspecified(Edit::range_replacement(
|
||||||
|
content,
|
||||||
|
stmt.range(),
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TID252
|
/// TID252
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_parser::ast::{Stmt, StmtKind};
|
use rustpython_parser::ast::{Stmt, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
|
||||||
|
@ -70,11 +70,11 @@ pub fn empty_type_checking_block<'a, 'b>(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if edit.is_deletion() || edit.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(stmt));
|
checker.deletions.insert(RefEquality(stmt));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
Err(e) => error!("Failed to remove empty type-checking block: {e}"),
|
Err(e) => error!("Failed to remove empty type-checking block: {e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser as parser;
|
use rustpython_parser as parser;
|
||||||
use rustpython_parser::ast::{StmtKind, Suite};
|
use rustpython_parser::ast::{StmtKind, Suite};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::is_docstring_stmt;
|
use ruff_python_ast::helpers::is_docstring_stmt;
|
||||||
use ruff_python_ast::imports::{Alias, AnyImport, FutureImport, Import, ImportFrom};
|
use ruff_python_ast::imports::{Alias, AnyImport, FutureImport, Import, ImportFrom};
|
||||||
|
@ -120,10 +120,10 @@ fn add_required_import(
|
||||||
TextRange::default(),
|
TextRange::default(),
|
||||||
);
|
);
|
||||||
if autofix.into() && settings.rules.should_fix(Rule::MissingRequiredImport) {
|
if autofix.into() && settings.rules.should_fix(Rule::MissingRequiredImport) {
|
||||||
diagnostic.set_fix(
|
diagnostic.set_fix(Fix::unspecified(
|
||||||
Importer::new(python_ast, locator, stylist)
|
Importer::new(python_ast, locator, stylist)
|
||||||
.add_import(required_import, TextSize::default()),
|
.add_import(required_import, TextSize::default()),
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
Some(diagnostic)
|
Some(diagnostic)
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,7 +138,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) {
|
||||||
} else {
|
} else {
|
||||||
Edit::range_replacement(new_indent, over_indented)
|
Edit::range_replacement(new_indent, over_indented)
|
||||||
};
|
};
|
||||||
diagnostic.set_fix(edit);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{Expr, ExprKind};
|
use rustpython_parser::ast::{Expr, ExprKind};
|
||||||
use rustpython_parser::{lexer, Mode, StringKind, Tok};
|
use rustpython_parser::{lexer, Mode, StringKind, Tok};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
|
|
||||||
|
@ -87,13 +87,13 @@ fn fix_f_string_missing_placeholders(
|
||||||
prefix_range: TextRange,
|
prefix_range: TextRange,
|
||||||
tok_range: TextRange,
|
tok_range: TextRange,
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
) -> Edit {
|
) -> Fix {
|
||||||
let content = &checker.locator.contents()[TextRange::new(prefix_range.end(), tok_range.end())];
|
let content = &checker.locator.contents()[TextRange::new(prefix_range.end(), tok_range.end())];
|
||||||
Edit::replacement(
|
Fix::unspecified(Edit::replacement(
|
||||||
unescape_f_string(content),
|
unescape_f_string(content),
|
||||||
prefix_range.start(),
|
prefix_range.start(),
|
||||||
tok_range.end(),
|
tok_range.end(),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// F541
|
/// F541
|
||||||
|
|
|
@ -311,7 +311,8 @@ pub(crate) fn percent_format_extra_named_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_unused_format_arguments_from_dict(
|
remove_unused_format_arguments_from_dict(
|
||||||
&missing,
|
&missing,
|
||||||
right,
|
right,
|
||||||
|
@ -474,7 +475,8 @@ pub(crate) fn string_dot_format_extra_named_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_unused_keyword_arguments_from_format_call(
|
remove_unused_keyword_arguments_from_format_call(
|
||||||
&missing,
|
&missing,
|
||||||
location,
|
location,
|
||||||
|
@ -518,7 +520,8 @@ pub(crate) fn string_dot_format_extra_positional_arguments(
|
||||||
location,
|
location,
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_unused_positional_arguments_from_format_call(
|
remove_unused_positional_arguments_from_format_call(
|
||||||
&missing,
|
&missing,
|
||||||
location,
|
location,
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{ExprKind, Located, Stmt, StmtKind};
|
use rustpython_parser::ast::{ExprKind, Located, Stmt, StmtKind};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::contains_effect;
|
use ruff_python_ast::helpers::contains_effect;
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
|
@ -194,7 +194,7 @@ fn remove_unused_variable(
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
checker: &Checker,
|
checker: &Checker,
|
||||||
) -> Option<(DeletionKind, Edit)> {
|
) -> Option<(DeletionKind, Fix)> {
|
||||||
// First case: simple assignment (`x = 1`)
|
// First case: simple assignment (`x = 1`)
|
||||||
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
|
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
|
||||||
if let Some(target) = targets.iter().find(|target| range == target.range()) {
|
if let Some(target) = targets.iter().find(|target| range == target.range()) {
|
||||||
|
@ -206,11 +206,11 @@ fn remove_unused_variable(
|
||||||
// but preserve the right-hand side.
|
// but preserve the right-hand side.
|
||||||
Some((
|
Some((
|
||||||
DeletionKind::Partial,
|
DeletionKind::Partial,
|
||||||
Edit::deletion(
|
Fix::unspecified(Edit::deletion(
|
||||||
target.start(),
|
target.start(),
|
||||||
match_token_after(target, checker.locator, |tok| tok == Tok::Equal)
|
match_token_after(target, checker.locator, |tok| tok == Tok::Equal)
|
||||||
.start(),
|
.start(),
|
||||||
),
|
)),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
// If (e.g.) assigning to a constant (`x = 1`), delete the entire statement.
|
// If (e.g.) assigning to a constant (`x = 1`), delete the entire statement.
|
||||||
|
@ -224,7 +224,7 @@ fn remove_unused_variable(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => Some((DeletionKind::Whole, fix)),
|
Ok(fix) => Some((DeletionKind::Whole, Fix::unspecified(fix))),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to delete unused variable: {}", err);
|
error!("Failed to delete unused variable: {}", err);
|
||||||
None
|
None
|
||||||
|
@ -248,10 +248,10 @@ fn remove_unused_variable(
|
||||||
// but preserve the right-hand side.
|
// but preserve the right-hand side.
|
||||||
Some((
|
Some((
|
||||||
DeletionKind::Partial,
|
DeletionKind::Partial,
|
||||||
Edit::deletion(
|
Fix::unspecified(Edit::deletion(
|
||||||
stmt.start(),
|
stmt.start(),
|
||||||
match_token_after(stmt, checker.locator, |tok| tok == Tok::Equal).start(),
|
match_token_after(stmt, checker.locator, |tok| tok == Tok::Equal).start(),
|
||||||
),
|
)),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
// If assigning to a constant (`x = 1`), delete the entire statement.
|
// If assigning to a constant (`x = 1`), delete the entire statement.
|
||||||
|
@ -265,7 +265,7 @@ fn remove_unused_variable(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => Some((DeletionKind::Whole, fix)),
|
Ok(edit) => Some((DeletionKind::Whole, Fix::unspecified(edit))),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to delete unused variable: {}", err);
|
error!("Failed to delete unused variable: {}", err);
|
||||||
None
|
None
|
||||||
|
@ -284,7 +284,7 @@ fn remove_unused_variable(
|
||||||
if optional_vars.range() == range {
|
if optional_vars.range() == range {
|
||||||
return Some((
|
return Some((
|
||||||
DeletionKind::Partial,
|
DeletionKind::Partial,
|
||||||
Edit::deletion(
|
Fix::unspecified(Edit::deletion(
|
||||||
item.context_expr.end(),
|
item.context_expr.end(),
|
||||||
// The end of the `Withitem` is the colon, comma, or closing
|
// The end of the `Withitem` is the colon, comma, or closing
|
||||||
// parenthesis following the `optional_vars`.
|
// parenthesis following the `optional_vars`.
|
||||||
|
@ -292,7 +292,7 @@ fn remove_unused_variable(
|
||||||
tok == Tok::Colon || tok == Tok::Comma || tok == Tok::Rpar
|
tok == Tok::Colon || tok == Tok::Comma || tok == Tok::Rpar
|
||||||
})
|
})
|
||||||
.start(),
|
.start(),
|
||||||
),
|
)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::TextSize;
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Violation};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{has_comments, unparse_expr};
|
use ruff_python_ast::helpers::{has_comments, unparse_expr};
|
||||||
use ruff_python_semantic::context::Context;
|
use ruff_python_semantic::context::Context;
|
||||||
|
@ -121,10 +121,10 @@ pub fn nested_min_max(
|
||||||
keywords: keywords.to_owned(),
|
keywords: keywords.to_owned(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
diagnostic.set_fix(Edit::range_replacement(
|
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
|
||||||
unparse_expr(&flattened_expr, checker.stylist),
|
unparse_expr(&flattened_expr, checker.stylist),
|
||||||
expr.range(),
|
expr.range(),
|
||||||
));
|
)));
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt, StmtKind};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{is_const_none, ReturnStatementVisitor};
|
use ruff_python_ast::helpers::{is_const_none, ReturnStatementVisitor};
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
@ -116,11 +116,11 @@ pub fn useless_return<'a>(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if edit.is_deletion() || edit.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(last_stmt));
|
checker.deletions.insert(RefEquality(last_stmt));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to delete `return` statement: {}", e);
|
error!("Failed to delete `return` statement: {}", e);
|
||||||
|
|
|
@ -2,7 +2,7 @@ use anyhow::{bail, Result};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Keyword, Stmt, StmtKind};
|
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Keyword, Stmt, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{create_expr, create_stmt, unparse_stmt};
|
use ruff_python_ast::helpers::{create_expr, create_stmt, unparse_stmt};
|
||||||
use ruff_python_ast::source_code::Stylist;
|
use ruff_python_ast::source_code::Stylist;
|
||||||
|
@ -162,11 +162,11 @@ fn convert_to_class(
|
||||||
body: Vec<Stmt>,
|
body: Vec<Stmt>,
|
||||||
base_class: &Expr,
|
base_class: &Expr,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
) -> Edit {
|
) -> Fix {
|
||||||
Edit::range_replacement(
|
Fix::unspecified(Edit::range_replacement(
|
||||||
unparse_stmt(&create_class_def_stmt(typename, body, base_class), stylist),
|
unparse_stmt(&create_class_def_stmt(typename, body, base_class), stylist),
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UP014
|
/// UP014
|
||||||
|
|
|
@ -2,7 +2,7 @@ use anyhow::{bail, Result};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Keyword, Stmt, StmtKind};
|
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Keyword, Stmt, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{create_expr, create_stmt, unparse_stmt};
|
use ruff_python_ast::helpers::{create_expr, create_stmt, unparse_stmt};
|
||||||
use ruff_python_ast::source_code::Stylist;
|
use ruff_python_ast::source_code::Stylist;
|
||||||
|
@ -209,14 +209,14 @@ fn convert_to_class(
|
||||||
total_keyword: Option<&Keyword>,
|
total_keyword: Option<&Keyword>,
|
||||||
base_class: &Expr,
|
base_class: &Expr,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
) -> Edit {
|
) -> Fix {
|
||||||
Edit::range_replacement(
|
Fix::unspecified(Edit::range_replacement(
|
||||||
unparse_stmt(
|
unparse_stmt(
|
||||||
&create_class_def_stmt(class_name, body, total_keyword, base_class),
|
&create_class_def_stmt(class_name, body, total_keyword, base_class),
|
||||||
stylist,
|
stylist,
|
||||||
),
|
),
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UP013
|
/// UP013
|
||||||
|
|
|
@ -331,7 +331,8 @@ 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) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
format_import_from(stmt, indent, checker.locator, checker.stylist)
|
format_import_from(stmt, indent, checker.locator, checker.stylist)
|
||||||
.map(|content| Edit::range_replacement(content, stmt.range()))
|
.map(|content| Edit::range_replacement(content, stmt.range()))
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,7 +6,7 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{Cmpop, Constant, Expr, ExprKind, Located, Stmt};
|
use rustpython_parser::ast::{Cmpop, Constant, Expr, ExprKind, Located, Stmt};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
@ -155,7 +155,7 @@ fn fix_py2_block(
|
||||||
body: &[Stmt],
|
body: &[Stmt],
|
||||||
orelse: &[Stmt],
|
orelse: &[Stmt],
|
||||||
block: &BlockMetadata,
|
block: &BlockMetadata,
|
||||||
) -> Option<Edit> {
|
) -> Option<Fix> {
|
||||||
if orelse.is_empty() {
|
if orelse.is_empty() {
|
||||||
// Delete the entire statement. If this is an `elif`, know it's the only child
|
// Delete the entire statement. If this is an `elif`, know it's the only child
|
||||||
// of its parent, so avoid passing in the parent at all. Otherwise,
|
// of its parent, so avoid passing in the parent at all. Otherwise,
|
||||||
|
@ -175,9 +175,9 @@ fn fix_py2_block(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
checker.deletions.insert(RefEquality(defined_by));
|
checker.deletions.insert(RefEquality(defined_by));
|
||||||
Some(fix)
|
Some(Fix::unspecified(edit))
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to remove block: {}", err);
|
error!("Failed to remove block: {}", err);
|
||||||
|
@ -193,13 +193,13 @@ fn fix_py2_block(
|
||||||
|
|
||||||
if indentation(checker.locator, start).is_none() {
|
if indentation(checker.locator, start).is_none() {
|
||||||
// Inline `else` block (e.g., `else: x = 1`).
|
// Inline `else` block (e.g., `else: x = 1`).
|
||||||
Some(Edit::range_replacement(
|
Some(Fix::unspecified(Edit::range_replacement(
|
||||||
checker
|
checker
|
||||||
.locator
|
.locator
|
||||||
.slice(TextRange::new(start.start(), end.end()))
|
.slice(TextRange::new(start.start(), end.end()))
|
||||||
.to_string(),
|
.to_string(),
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
))
|
)))
|
||||||
} else {
|
} else {
|
||||||
indentation(checker.locator, stmt)
|
indentation(checker.locator, stmt)
|
||||||
.and_then(|indentation| {
|
.and_then(|indentation| {
|
||||||
|
@ -212,11 +212,11 @@ fn fix_py2_block(
|
||||||
.ok()
|
.ok()
|
||||||
})
|
})
|
||||||
.map(|contents| {
|
.map(|contents| {
|
||||||
Edit::replacement(
|
Fix::unspecified(Edit::replacement(
|
||||||
contents,
|
contents,
|
||||||
checker.locator.line_start(stmt.start()),
|
checker.locator.line_start(stmt.start()),
|
||||||
stmt.end(),
|
stmt.end(),
|
||||||
)
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -233,7 +233,7 @@ fn fix_py2_block(
|
||||||
end_location = body.last().unwrap().end();
|
end_location = body.last().unwrap().end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Edit::deletion(stmt.start(), end_location))
|
Some(Fix::unspecified(Edit::deletion(stmt.start(), end_location)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ fn fix_py3_block(
|
||||||
test: &Expr,
|
test: &Expr,
|
||||||
body: &[Stmt],
|
body: &[Stmt],
|
||||||
block: &BlockMetadata,
|
block: &BlockMetadata,
|
||||||
) -> Option<Edit> {
|
) -> Option<Fix> {
|
||||||
match block.starter {
|
match block.starter {
|
||||||
Tok::If => {
|
Tok::If => {
|
||||||
// If the first statement is an if, use the body of this statement, and ignore
|
// If the first statement is an if, use the body of this statement, and ignore
|
||||||
|
@ -254,13 +254,13 @@ fn fix_py3_block(
|
||||||
|
|
||||||
if indentation(checker.locator, start).is_none() {
|
if indentation(checker.locator, start).is_none() {
|
||||||
// Inline `if` block (e.g., `if ...: x = 1`).
|
// Inline `if` block (e.g., `if ...: x = 1`).
|
||||||
Some(Edit::range_replacement(
|
Some(Fix::unspecified(Edit::range_replacement(
|
||||||
checker
|
checker
|
||||||
.locator
|
.locator
|
||||||
.slice(TextRange::new(start.start(), end.end()))
|
.slice(TextRange::new(start.start(), end.end()))
|
||||||
.to_string(),
|
.to_string(),
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
))
|
)))
|
||||||
} else {
|
} else {
|
||||||
indentation(checker.locator, stmt)
|
indentation(checker.locator, stmt)
|
||||||
.and_then(|indentation| {
|
.and_then(|indentation| {
|
||||||
|
@ -273,11 +273,11 @@ fn fix_py3_block(
|
||||||
.ok()
|
.ok()
|
||||||
})
|
})
|
||||||
.map(|contents| {
|
.map(|contents| {
|
||||||
Edit::replacement(
|
Fix::unspecified(Edit::replacement(
|
||||||
contents,
|
contents,
|
||||||
checker.locator.line_start(stmt.start()),
|
checker.locator.line_start(stmt.start()),
|
||||||
stmt.end(),
|
stmt.end(),
|
||||||
)
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,7 +286,10 @@ fn fix_py3_block(
|
||||||
// the rest.
|
// the rest.
|
||||||
let end = body.last().unwrap();
|
let end = body.last().unwrap();
|
||||||
let text = checker.locator.slice(TextRange::new(test.end(), end.end()));
|
let text = checker.locator.slice(TextRange::new(test.end(), end.end()));
|
||||||
Some(Edit::range_replacement(format!("else{text}"), stmt.range()))
|
Some(Fix::unspecified(Edit::range_replacement(
|
||||||
|
format!("else{text}"),
|
||||||
|
stmt.range(),
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,8 @@ fn create_check(
|
||||||
mode_param.range(),
|
mode_param.range(),
|
||||||
)));
|
)));
|
||||||
} else {
|
} else {
|
||||||
diagnostic.try_set_fix(|| create_remove_param_fix(locator, expr, mode_param));
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| create_remove_param_fix(locator, expr, mode_param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
diagnostic
|
diagnostic
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use rustpython_parser::ast::{ArgData, Expr, ExprKind, StmtKind};
|
use rustpython_parser::ast::{ArgData, Expr, ExprKind, StmtKind};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::scope::ScopeKind;
|
use ruff_python_semantic::scope::ScopeKind;
|
||||||
|
|
||||||
|
@ -96,8 +96,8 @@ pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Exp
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(SuperCallWithParameters, expr.range());
|
let mut diagnostic = Diagnostic::new(SuperCallWithParameters, expr.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
if let Some(fix) = fixes::remove_super_arguments(checker.locator, checker.stylist, expr) {
|
if let Some(edit) = fixes::remove_super_arguments(checker.locator, checker.stylist, expr) {
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
|
|
|
@ -2,7 +2,7 @@ use itertools::Itertools;
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_parser::ast::{Alias, AliasData, Located, Stmt};
|
use rustpython_parser::ast::{Alias, AliasData, Located, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
|
||||||
|
@ -121,11 +121,11 @@ pub fn unnecessary_builtin_import(
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if edit.is_deletion() || edit.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(defined_by));
|
checker.deletions.insert(RefEquality(defined_by));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
Err(e) => error!("Failed to remove builtin import: {e}"),
|
Err(e) => error!("Failed to remove builtin import: {e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
|
|
||||||
|
@ -106,8 +106,8 @@ fn match_encoding_arg<'a>(args: &'a [Expr], kwargs: &'a [Keyword]) -> Option<Enc
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return an [`Edit`] replacing the call to encode with a byte string.
|
/// Return a [`Fix`] replacing the call to encode with a byte string.
|
||||||
fn replace_with_bytes_literal(locator: &Locator, expr: &Expr, constant: &Expr) -> Edit {
|
fn replace_with_bytes_literal(locator: &Locator, expr: &Expr, constant: &Expr) -> Fix {
|
||||||
// Build up a replacement string by prefixing all string tokens with `b`.
|
// Build up a replacement string by prefixing all string tokens with `b`.
|
||||||
let contents = locator.slice(constant.range());
|
let contents = locator.slice(constant.range());
|
||||||
let mut replacement = String::with_capacity(contents.len() + 1);
|
let mut replacement = String::with_capacity(contents.len() + 1);
|
||||||
|
@ -129,7 +129,7 @@ fn replace_with_bytes_literal(locator: &Locator, expr: &Expr, constant: &Expr) -
|
||||||
}
|
}
|
||||||
prev = Some(range.end());
|
prev = Some(range.end());
|
||||||
}
|
}
|
||||||
Edit::range_replacement(replacement, expr.range())
|
Fix::unspecified(Edit::range_replacement(replacement, expr.range()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UP012
|
/// UP012
|
||||||
|
@ -176,7 +176,8 @@ pub fn unnecessary_encode_utf8(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_argument(
|
remove_argument(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
func.start(),
|
func.start(),
|
||||||
|
@ -197,7 +198,8 @@ pub fn unnecessary_encode_utf8(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_argument(
|
remove_argument(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
func.start(),
|
func.start(),
|
||||||
|
@ -225,7 +227,8 @@ pub fn unnecessary_encode_utf8(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_argument(
|
remove_argument(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
func.start(),
|
func.start(),
|
||||||
|
@ -246,7 +249,8 @@ pub fn unnecessary_encode_utf8(
|
||||||
expr.range(),
|
expr.range(),
|
||||||
);
|
);
|
||||||
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
if checker.patch(Rule::UnnecessaryEncodeUTF8) {
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_argument(
|
remove_argument(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
func.start(),
|
func.start(),
|
||||||
|
|
|
@ -2,7 +2,7 @@ use itertools::Itertools;
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_parser::ast::{Alias, AliasData, Located, Stmt};
|
use rustpython_parser::ast::{Alias, AliasData, Located, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if fix.is_deletion() || fix.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(defined_by));
|
checker.deletions.insert(RefEquality(defined_by));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(fix));
|
||||||
}
|
}
|
||||||
Err(e) => error!("Failed to remove `__future__` import: {e}"),
|
Err(e) => error!("Failed to remove `__future__` import: {e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use log::error;
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Stmt};
|
use rustpython_parser::ast::{Expr, ExprKind, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::types::RefEquality;
|
use ruff_python_ast::types::RefEquality;
|
||||||
|
|
||||||
|
@ -61,11 +61,11 @@ pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr,
|
||||||
checker.indexer,
|
checker.indexer,
|
||||||
checker.stylist,
|
checker.stylist,
|
||||||
) {
|
) {
|
||||||
Ok(fix) => {
|
Ok(edit) => {
|
||||||
if fix.is_deletion() || fix.content() == Some("pass") {
|
if edit.is_deletion() || edit.content() == Some("pass") {
|
||||||
checker.deletions.insert(RefEquality(defined_by));
|
checker.deletions.insert(RefEquality(defined_by));
|
||||||
}
|
}
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
Err(e) => error!("Failed to fix remove metaclass type: {e}"),
|
Err(e) => error!("Failed to fix remove metaclass type: {e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,8 @@ pub fn useless_object_inheritance(
|
||||||
if let Some(mut diagnostic) = rule(name, bases, checker.ctx.scope(), &checker.ctx.bindings) {
|
if let Some(mut diagnostic) = rule(name, bases, checker.ctx.scope(), &checker.ctx.bindings) {
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
let expr_range = diagnostic.range();
|
let expr_range = diagnostic.range();
|
||||||
diagnostic.try_set_fix(|| {
|
#[allow(deprecated)]
|
||||||
|
diagnostic.try_set_fix_from_edit(|| {
|
||||||
remove_argument(
|
remove_argument(
|
||||||
checker.locator,
|
checker.locator,
|
||||||
stmt.start(),
|
stmt.start(),
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::Fix;
|
use crate::{Edit, Fix};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
@ -40,14 +40,21 @@ impl Diagnostic {
|
||||||
|
|
||||||
/// Set the [`Fix`] used to fix the diagnostic.
|
/// Set the [`Fix`] used to fix the diagnostic.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_fix<T: Into<Fix>>(&mut self, fix: T) {
|
pub fn set_fix(&mut self, fix: Fix) {
|
||||||
self.fix = Some(fix.into());
|
self.fix = Some(fix);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the [`Fix`] used to fix the diagnostic.
|
||||||
|
#[inline]
|
||||||
|
#[deprecated(note = "Use `Diagnostic::set_fix` instead.")]
|
||||||
|
pub fn set_fix_from_edit(&mut self, edit: Edit) {
|
||||||
|
self.fix = Some(Fix::unspecified(edit));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consumes `self` and returns a new `Diagnostic` with the given `fix`.
|
/// Consumes `self` and returns a new `Diagnostic` with the given `fix`.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_fix<T: Into<Fix>>(mut self, fix: T) -> Self {
|
pub fn with_fix(mut self, fix: Fix) -> Self {
|
||||||
self.set_fix(fix);
|
self.set_fix(fix);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -55,9 +62,20 @@ impl Diagnostic {
|
||||||
/// Set the [`Fix`] used to fix the diagnostic, if the provided function returns `Ok`.
|
/// Set the [`Fix`] used to fix the diagnostic, if the provided function returns `Ok`.
|
||||||
/// Otherwise, log the error.
|
/// Otherwise, log the error.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_set_fix<T: Into<Fix>>(&mut self, func: impl FnOnce() -> Result<T>) {
|
pub fn try_set_fix(&mut self, func: impl FnOnce() -> Result<Fix>) {
|
||||||
match func() {
|
match func() {
|
||||||
Ok(fix) => self.fix = Some(fix.into()),
|
Ok(fix) => self.fix = Some(fix),
|
||||||
|
Err(err) => error!("Failed to create fix: {}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets an [`Edit`] used to fix the diagnostic, if the provided function returns `Ok`.
|
||||||
|
/// Otherwise, log the error.
|
||||||
|
#[inline]
|
||||||
|
#[deprecated(note = "Use Diagnostic::try_set_fix instead")]
|
||||||
|
pub fn try_set_fix_from_edit(&mut self, func: impl FnOnce() -> Result<Edit>) {
|
||||||
|
match func() {
|
||||||
|
Ok(edit) => self.fix = Some(Fix::unspecified(edit)),
|
||||||
Err(err) => error!("Failed to create fix: {}", err),
|
Err(err) => error!("Failed to create fix: {}", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,9 +38,3 @@ impl Fix {
|
||||||
self.edits
|
self.edits
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Edit> for Fix {
|
|
||||||
fn from(edit: Edit) -> Self {
|
|
||||||
Self { edits: vec![edit] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue