diff --git a/crates/ruff/src/autofix/mod.rs b/crates/ruff/src/autofix/mod.rs index 50283071e7af9..09802b4374e77 100644 --- a/crates/ruff/src/autofix/mod.rs +++ b/crates/ruff/src/autofix/mod.rs @@ -16,7 +16,7 @@ pub mod actions; pub fn fix_file(diagnostics: &[Diagnostic], locator: &Locator) -> Option<(String, FixTable)> { let mut with_fixes = diagnostics .iter() - .filter(|diag| !diag.fix.is_empty()) + .filter(|diag| diag.fix.is_some()) .peekable(); if with_fixes.peek().is_none() { @@ -38,11 +38,10 @@ fn apply_fixes<'a>( for (rule, fix) in diagnostics .filter_map(|diagnostic| { - if diagnostic.fix.is_empty() { - None - } else { - Some((diagnostic.kind.rule(), &diagnostic.fix)) - } + diagnostic + .fix + .as_ref() + .map(|fix| (diagnostic.kind.rule(), fix)) }) .sorted_by(|(rule1, fix1), (rule2, fix2)| cmp_fix(*rule1, *rule2, fix1, fix2)) { @@ -103,6 +102,7 @@ mod tests { use ruff_diagnostics::Diagnostic; use ruff_diagnostics::Edit; + use ruff_diagnostics::Fix; use ruff_python_ast::source_code::Locator; use crate::autofix::apply_fixes; @@ -114,7 +114,7 @@ mod tests { // The choice of rule here is arbitrary. kind: MissingNewlineAtEndOfFile.into(), range: edit.range(), - fix: edit.into(), + fix: Some(Fix::unspecified(edit)), parent: None, }) .collect() diff --git a/crates/ruff/src/checkers/logical_lines.rs b/crates/ruff/src/checkers/logical_lines.rs index d51ea0937d272..0480f59d5945b 100644 --- a/crates/ruff/src/checkers/logical_lines.rs +++ b/crates/ruff/src/checkers/logical_lines.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextRange; use rustpython_parser::lexer::LexResult; -use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix}; +use ruff_diagnostics::{Diagnostic, DiagnosticKind}; use ruff_python_ast::source_code::{Locator, Stylist}; use ruff_python_ast::token_kind::TokenKind; @@ -144,7 +144,7 @@ impl<'a> LogicalLinesContext<'a> { self.diagnostics.push(Diagnostic { kind, range, - fix: Fix::empty(), + fix: None, parent: None, }); } diff --git a/crates/ruff/src/checkers/noqa.rs b/crates/ruff/src/checkers/noqa.rs index 25ad27a9992cd..d1657b5f56192 100644 --- a/crates/ruff/src/checkers/noqa.rs +++ b/crates/ruff/src/checkers/noqa.rs @@ -3,7 +3,7 @@ use itertools::Itertools; use ruff_text_size::{TextLen, TextRange, TextSize}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_python_ast::source_code::Locator; use crate::noqa; @@ -178,10 +178,10 @@ pub fn check_noqa( locator, )); } else { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!("# noqa: {}", valid_codes.join(", ")), *range, - )); + ))); } } diagnostics.push(diagnostic); diff --git a/crates/ruff/src/message/diff.rs b/crates/ruff/src/message/diff.rs index 76b9ef2901ace..12026f4555ffb 100644 --- a/crates/ruff/src/message/diff.rs +++ b/crates/ruff/src/message/diff.rs @@ -22,14 +22,10 @@ pub(super) struct Diff<'a> { impl<'a> Diff<'a> { pub fn from_message(message: &'a Message) -> Option { - if message.fix.is_empty() { - None - } else { - Some(Diff { - source_code: &message.file, - fix: &message.fix, - }) - } + message.fix.as_ref().map(|fix| Diff { + source_code: &message.file, + fix, + }) } } diff --git a/crates/ruff/src/message/json.rs b/crates/ruff/src/message/json.rs index 1e980f78b59f0..ebb12053cdb76 100644 --- a/crates/ruff/src/message/json.rs +++ b/crates/ruff/src/message/json.rs @@ -37,14 +37,12 @@ impl Serialize for ExpandedMessages<'_> { for message in self.messages { let source_code = message.file.to_source_code(); - let fix = if message.fix.is_empty() { - None - } else { - Some(json!({ - "message": message.kind.suggestion.as_deref(), - "edits": &ExpandedEdits { edits: message.fix.edits(), source_code: &source_code }, - })) - }; + let fix = message.fix.as_ref().map(|fix| { + json!({ + "message": message.kind.suggestion.as_deref(), + "edits": &ExpandedEdits { edits: fix.edits(), source_code: &source_code }, + }) + }); let start_location = source_code.source_location(message.start()); let end_location = source_code.source_location(message.end()); diff --git a/crates/ruff/src/message/mod.rs b/crates/ruff/src/message/mod.rs index 41c1776a884ba..4f2cd6f685ec6 100644 --- a/crates/ruff/src/message/mod.rs +++ b/crates/ruff/src/message/mod.rs @@ -33,7 +33,7 @@ use ruff_python_ast::source_code::{SourceFile, SourceLocation}; pub struct Message { pub kind: DiagnosticKind, pub range: TextRange, - pub fix: Fix, + pub fix: Option, pub file: SourceFile, pub noqa_offset: TextSize, } @@ -191,10 +191,10 @@ def fibonacci(n): }, TextRange::new(TextSize::from(94), TextSize::from(95)), ) - .with_fix(Fix::new(vec![Edit::deletion( + .with_fix(Fix::unspecified(Edit::deletion( TextSize::from(94), TextSize::from(99), - )])); + ))); let file_2 = r#"if a == 1: pass"#; diff --git a/crates/ruff/src/rules/eradicate/rules.rs b/crates/ruff/src/rules/eradicate/rules.rs index 2a67f4ec9ea5e..c2f730c4cb59a 100644 --- a/crates/ruff/src/rules/eradicate/rules.rs +++ b/crates/ruff/src/rules/eradicate/rules.rs @@ -1,6 +1,6 @@ use ruff_text_size::TextRange; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -58,7 +58,9 @@ pub fn commented_out_code( if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) { let mut diagnostic = Diagnostic::new(CommentedOutCode, range); if autofix.into() && settings.rules.should_fix(Rule::CommentedOutCode) { - diagnostic.set_fix(Edit::range_deletion(locator.full_lines_range(range))); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion( + locator.full_lines_range(range), + ))); } Some(diagnostic) } else { diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs b/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs index f49db94cee7a5..43ca5081c095b 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs @@ -94,7 +94,7 @@ fn fix_abstractmethod_missing( ), stmt.range().start(), ); - Ok(Fix::from_iter([import_edit, reference_edit])) + Ok(Fix::unspecified_edits(import_edit, [reference_edit])) } /// B024 diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs b/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs index 801a1fae98615..3f5b130e8da32 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextSize; use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_stmt; @@ -63,10 +63,10 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option let mut diagnostic = Diagnostic::new(AssertFalse, test.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_stmt(&assertion_error(msg), checker.stylist), stmt.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs b/crates/ruff/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs index 3c6def1b6bb02..c0233d7608e0a 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs @@ -4,7 +4,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind}; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path; use ruff_python_ast::call_path::CallPath; @@ -96,14 +96,14 @@ fn duplicate_handler_exceptions<'a>( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( if unique_elts.len() == 1 { unparse_expr(unique_elts[0], checker.stylist) } else { unparse_expr(&type_pattern(unique_elts), checker.stylist) }, expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs b/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs index 22019ae69f0f8..67c9b54717f73 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextSize; use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_expr; use ruff_python_stdlib::identifiers::{is_identifier, is_mangled_private}; @@ -64,10 +64,10 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar let mut diagnostic = Diagnostic::new(GetAttrWithConstant, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&attribute(obj, value), checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs b/crates/ruff/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs index 21412249a5f3b..90f87c7644cbb 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_expr; @@ -47,10 +47,10 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E type_.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(elt, checker.stylist), type_.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs b/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs index 705c1e03a3e72..edf5345b3b65d 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextSize; use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_stmt; use ruff_python_ast::source_code::Stylist; @@ -79,10 +79,10 @@ pub fn setattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar let mut diagnostic = Diagnostic::new(SetAttrWithConstant, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( assignment(obj, name, value, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs b/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs index 7e53161e5d4f2..6edcba20adefa 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs @@ -22,7 +22,7 @@ use rustc_hash::FxHashMap; use rustpython_parser::ast::{Expr, ExprKind, Stmt}; 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}; use ruff_python_ast::types::RefEquality; use ruff_python_ast::visitor::Visitor; @@ -176,7 +176,10 @@ pub fn unused_loop_control_variable( if let Some(binding) = binding { if binding.kind.is_loop_var() { if !binding.used() { - diagnostic.set_fix(Edit::range_replacement(rename, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + rename, + expr.range(), + ))); } } } diff --git a/crates/ruff/src/rules/flake8_commas/rules.rs b/crates/ruff/src/rules/flake8_commas/rules.rs index 64228455af7fc..37f67af2b9dba 100644 --- a/crates/ruff/src/rules/flake8_commas/rules.rs +++ b/crates/ruff/src/rules/flake8_commas/rules.rs @@ -4,7 +4,7 @@ use rustpython_parser::lexer::{LexResult, Spanned}; use rustpython_parser::Tok; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -333,7 +333,7 @@ pub fn trailing_commas( let comma = prev.spanned.unwrap(); let mut diagnostic = Diagnostic::new(ProhibitedTrailingComma, comma.1); if autofix.into() && settings.rules.should_fix(Rule::ProhibitedTrailingComma) { - diagnostic.set_fix(Edit::range_deletion(diagnostic.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(diagnostic.range()))); } diagnostics.push(diagnostic); } @@ -373,10 +373,10 @@ pub fn trailing_commas( // removing any brackets in the same linter pass - doing both at the same time could // lead to a syntax error. let contents = locator.slice(missing_comma.1); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!("{contents},"), missing_comma.1, - )); + ))); } diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_errmsg/rules.rs b/crates/ruff/src/rules/flake8_errmsg/rules.rs index ac30f0d7c02cf..9a03d131db827 100644 --- a/crates/ruff/src/rules/flake8_errmsg/rules.rs +++ b/crates/ruff/src/rules/flake8_errmsg/rules.rs @@ -203,7 +203,7 @@ fn generate_fix(stylist: &Stylist, stmt: &Stmt, exc_arg: &Expr, indentation: &st }), stylist, ); - Fix::from_iter([ + Fix::unspecified_edits( Edit::insertion( format!( "{}{}{}", @@ -213,8 +213,11 @@ fn generate_fix(stylist: &Stylist, stmt: &Stmt, exc_arg: &Expr, indentation: &st ), stmt.start(), ), - Edit::range_replacement(String::from("msg"), exc_arg.range()), - ]) + [Edit::range_replacement( + String::from("msg"), + exc_arg.range(), + )], + ) } /// EM101, EM102, EM103 diff --git a/crates/ruff/src/rules/flake8_executable/rules/shebang_whitespace.rs b/crates/ruff/src/rules/flake8_executable/rules/shebang_whitespace.rs index a50105cadcabb..3bf409e337f41 100644 --- a/crates/ruff/src/rules/flake8_executable/rules/shebang_whitespace.rs +++ b/crates/ruff/src/rules/flake8_executable/rules/shebang_whitespace.rs @@ -1,6 +1,6 @@ use ruff_text_size::{TextRange, TextSize}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::rules::flake8_executable::helpers::ShebangDirective; @@ -32,10 +32,10 @@ pub fn shebang_whitespace( TextRange::at(range.start(), *n_spaces), ); if autofix { - diagnostic.set_fix(Edit::range_deletion(TextRange::at( + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(TextRange::at( range.start(), *n_spaces, - ))); + )))); } Some(diagnostic) } else { diff --git a/crates/ruff/src/rules/flake8_logging_format/rules.rs b/crates/ruff/src/rules/flake8_logging_format/rules.rs index 0f09459327e36..48917201bcfe6 100644 --- a/crates/ruff/src/rules/flake8_logging_format/rules.rs +++ b/crates/ruff/src/rules/flake8_logging_format/rules.rs @@ -2,7 +2,7 @@ use ruff_text_size::{TextRange, TextSize}; use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, Operator}; use std::ops::Add; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_python_ast::helpers::{find_keyword, SimpleCallArgs}; use ruff_python_semantic::analyze::logging; use ruff_python_stdlib::logging::LoggingLevel; @@ -171,10 +171,10 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: { let mut diagnostic = Diagnostic::new(LoggingWarn, level_call_range); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "warning".to_string(), level_call_range, - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pie/rules.rs b/crates/ruff/src/rules/flake8_pie/rules.rs index 48f4acb4d4c2d..a58362169d059 100644 --- a/crates/ruff/src/rules/flake8_pie/rules.rs +++ b/crates/ruff/src/rules/flake8_pie/rules.rs @@ -9,7 +9,7 @@ use rustpython_parser::ast::{ }; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::comparable::ComparableExpr; use ruff_python_ast::helpers::{create_expr, trailing_comment_start_offset, unparse_expr}; @@ -136,7 +136,9 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) { let mut diagnostic = Diagnostic::new(UnnecessaryPass, pass_stmt.range()); if checker.patch(diagnostic.kind.rule()) { if let Some(index) = trailing_comment_start_offset(pass_stmt, checker.locator) { - diagnostic.set_fix(Edit::range_deletion(pass_stmt.range().add_end(index))); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion( + pass_stmt.range().add_end(index), + ))); } else { diagnostic.try_set_fix(|| { delete_stmt( @@ -414,10 +416,10 @@ pub fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) { .collect(), }); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&bool_op, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -439,7 +441,10 @@ pub fn reimplemented_list_builtin(checker: &mut Checker, expr: &Expr) { if elts.is_empty() { let mut diagnostic = Diagnostic::new(ReimplementedListBuiltin, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("list".to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "list".to_string(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pyi/rules/duplicate_union_member.rs b/crates/ruff/src/rules/flake8_pyi/rules/duplicate_union_member.rs index 8af4c510051d7..94b7c1e959be9 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/duplicate_union_member.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/duplicate_union_member.rs @@ -1,7 +1,7 @@ use rustc_hash::FxHashSet; use rustpython_parser::ast::{Expr, ExprKind, Operator}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::comparable::ComparableExpr; use ruff_python_ast::helpers::unparse_expr; @@ -77,13 +77,13 @@ fn traverse_union<'a>( }; // Replace the parent with its non-duplicate child. - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( if expr.node == left.node { right } else { left }, checker.stylist, ), parent.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs index 5313b68af4efb..468044057c861 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind, Operator, Unaryop}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_semantic::context::Context; @@ -298,10 +298,10 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) { Diagnostic::new(TypedArgumentDefaultInStub, default.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "...".to_string(), default.range(), - )); + ))); } checker.diagnostics.push(diagnostic); @@ -324,10 +324,10 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) { Diagnostic::new(TypedArgumentDefaultInStub, default.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "...".to_string(), default.range(), - )); + ))); } checker.diagnostics.push(diagnostic); @@ -353,10 +353,10 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) { Diagnostic::new(ArgumentDefaultInStub, default.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "...".to_string(), default.range(), - )); + ))); } checker.diagnostics.push(diagnostic); @@ -379,10 +379,10 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) { Diagnostic::new(ArgumentDefaultInStub, default.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "...".to_string(), default.range(), - )); + ))); } checker.diagnostics.push(diagnostic); @@ -410,7 +410,10 @@ pub fn assignment_default_in_stub(checker: &mut Checker, targets: &[Expr], value let mut diagnostic = Diagnostic::new(AssignmentDefaultInStub, value.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("...".to_string(), value.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "...".to_string(), + value.range(), + ))); } checker.diagnostics.push(diagnostic); } @@ -437,7 +440,10 @@ pub fn annotated_assignment_default_in_stub( let mut diagnostic = Diagnostic::new(AssignmentDefaultInStub, value.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("...".to_string(), value.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "...".to_string(), + value.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs index df6ef9c688192..bebf6a3dc99d1 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs @@ -9,7 +9,7 @@ use rustpython_parser::ast::{ Boolop, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Keyword, Stmt, StmtKind, Unaryop, }; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{has_comments_in, unparse_stmt, Truthiness}; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -203,10 +203,10 @@ pub fn unittest_assertion( ); if fixable && checker.patch(diagnostic.kind.rule()) { if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_stmt(&stmt, checker.stylist), expr.range(), - )); + ))); } } Some(diagnostic) diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs index f98ace22b240e..cba4636f82181 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs @@ -3,7 +3,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize}; use rustpython_parser::ast::{Arguments, Expr, ExprKind, Keyword, Stmt, StmtKind}; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::collect_call_path; use ruff_python_ast::helpers::collect_arg_names; @@ -398,10 +398,10 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: & stmt.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "return".to_string(), TextRange::at(stmt.start(), "yield".text_len()), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -470,7 +470,7 @@ fn check_fixture_marks(checker: &mut Checker, decorators: &[Expr]) { Diagnostic::new(PytestUnnecessaryAsyncioMarkOnFixture, expr.range()); if checker.patch(diagnostic.kind.rule()) { let range = checker.locator.full_lines_range(expr.range()); - diagnostic.set_fix(Edit::range_deletion(range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range))); } checker.diagnostics.push(diagnostic); } @@ -486,7 +486,7 @@ fn check_fixture_marks(checker: &mut Checker, decorators: &[Expr]) { Diagnostic::new(PytestErroneousUseFixturesOnFixture, expr.range()); if checker.patch(diagnostic.kind.rule()) { let line_range = checker.locator.full_lines_range(expr.range()); - diagnostic.set_fix(Edit::range_deletion(line_range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(line_range))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs index e8375fb42fa18..320370b63bd11 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextSize; use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::CallPath; @@ -113,9 +113,9 @@ fn check_useless_usefixtures(checker: &mut Checker, decorator: &Expr, call_path: if !has_parameters { let mut diagnostic = Diagnostic::new(PytestUseFixturesWithoutParameters, decorator.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_deletion( + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion( decorator.range().sub_start(TextSize::from(1)), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs index e72b3b11cbaa8..1f57ed9ae2eba 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind}; use rustpython_parser::{lexer, Mode, Tok}; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, unparse_expr}; @@ -148,7 +148,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { name_range, ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!( "({})", unparse_expr( @@ -168,7 +168,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { ) ), name_range, - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -181,7 +181,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { name_range, ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::List { elts: names @@ -198,7 +198,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { checker.stylist, ), name_range, - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -222,7 +222,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::List { elts: elts.clone(), @@ -231,7 +231,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { checker.stylist, ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -244,7 +244,10 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { if let Some(content) = elts_to_csv(elts, checker) { - diagnostic.set_fix(Edit::range_replacement(content, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + content, + expr.range(), + ))); } } checker.diagnostics.push(diagnostic); @@ -268,7 +271,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!( "({})", unparse_expr( @@ -280,7 +283,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { ) ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -293,7 +296,10 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { if let Some(content) = elts_to_csv(elts, checker) { - diagnostic.set_fix(Edit::range_replacement(content, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + content, + expr.range(), + ))); } } checker.diagnostics.push(diagnostic); @@ -366,10 +372,10 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&create_expr(value.node.clone()), checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_quotes/rules.rs b/crates/ruff/src/rules/flake8_quotes/rules.rs index b706126755752..e4b0a92741fee 100644 --- a/crates/ruff/src/rules/flake8_quotes/rules.rs +++ b/crates/ruff/src/rules/flake8_quotes/rules.rs @@ -2,7 +2,7 @@ use ruff_text_size::TextRange; use rustpython_parser::lexer::LexResult; use rustpython_parser::Tok; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -289,7 +289,10 @@ fn docstring( fixed_contents.push_str("e); fixed_contents.push_str(string_contents); fixed_contents.push_str("e); - diagnostic.set_fix(Edit::range_replacement(fixed_contents, range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + fixed_contents, + range, + ))); } Some(diagnostic) } @@ -364,7 +367,10 @@ fn strings( fixed_contents.push_str(quote); fixed_contents.push_str(string_contents); fixed_contents.push_str(quote); - diagnostic.set_fix(Edit::range_replacement(fixed_contents, *range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + fixed_contents, + *range, + ))); } diagnostics.push(diagnostic); } else { @@ -427,7 +433,10 @@ fn strings( fixed_contents.push(quote); - diagnostic.set_fix(Edit::range_replacement(fixed_contents, *range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + fixed_contents, + *range, + ))); } diagnostics.push(diagnostic); } @@ -450,7 +459,10 @@ fn strings( fixed_contents.push(quote); fixed_contents.push_str(string_contents); fixed_contents.push(quote); - diagnostic.set_fix(Edit::range_replacement(fixed_contents, *range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + fixed_contents, + *range, + ))); } diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs b/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs index 6d6564af70699..fc4351b7b898c 100644 --- a/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs +++ b/crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::match_parens; @@ -34,7 +34,7 @@ pub fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: &Expr) .expect("Expected call to include parentheses"); let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, range); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::deletion(func.end(), range.end())); + diagnostic.set_fix(Fix::unspecified(Edit::deletion(func.end(), range.end()))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_return/rules.rs b/crates/ruff/src/rules/flake8_return/rules.rs index 88e8e6694c54a..d064948c53789 100644 --- a/crates/ruff/src/rules/flake8_return/rules.rs +++ b/crates/ruff/src/rules/flake8_return/rules.rs @@ -3,7 +3,7 @@ use ruff_text_size::{TextRange, TextSize}; use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt, StmtKind}; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::elif_else_range; use ruff_python_ast::helpers::is_const_none; @@ -345,7 +345,10 @@ fn unnecessary_return_none(checker: &mut Checker, stack: &Stack) { } let mut diagnostic = Diagnostic::new(UnnecessaryReturnNone, stmt.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("return".to_string(), stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "return".to_string(), + stmt.range(), + ))); } checker.diagnostics.push(diagnostic); } @@ -359,10 +362,10 @@ fn implicit_return_value(checker: &mut Checker, stack: &Stack) { } let mut diagnostic = Diagnostic::new(ImplicitReturnValue, stmt.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "return None".to_string(), stmt.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -416,10 +419,10 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) { content.push_str(checker.stylist.line_ending().as_str()); content.push_str(indent); content.push_str("return None"); - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( content, end_of_last_statement(stmt, checker.locator), - )); + ))); } } checker.diagnostics.push(diagnostic); @@ -454,10 +457,10 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) { content.push_str(checker.stylist.line_ending().as_str()); content.push_str(indent); content.push_str("return None"); - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( content, end_of_last_statement(stmt, checker.locator), - )); + ))); } } checker.diagnostics.push(diagnostic); @@ -493,10 +496,10 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) { content.push_str(checker.stylist.line_ending().as_str()); content.push_str(indent); content.push_str("return None"); - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( content, end_of_last_statement(stmt, checker.locator), - )); + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs index 5c2d571d6c729..1af11793405ff 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -7,7 +7,7 @@ use ruff_text_size::TextRange; use rustc_hash::FxHashMap; use rustpython_parser::ast::{Boolop, Cmpop, Expr, ExprContext, ExprKind, Unaryop}; -use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::comparable::ComparableExpr; use ruff_python_ast::helpers::{ @@ -366,10 +366,10 @@ pub fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) { // Populate the `Fix`. Replace the _entire_ `BoolOp`. Note that if we have // multiple duplicates, the fixes will conflict. - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&bool_op, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -468,10 +468,10 @@ pub fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { values: iter::once(in_expr).chain(unmatched).collect(), }) }; - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&in_expr, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -519,7 +519,10 @@ pub fn expr_and_not_expr(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("False".to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "False".to_string(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } @@ -569,7 +572,10 @@ pub fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("True".to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "True".to_string(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs index d010dd109c08b..da285d9ff11fd 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Constant, Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, unparse_expr}; @@ -116,10 +116,10 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) { value: capital_env_var.into(), kind: kind.clone(), }); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&new_env_var, checker.stylist), slice.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -176,7 +176,10 @@ pub fn dict_get_with_none_default(checker: &mut Checker, expr: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(expected, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + expected, + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs index 1a33073175f3e..35b7dd4678273 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs @@ -4,7 +4,7 @@ use rustc_hash::FxHashSet; use rustpython_parser::ast::{Cmpop, Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind}; use unicode_width::UnicodeWidthStr; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::comparable::{ComparableConstant, ComparableExpr, ComparableStmt}; use ruff_python_ast::helpers::{ @@ -370,7 +370,7 @@ pub fn needless_bool(checker: &mut Checker, stmt: &Stmt) { if fixable && checker.patch(diagnostic.kind.rule()) { if matches!(test.node, ExprKind::Compare { .. }) { // If the condition is a comparison, we can replace it with the condition. - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_stmt( &create_stmt(StmtKind::Return { value: Some(test.clone()), @@ -378,11 +378,11 @@ pub fn needless_bool(checker: &mut Checker, stmt: &Stmt) { checker.stylist, ), stmt.range(), - )); + ))); } else { // Otherwise, we need to wrap the condition in a call to `bool`. (We've already // verified, above, that `bool` is a builtin.) - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_stmt( &create_stmt(StmtKind::Return { value: Some(Box::new(create_expr(ExprKind::Call { @@ -397,7 +397,7 @@ pub fn needless_bool(checker: &mut Checker, stmt: &Stmt) { checker.stylist, ), stmt.range(), - )); + ))); }; } checker.diagnostics.push(diagnostic); @@ -528,7 +528,10 @@ pub fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: Option<& stmt.range(), ); if fixable && checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(contents, stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + stmt.range(), + ))); } checker.diagnostics.push(diagnostic); } @@ -875,7 +878,10 @@ pub fn use_dict_get_with_default( stmt.range(), ); if fixable && checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(contents, stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + stmt.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs index 0d9ccc6f7d750..552df89e3ed3f 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Unaryop}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, unparse_expr}; @@ -100,12 +100,12 @@ pub fn explicit_true_false_in_ifexpr( ); if checker.patch(diagnostic.kind.rule()) { if matches!(test.node, ExprKind::Compare { .. }) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&test.clone(), checker.stylist), expr.range(), - )); + ))); } else if checker.ctx.is_builtin("bool") { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::Call { func: Box::new(create_expr(ExprKind::Name { @@ -118,7 +118,7 @@ pub fn explicit_true_false_in_ifexpr( checker.stylist, ), expr.range(), - )); + ))); }; } checker.diagnostics.push(diagnostic); @@ -152,7 +152,7 @@ pub fn explicit_false_true_in_ifexpr( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::UnaryOp { op: Unaryop::Not, @@ -161,7 +161,7 @@ pub fn explicit_false_true_in_ifexpr( checker.stylist, ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -200,7 +200,7 @@ pub fn twisted_arms_in_ifexpr( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::IfExp { test: Box::new(create_expr(orelse.node.clone())), @@ -210,7 +210,7 @@ pub fn twisted_arms_in_ifexpr( checker.stylist, ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_unary_op.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_unary_op.rs index 1b978989cfb18..bb932d0ec4b9e 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_unary_op.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_unary_op.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Cmpop, Expr, ExprKind, Stmt, StmtKind, Unaryop}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, unparse_expr}; use ruff_python_semantic::scope::ScopeKind; @@ -107,7 +107,7 @@ pub fn negation_with_equal_op(checker: &mut Checker, expr: &Expr, op: &Unaryop, expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::Compare { left: left.clone(), @@ -117,7 +117,7 @@ pub fn negation_with_equal_op(checker: &mut Checker, expr: &Expr, op: &Unaryop, checker.stylist, ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -157,7 +157,7 @@ pub fn negation_with_not_equal_op( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr( &create_expr(ExprKind::Compare { left: left.clone(), @@ -167,7 +167,7 @@ pub fn negation_with_not_equal_op( checker.stylist, ), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -191,10 +191,10 @@ pub fn double_negation(checker: &mut Checker, expr: &Expr, op: &Unaryop, operand expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(operand, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs b/crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs index eebd1131f06b4..97d49f22ae7ad 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs @@ -5,7 +5,7 @@ use ruff_text_size::TextRange; use rustpython_parser::ast::{Cmpop, Expr, ExprKind}; use ruff_diagnostics::Edit; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -91,7 +91,10 @@ fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: TextRang range, ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(value_content, right.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + value_content, + right.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/reimplemented_builtin.rs b/crates/ruff/src/rules/flake8_simplify/rules/reimplemented_builtin.rs index ab31839c26ab1..f2452e830298b 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/reimplemented_builtin.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/reimplemented_builtin.rs @@ -4,7 +4,7 @@ use rustpython_parser::ast::{ }; use unicode_width::UnicodeWidthStr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, create_stmt, unparse_stmt}; use ruff_python_ast::source_code::Stylist; @@ -227,11 +227,11 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling: stmt.range(), ); if checker.patch(diagnostic.kind.rule()) && checker.ctx.is_builtin("any") { - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( contents, stmt.start(), loop_info.terminal, - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -308,11 +308,11 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling: stmt.range(), ); if checker.patch(diagnostic.kind.rule()) && checker.ctx.is_builtin("all") { - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( contents, stmt.start(), loop_info.terminal, - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs b/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs index faea06b192386..b9470889ec4bd 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -104,7 +104,10 @@ pub fn suppressible_exception( ); let handler_line_begin = checker.locator.line_start(handler.start()); let remove_handler = Edit::deletion(handler_line_begin, handler.end()); - Ok(Fix::from_iter([import_edit, replace_try, remove_handler])) + Ok(Fix::unspecified_edits( + import_edit, + [replace_try, remove_handler], + )) }); } diff --git a/crates/ruff/src/rules/flake8_simplify/rules/yoda_conditions.rs b/crates/ruff/src/rules/flake8_simplify/rules/yoda_conditions.rs index f475588905011..501bbd754b1b3 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/yoda_conditions.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/yoda_conditions.rs @@ -2,7 +2,7 @@ use anyhow::Result; use libcst_native::{Codegen, CodegenState, CompOp}; use rustpython_parser::ast::{Cmpop, Expr, ExprKind, Unaryop}; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::{Locator, Stylist}; use ruff_python_stdlib::str::{self}; @@ -161,7 +161,10 @@ pub fn yoda_conditions( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(suggestion, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + suggestion, + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } else { diff --git a/crates/ruff/src/rules/isort/rules/organize_imports.rs b/crates/ruff/src/rules/isort/rules/organize_imports.rs index 46bcba3d8e3c4..ff863723f0f20 100644 --- a/crates/ruff/src/rules/isort/rules/organize_imports.rs +++ b/crates/ruff/src/rules/isort/rules/organize_imports.rs @@ -5,7 +5,7 @@ use ruff_text_size::TextRange; use rustpython_parser::ast::Stmt; use textwrap::indent; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{ followed_by_multi_statement_line, preceded_by_multi_statement_line, trailing_lines_end, @@ -148,10 +148,10 @@ pub fn organize_imports( } else { let mut diagnostic = Diagnostic::new(UnsortedImports, range); if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( indent(&expected, indentation), range, - )); + ))); } Some(diagnostic) } diff --git a/crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs b/crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs index b4b59df98a06d..d60d326f14e58 100644 --- a/crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs +++ b/crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -70,7 +70,7 @@ pub fn deprecated_type_alias(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( match type_name { "unicode" => "str", "long" => "int", @@ -78,7 +78,7 @@ pub fn deprecated_type_alias(checker: &mut Checker, expr: &Expr) { } .to_string(), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pandas_vet/fixes.rs b/crates/ruff/src/rules/pandas_vet/fixes.rs index 360d9ef7a6c51..5b10c61c9afe7 100644 --- a/crates/ruff/src/rules/pandas_vet/fixes.rs +++ b/crates/ruff/src/rules/pandas_vet/fixes.rs @@ -41,5 +41,5 @@ pub(super) fn convert_inplace_argument_to_assignment( ) .ok()?; - Some(Fix::from_iter([insert_assignment, remove_argument])) + Some(Fix::unspecified_edits(insert_assignment, [remove_argument])) } diff --git a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs index 2446984993581..0c223ea4e5ebe 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -3,7 +3,7 @@ use rustpython_parser::lexer::LexResult; use rustpython_parser::Tok; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::registry::Rule; @@ -164,7 +164,7 @@ pub fn compound_statements( let mut diagnostic = Diagnostic::new(UselessSemicolon, TextRange::new(start, end)); if autofix.into() && settings.rules.should_fix(Rule::UselessSemicolon) { - diagnostic.set_fix(Edit::deletion(start, end)); + diagnostic.set_fix(Fix::unspecified(Edit::deletion(start, end))); }; diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs index 2a1bd7620feb0..0c3488b5fc038 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs @@ -2,7 +2,7 @@ use anyhow::{bail, Result}; use log::error; use ruff_text_size::{TextLen, TextRange, TextSize}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -108,10 +108,10 @@ pub fn invalid_escape_sequence( let range = TextRange::at(location, next_char.text_len() + TextSize::from(1)); let mut diagnostic = Diagnostic::new(InvalidEscapeSequence(*next_char), range); if autofix { - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( r"\".to_string(), range.start() + TextSize::from(1), - )); + ))); } diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs index 0ecd28b187278..147b30cc616ee 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs @@ -1,4 +1,4 @@ -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{has_leading_content, has_trailing_content, unparse_stmt}; use ruff_python_ast::newlines::StrExt; @@ -103,7 +103,10 @@ pub fn lambda_assignment( indented.push_str(&line); } } - diagnostic.set_fix(Edit::range_replacement(indented, stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + indented, + stmt.range(), + ))); } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pycodestyle/rules/literal_comparisons.rs b/crates/ruff/src/rules/pycodestyle/rules/literal_comparisons.rs index 7b8713d0e3e13..51a0d46a846c2 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/literal_comparisons.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/literal_comparisons.rs @@ -2,7 +2,7 @@ use itertools::izip; use rustc_hash::FxHashMap; use rustpython_parser::ast::{Cmpop, Constant, Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers; @@ -270,7 +270,10 @@ pub fn literal_comparisons( .collect::>(); let content = compare(left, &ops, comparators, checker.stylist); for diagnostic in &mut diagnostics { - diagnostic.set_fix(Edit::range_replacement(content.to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + content.to_string(), + expr.range(), + ))); } } diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs index 56125881046c8..6eb00d4c82407 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs @@ -1,7 +1,7 @@ use super::LogicalLine; use crate::checkers::logical_lines::LogicalLinesContext; use ruff_diagnostics::Edit; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::token_kind::TokenKind; use ruff_text_size::{TextRange, TextSize}; @@ -86,7 +86,10 @@ pub(crate) fn missing_whitespace( let mut diagnostic = Diagnostic::new(kind, TextRange::empty(token.start())); if autofix { - diagnostic.set_fix(Edit::insertion(" ".to_string(), token.end())); + diagnostic.set_fix(Fix::unspecified(Edit::insertion( + " ".to_string(), + token.end(), + ))); } context.push_diagnostic(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_before_parameters.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_before_parameters.rs index 1007a3515194e..22422ff4618c5 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_before_parameters.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_before_parameters.rs @@ -1,6 +1,6 @@ use crate::checkers::logical_lines::LogicalLinesContext; use crate::rules::pycodestyle::rules::logical_lines::LogicalLine; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::token_kind::TokenKind; use ruff_text_size::{TextRange, TextSize}; @@ -63,7 +63,7 @@ pub(crate) fn whitespace_before_parameters( let mut diagnostic = Diagnostic::new(kind, TextRange::new(start, end)); if autofix { - diagnostic.set_fix(Edit::deletion(start, end)); + diagnostic.set_fix(Fix::unspecified(Edit::deletion(start, end))); } context.push_diagnostic(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/missing_newline_at_end_of_file.rs b/crates/ruff/src/rules/pycodestyle/rules/missing_newline_at_end_of_file.rs index 94aed8b41d694..42ab5b76288d8 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/missing_newline_at_end_of_file.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/missing_newline_at_end_of_file.rs @@ -1,6 +1,6 @@ use ruff_text_size::{TextLen, TextRange}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -55,10 +55,10 @@ pub fn no_newline_at_end_of_file( let mut diagnostic = Diagnostic::new(MissingNewlineAtEndOfFile, range); if autofix { - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( stylist.line_ending().to_string(), range.start(), - )); + ))); } return Some(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/not_tests.rs b/crates/ruff/src/rules/pycodestyle/rules/not_tests.rs index 8dd82ce08d6df..ec7b3ac23d037 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/not_tests.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/not_tests.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Cmpop, Expr, ExprKind, Unaryop}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -99,10 +99,10 @@ pub fn not_tests( if check_not_in { let mut diagnostic = Diagnostic::new(NotInTest, operand.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( compare(left, &[Cmpop::NotIn], comparators, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -111,10 +111,10 @@ pub fn not_tests( if check_not_is { let mut diagnostic = Diagnostic::new(NotIsTest, operand.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( compare(left, &[Cmpop::IsNot], comparators, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs index da6074c65d839..5fb88c9e59f14 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -1,6 +1,6 @@ use ruff_text_size::{TextLen, TextRange, TextSize}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::Line; @@ -95,7 +95,7 @@ pub(crate) fn trailing_whitespace( if matches!(autofix, flags::Autofix::Enabled) && settings.rules.should_fix(Rule::BlankLineWithWhitespace) { - diagnostic.set_fix(Edit::range_deletion(range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range))); } return Some(diagnostic); } @@ -104,7 +104,7 @@ pub(crate) fn trailing_whitespace( if matches!(autofix, flags::Autofix::Enabled) && settings.rules.should_fix(Rule::TrailingWhitespace) { - diagnostic.set_fix(Edit::range_deletion(range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range))); } return Some(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/blank_after_summary.rs b/crates/ruff/src/rules/pydocstyle/rules/blank_after_summary.rs index bbac7b3a72fb1..effdb59e756db 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/blank_after_summary.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/blank_after_summary.rs @@ -1,4 +1,4 @@ -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::{StrExt, UniversalNewlineIterator}; @@ -82,11 +82,11 @@ pub fn blank_after_summary(checker: &mut Checker, docstring: &Docstring) { } // Insert one blank line after the summary (replacing any existing lines). - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( checker.stylist.line_ending().to_string(), summary_end, blank_end, - )); + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_class.rs b/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_class.rs index be9ed4141e8f8..9cfa506020882 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_class.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_class.rs @@ -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_python_ast::newlines::{StrExt, UniversalNewlineIterator}; use ruff_text_size::{TextLen, TextRange}; @@ -94,10 +94,10 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { ); if checker.patch(diagnostic.kind.rule()) { // Delete the blank line before the class. - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( blank_lines_start, docstring.start() - docstring.indentation.text_len(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -116,11 +116,11 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { ); if checker.patch(diagnostic.kind.rule()) { // Insert one blank line before the class. - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( checker.stylist.line_ending().to_string(), blank_lines_start, docstring.start() - docstring.indentation.text_len(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -163,11 +163,11 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { ); if checker.patch(diagnostic.kind.rule()) { // Insert a blank line before the class (replacing any existing lines). - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( checker.stylist.line_ending().to_string(), first_line_start, blank_lines_end, - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_function.rs b/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_function.rs index 25cd6ca58b551..080fcc2fe8f93 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_function.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/blank_before_after_function.rs @@ -2,7 +2,7 @@ use once_cell::sync::Lazy; use regex::Regex; use ruff_text_size::{TextLen, TextRange}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::{StrExt, UniversalNewlineIterator}; @@ -88,10 +88,10 @@ pub fn blank_before_after_function(checker: &mut Checker, docstring: &Docstring) ); if checker.patch(diagnostic.kind.rule()) { // Delete the blank line before the docstring. - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( blank_lines_start, docstring.start() - docstring.indentation.text_len(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -149,7 +149,10 @@ pub fn blank_before_after_function(checker: &mut Checker, docstring: &Docstring) ); if checker.patch(diagnostic.kind.rule()) { // Delete the blank line after the docstring. - diagnostic.set_fix(Edit::deletion(first_line_end, blank_lines_end)); + diagnostic.set_fix(Fix::unspecified(Edit::deletion( + first_line_end, + blank_lines_end, + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/capitalized.rs b/crates/ruff/src/rules/pydocstyle/rules/capitalized.rs index c6ecac7ed12de..0132e7637f3a2 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/capitalized.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/capitalized.rs @@ -1,6 +1,6 @@ use ruff_text_size::{TextLen, TextRange}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -71,10 +71,10 @@ pub fn capitalized(checker: &mut Checker, docstring: &Docstring) { ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( capitalized_word, TextRange::at(body.start(), first_word.text_len()), - )); + ))); } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pydocstyle/rules/ends_with_period.rs b/crates/ruff/src/rules/pydocstyle/rules/ends_with_period.rs index ffeb4af655b42..bdbf461c3dfd2 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/ends_with_period.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/ends_with_period.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextLen; use strum::IntoEnumIterator; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::{StrExt, UniversalNewlineIterator}; @@ -64,10 +64,10 @@ pub fn ends_with_period(checker: &mut Checker, docstring: &Docstring) { && !trimmed.ends_with(':') && !trimmed.ends_with(';') { - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( ".".to_string(), line.start() + trimmed.text_len(), - )); + ))); } checker.diagnostics.push(diagnostic); }; diff --git a/crates/ruff/src/rules/pydocstyle/rules/ends_with_punctuation.rs b/crates/ruff/src/rules/pydocstyle/rules/ends_with_punctuation.rs index b2ed83821fd10..d123cbe6fd9ab 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/ends_with_punctuation.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/ends_with_punctuation.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextLen; use strum::IntoEnumIterator; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::{StrExt, UniversalNewlineIterator}; @@ -61,10 +61,10 @@ pub fn ends_with_punctuation(checker: &mut Checker, docstring: &Docstring) { let mut diagnostic = Diagnostic::new(EndsInPunctuation, docstring.range()); // Best-effort autofix: avoid adding a period after other punctuation marks. if checker.patch(diagnostic.kind.rule()) && !trimmed.ends_with([':', ';']) { - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( ".".to_string(), line.start() + trimmed.text_len(), - )); + ))); } checker.diagnostics.push(diagnostic); }; diff --git a/crates/ruff/src/rules/pydocstyle/rules/indent.rs b/crates/ruff/src/rules/pydocstyle/rules/indent.rs index 92fc84ce10beb..24ee0fc06ed02 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/indent.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/indent.rs @@ -1,5 +1,5 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::NewlineWithTrailingNewline; use ruff_python_ast::whitespace; @@ -90,10 +90,10 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { let mut diagnostic = Diagnostic::new(UnderIndentation, TextRange::empty(line.start())); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( whitespace::clean(docstring.indentation), TextRange::at(line.start(), line_indent.text_len()), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -151,10 +151,10 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { let mut diagnostic = Diagnostic::new(OverIndentation, TextRange::empty(last.start())); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( whitespace::clean(docstring.indentation), TextRange::at(last.start(), line_indent.text_len()), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/multi_line_summary_start.rs b/crates/ruff/src/rules/pydocstyle/rules/multi_line_summary_start.rs index 18e51fe435f6c..6275913a1f185 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/multi_line_summary_start.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/multi_line_summary_start.rs @@ -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_python_ast::newlines::{NewlineWithTrailingNewline, UniversalNewlineIterator}; use ruff_python_ast::str::{is_triple_quote, leading_quote}; @@ -67,10 +67,10 @@ pub fn multi_line_summary_start(checker: &mut Checker, docstring: &Docstring) { // Delete until first non-whitespace char. for line in content_lines { if let Some(end_column) = line.find(|c: char| !c.is_whitespace()) { - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( first_line.end(), line.start() + TextSize::try_from(end_column).unwrap(), - )); + ))); break; } } @@ -123,7 +123,11 @@ pub fn multi_line_summary_start(checker: &mut Checker, docstring: &Docstring) { first_line.strip_prefix(prefix).unwrap().trim_start() ); - diagnostic.set_fix(Edit::replacement(repl, body.start(), first_line.end())); + diagnostic.set_fix(Fix::unspecified(Edit::replacement( + repl, + body.start(), + first_line.end(), + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs b/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs index 3728a42579be4..912c03b7e7f1d 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs @@ -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_python_ast::newlines::{NewlineWithTrailingNewline, StrExt}; use ruff_python_ast::whitespace; @@ -56,11 +56,11 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, docstring: &Docstring checker.stylist.line_ending().as_str(), whitespace::clean(docstring.indentation) ); - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( content, docstring.expr.end() - num_trailing_quotes - num_trailing_spaces, docstring.expr.end() - num_trailing_quotes, - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs b/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs index 950390757671b..f07e94799f51b 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs @@ -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_python_ast::newlines::NewlineWithTrailingNewline; use ruff_text_size::{TextLen, TextRange}; @@ -44,10 +44,10 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) { // characters, avoid applying the fix. if !trimmed.ends_with(quote) && !trimmed.starts_with(quote) && !ends_with_backslash(trimmed) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( trimmed.to_string(), TextRange::at(body.start(), line.text_len()), - )); + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs b/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs index dfbbc32eeae50..bafd15898cca3 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs @@ -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_python_ast::newlines::NewlineWithTrailingNewline; use ruff_python_ast::str::{leading_quote, trailing_quote}; @@ -49,10 +49,10 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) { if !trimmed.ends_with(trailing.chars().last().unwrap()) && !trimmed.starts_with(leading.chars().last().unwrap()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!("{leading}{trimmed}{trailing}"), docstring.range(), - )); + ))); } } } diff --git a/crates/ruff/src/rules/pydocstyle/rules/sections.rs b/crates/ruff/src/rules/pydocstyle/rules/sections.rs index 8ba8b443f25fe..b80539c301f31 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/sections.rs @@ -6,7 +6,7 @@ use rustc_hash::FxHashSet; use rustpython_parser::ast::StmtKind; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; -use ruff_diagnostics::{Diagnostic, Edit}; +use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::identifier_range; use ruff_python_ast::newlines::NewlineWithTrailingNewline; @@ -373,7 +373,7 @@ fn blanks_and_section_underline( let range = TextRange::new(context.following_range().start(), blank_lines_end); // Delete any blank lines between the header and the underline. - diagnostic.set_fix(Edit::range_deletion(range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range))); } checker.diagnostics.push(diagnostic); } @@ -405,11 +405,11 @@ fn blanks_and_section_underline( "-".repeat(context.section_name().len()), checker.stylist.line_ending().as_str() ); - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( content, blank_lines_end, non_blank_line.full_end(), - )); + ))); }; checker.diagnostics.push(diagnostic); } @@ -435,10 +435,10 @@ fn blanks_and_section_underline( ); // Replace the existing indentation with whitespace of the appropriate length. - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( whitespace::clean(docstring.indentation), range, - )); + ))); }; checker.diagnostics.push(diagnostic); } @@ -478,10 +478,10 @@ fn blanks_and_section_underline( ); if checker.patch(diagnostic.kind.rule()) { // Delete any blank lines between the header and content. - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( line_after_dashes.start(), blank_lines_after_dashes_end, - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -516,7 +516,10 @@ fn blanks_and_section_underline( whitespace::clean(docstring.indentation), "-".repeat(context.section_name().len()), ); - diagnostic.set_fix(Edit::insertion(content, context.summary_range().end())); + diagnostic.set_fix(Fix::unspecified(Edit::insertion( + content, + context.summary_range().end(), + ))); } checker.diagnostics.push(diagnostic); } @@ -536,7 +539,7 @@ fn blanks_and_section_underline( let range = TextRange::new(context.following_range().start(), blank_lines_end); // Delete any blank lines between the header and content. - diagnostic.set_fix(Edit::range_deletion(range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range))); } checker.diagnostics.push(diagnostic); } @@ -565,7 +568,10 @@ fn blanks_and_section_underline( "-".repeat(context.section_name().len()), ); - diagnostic.set_fix(Edit::insertion(content, context.summary_range().end())); + diagnostic.set_fix(Fix::unspecified(Edit::insertion( + content, + context.summary_range().end(), + ))); } checker.diagnostics.push(diagnostic); } @@ -599,10 +605,10 @@ fn common_section( // Replace the section title with the capitalized variant. This requires // locating the start and end of the section name. let section_range = context.section_name_range(); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( capitalized_section_name.to_string(), section_range, - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -622,11 +628,11 @@ fn common_section( let content = whitespace::clean(docstring.indentation); let fix_range = TextRange::at(context.range().start(), leading_space.text_len()); - diagnostic.set_fix(if content.is_empty() { + diagnostic.set_fix(Fix::unspecified(if content.is_empty() { Edit::range_deletion(fix_range) } else { Edit::range_replacement(content, fix_range) - }); + })); }; checker.diagnostics.push(diagnostic); } @@ -649,7 +655,10 @@ fn common_section( ); if checker.patch(diagnostic.kind.rule()) { // Add a newline at the beginning of the next section. - diagnostic.set_fix(Edit::insertion(line_end.to_string(), next.range().start())); + diagnostic.set_fix(Fix::unspecified(Edit::insertion( + line_end.to_string(), + next.range().start(), + ))); } checker.diagnostics.push(diagnostic); } @@ -667,10 +676,10 @@ fn common_section( ); if checker.patch(diagnostic.kind.rule()) { // Add a newline after the section. - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( format!("{}{}", line_end, docstring.indentation), context.range().end(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -691,10 +700,10 @@ fn common_section( ); if checker.patch(diagnostic.kind.rule()) { // Add a blank line before the section. - diagnostic.set_fix(Edit::insertion( + diagnostic.set_fix(Fix::unspecified(Edit::insertion( line_end.to_string(), context.range().start(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -893,10 +902,10 @@ fn numpy_section( ); if checker.patch(diagnostic.kind.rule()) { let section_range = context.section_name_range(); - diagnostic.set_fix(Edit::range_deletion(TextRange::at( + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(TextRange::at( section_range.end(), suffix.text_len(), - ))); + )))); } checker.diagnostics.push(diagnostic); @@ -930,10 +939,10 @@ fn google_section( if checker.patch(diagnostic.kind.rule()) { // Replace the suffix. let section_name_range = context.section_name_range(); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( ":".to_string(), TextRange::at(section_name_range.end(), suffix.text_len()), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyflakes/rules/invalid_literal_comparisons.rs b/crates/ruff/src/rules/pyflakes/rules/invalid_literal_comparisons.rs index 2763551a5a7f7..eb1762bc7a6fa 100644 --- a/crates/ruff/src/rules/pyflakes/rules/invalid_literal_comparisons.rs +++ b/crates/ruff/src/rules/pyflakes/rules/invalid_literal_comparisons.rs @@ -4,7 +4,7 @@ use once_cell::unsync::Lazy; use ruff_text_size::TextRange; use rustpython_parser::ast::{Cmpop, Expr}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers; @@ -78,10 +78,10 @@ pub fn invalid_literal_comparison( None } } { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( content, located_op.range() + location.start(), - )); + ))); } } else { error!("Failed to fix invalid comparison due to missing op"); diff --git a/crates/ruff/src/rules/pyflakes/rules/raise_not_implemented.rs b/crates/ruff/src/rules/pyflakes/rules/raise_not_implemented.rs index 15cc3e9f3b6df..504305f651229 100644 --- a/crates/ruff/src/rules/pyflakes/rules/raise_not_implemented.rs +++ b/crates/ruff/src/rules/pyflakes/rules/raise_not_implemented.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -46,10 +46,10 @@ pub fn raise_not_implemented(checker: &mut Checker, expr: &Expr) { }; let mut diagnostic = Diagnostic::new(RaiseNotImplemented, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "NotImplementedError".to_string(), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyflakes/rules/repeated_keys.rs b/crates/ruff/src/rules/pyflakes/rules/repeated_keys.rs index a85c484cd4517..9923b9596c1da 100644 --- a/crates/ruff/src/rules/pyflakes/rules/repeated_keys.rs +++ b/crates/ruff/src/rules/pyflakes/rules/repeated_keys.rs @@ -3,7 +3,7 @@ use std::hash::{BuildHasherDefault, Hash}; use rustc_hash::{FxHashMap, FxHashSet}; use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::comparable::{ComparableConstant, ComparableExpr}; use ruff_python_ast::helpers::unparse_expr; @@ -109,10 +109,10 @@ pub fn repeated_keys(checker: &mut Checker, keys: &[Option], values: &[Exp ); if is_duplicate_value { if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( values[i - 1].end(), values[i].end(), - )); + ))); } } else { seen_values.insert(comparable_value); @@ -137,10 +137,10 @@ pub fn repeated_keys(checker: &mut Checker, keys: &[Option], values: &[Exp ); if is_duplicate_value { if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::deletion( + diagnostic.set_fix(Fix::unspecified(Edit::deletion( values[i - 1].end(), values[i].end(), - )); + ))); } } else { seen_values.insert(comparable_value); diff --git a/crates/ruff/src/rules/pylint/rules/invalid_string_characters.rs b/crates/ruff/src/rules/pylint/rules/invalid_string_characters.rs index d57a2f61521df..46d24bd6e2e47 100644 --- a/crates/ruff/src/rules/pylint/rules/invalid_string_characters.rs +++ b/crates/ruff/src/rules/pylint/rules/invalid_string_characters.rs @@ -2,7 +2,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize}; use ruff_diagnostics::AlwaysAutofixableViolation; use ruff_diagnostics::Edit; -use ruff_diagnostics::{Diagnostic, DiagnosticKind}; +use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -197,7 +197,10 @@ pub fn invalid_string_characters( let mut diagnostic = Diagnostic::new(rule, range); if autofix { - diagnostic.set_fix(Edit::range_replacement(replacement.to_string(), range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + replacement.to_string(), + range, + ))); } diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pylint/rules/manual_import_from.rs b/crates/ruff/src/rules/pylint/rules/manual_import_from.rs index 2658c00e55aa8..699b4f4208936 100644 --- a/crates/ruff/src/rules/pylint/rules/manual_import_from.rs +++ b/crates/ruff/src/rules/pylint/rules/manual_import_from.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Alias, AliasData, Located, 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_python_ast::helpers::{create_stmt, unparse_stmt}; @@ -53,7 +53,7 @@ pub fn manual_from_import(checker: &mut Checker, stmt: &Stmt, alias: &Alias, nam alias.range(), ); if fixable && checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_stmt( &create_stmt(StmtKind::ImportFrom { module: Some(module.to_string()), @@ -69,7 +69,7 @@ pub fn manual_from_import(checker: &mut Checker, stmt: &Stmt, alias: &Alias, nam checker.stylist, ), stmt.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pylint/rules/sys_exit_alias.rs b/crates/ruff/src/rules/pylint/rules/sys_exit_alias.rs index cc563eba2b456..ebe01737dafdb 100644 --- a/crates/ruff/src/rules/pylint/rules/sys_exit_alias.rs +++ b/crates/ruff/src/rules/pylint/rules/sys_exit_alias.rs @@ -54,7 +54,7 @@ pub fn sys_exit_alias(checker: &mut Checker, func: &Expr) { checker.locator, )?; let reference_edit = Edit::range_replacement(binding, func.range()); - Ok(Fix::from_iter([import_edit, reference_edit])) + Ok(Fix::unspecified_edits(import_edit, [reference_edit])) }); } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs b/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs index 3249e80a6b75a..9fcaa5437d718 100644 --- a/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs +++ b/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::Alias; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -49,7 +49,10 @@ pub fn useless_import_alias(checker: &mut Checker, alias: &Alias) { let mut diagnostic = Diagnostic::new(UselessImportAlias, alias.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(asname.to_string(), alias.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + asname.to_string(), + alias.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/datetime_utc_alias.rs b/crates/ruff/src/rules/pyupgrade/rules/datetime_utc_alias.rs index f31e3049470f2..35395841dc541 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/datetime_utc_alias.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/datetime_utc_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::collect_call_path; @@ -44,10 +44,10 @@ pub fn datetime_utc_alias(checker: &mut Checker, expr: &Expr) { let mut diagnostic = Diagnostic::new(DatetimeTimezoneUTC { straight_import }, expr.range()); if checker.patch(diagnostic.kind.rule()) { if straight_import { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "datetime.UTC".to_string(), expr.range(), - )); + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/deprecated_c_element_tree.rs b/crates/ruff/src/rules/pyupgrade/rules/deprecated_c_element_tree.rs index 7af7c123e4d7f..b79f6167b43d6 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/deprecated_c_element_tree.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/deprecated_c_element_tree.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Located, Stmt, StmtKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -24,10 +24,10 @@ fn add_check_for_node(checker: &mut Checker, node: &Located) { let mut diagnostic = Diagnostic::new(DeprecatedCElementTree, node.range()); if checker.patch(diagnostic.kind.rule()) { let contents = checker.locator.slice(node.range()); - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( contents.replacen("cElementTree", "ElementTree", 1), node.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs b/crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs index 781c5b8ddeb73..743d503667ff4 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use rustpython_parser::ast::{Alias, AliasData, Stmt}; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::{Locator, Stylist}; use ruff_python_ast::whitespace::indentation; @@ -551,7 +551,10 @@ pub fn deprecated_import( ); if checker.patch(Rule::DeprecatedImport) { if let Some(content) = fix { - diagnostic.set_fix(Edit::range_replacement(content, stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + content, + stmt.range(), + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/deprecated_mock_import.rs b/crates/ruff/src/rules/pyupgrade/rules/deprecated_mock_import.rs index e638aa459f599..7af445d7571e8 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/deprecated_mock_import.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/deprecated_mock_import.rs @@ -6,7 +6,7 @@ use libcst_native::{ use log::error; use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::collect_call_path; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -257,7 +257,10 @@ pub fn deprecated_mock_attribute(checker: &mut Checker, expr: &Expr) { value.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("mock".to_string(), value.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "mock".to_string(), + value.range(), + ))); } checker.diagnostics.push(diagnostic); } @@ -300,8 +303,10 @@ pub fn deprecated_mock_import(checker: &mut Checker, stmt: &Stmt) { name.range(), ); if let Some(content) = content.as_ref() { - diagnostic - .set_fix(Edit::range_replacement(content.clone(), stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + content.clone(), + stmt.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs b/crates/ruff/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs index 73db5418eae2d..16a561fe2654f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs @@ -2,7 +2,7 @@ use once_cell::sync::Lazy; use rustc_hash::FxHashMap; use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -69,10 +69,10 @@ pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!("self.{target}"), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs b/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs index d7dc4964ad8e6..97b64e002a921 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/extraneous_parentheses.rs @@ -2,7 +2,7 @@ use ruff_text_size::TextRange; use rustpython_parser::lexer::LexResult; use rustpython_parser::Tok; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -142,11 +142,11 @@ pub fn extraneous_parentheses( if autofix.into() && settings.rules.should_fix(Rule::ExtraneousParentheses) { let contents = locator.slice(TextRange::new(start_range.start(), end_range.end())); - diagnostic.set_fix(Edit::replacement( + diagnostic.set_fix(Fix::unspecified(Edit::replacement( contents[1..contents.len() - 1].to_string(), start_range.start(), end_range.end(), - )); + ))); } diagnostics.push(diagnostic); } else { diff --git a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs index 2038ef5bf7101..d2c1116264daf 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs @@ -5,7 +5,7 @@ use rustpython_common::format::{ }; use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::str::{is_implicit_concatenation, leading_quote, trailing_quote}; @@ -273,7 +273,10 @@ pub(crate) fn f_strings(checker: &mut Checker, summary: &FormatSummary, expr: &E let mut diagnostic = Diagnostic::new(FString, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(contents, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + expr.range(), + ))); }; checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/format_literals.rs b/crates/ruff/src/rules/pyupgrade/rules/format_literals.rs index d912872e77296..4b36d730f931a 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/format_literals.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/format_literals.rs @@ -4,7 +4,7 @@ use once_cell::sync::Lazy; use regex::Regex; use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::{Locator, Stylist}; @@ -145,7 +145,10 @@ pub(crate) fn format_literals(checker: &mut Checker, summary: &FormatSummary, ex if let Ok(contents) = generate_call(expr, &summary.indices, checker.locator, checker.stylist) { - diagnostic.set_fix(Edit::range_replacement(contents, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + expr.range(), + ))); }; } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/lru_cache_with_maxsize_none.rs b/crates/ruff/src/rules/pyupgrade/rules/lru_cache_with_maxsize_none.rs index c0f266e968b9e..95c6ca97ce098 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/lru_cache_with_maxsize_none.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/lru_cache_with_maxsize_none.rs @@ -67,7 +67,7 @@ pub fn lru_cache_with_maxsize_none(checker: &mut Checker, decorator_list: &[Expr checker.locator, )?; let reference_edit = Edit::range_replacement(binding, expr.range()); - Ok(Fix::from_iter([import_edit, reference_edit])) + Ok(Fix::unspecified_edits(import_edit, [reference_edit])) }); } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs b/crates/ruff/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs index fb15799b050a3..fdf048468b9b1 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextRange; use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_expr; @@ -48,10 +48,10 @@ pub fn lru_cache_without_parameters(checker: &mut Checker, decorator_list: &[Exp TextRange::new(func.end(), expr.end()), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(func, checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/native_literals.rs b/crates/ruff/src/rules/pyupgrade/rules/native_literals.rs index 734caa7d56c5c..5603ff8a8c84f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/native_literals.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/native_literals.rs @@ -2,7 +2,7 @@ use std::fmt; use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::str::is_implicit_concatenation; @@ -64,7 +64,7 @@ pub fn native_literals( LiteralType::Bytes }}, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( if id == "bytes" { let mut content = String::with_capacity(3); content.push('b'); @@ -78,7 +78,7 @@ pub fn native_literals( content }, expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); return; @@ -127,7 +127,10 @@ pub fn native_literals( expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(arg_code.to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + arg_code.to_string(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/open_alias.rs b/crates/ruff/src/rules/pyupgrade/rules/open_alias.rs index c6db74f64f913..20c0820f2b59f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/open_alias.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/open_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -38,7 +38,10 @@ pub fn open_alias(checker: &mut Checker, expr: &Expr, func: &Expr) { .map_or(true, |binding| binding.kind.is_builtin()); let mut diagnostic = Diagnostic::new(OpenAlias { fixable }, expr.range()); if fixable && checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("open".to_string(), func.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "open".to_string(), + func.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/os_error_alias.rs b/crates/ruff/src/rules/pyupgrade/rules/os_error_alias.rs index 794220b864540..92341af96383a 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/os_error_alias.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/os_error_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::compose_call_path; use ruff_python_ast::helpers::{create_expr, unparse_expr}; @@ -63,10 +63,10 @@ fn atom_diagnostic(checker: &mut Checker, target: &Expr) { target.range(), ); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "OSError".to_string(), target.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -103,12 +103,12 @@ fn tuple_diagnostic(checker: &mut Checker, target: &Expr, aliases: &[&Expr]) { } if remaining.len() == 1 { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( "OSError".to_string(), target.range(), - )); + ))); } else { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( format!( "({})", unparse_expr( @@ -120,7 +120,7 @@ fn tuple_diagnostic(checker: &mut Checker, target: &Expr, aliases: &[&Expr]) { ) ), target.range(), - )); + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/pyupgrade/rules/printf_string_formatting.rs b/crates/ruff/src/rules/pyupgrade/rules/printf_string_formatting.rs index d8438bf76480d..5ad27adf3d97b 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/printf_string_formatting.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/printf_string_formatting.rs @@ -7,7 +7,7 @@ use rustpython_common::cformat::{ use rustpython_parser::ast::{Constant, Expr, ExprKind}; 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_python_ast::source_code::Locator; use ruff_python_ast::str::{leading_quote, trailing_quote}; @@ -444,7 +444,10 @@ pub(crate) fn printf_string_formatting( let mut diagnostic = Diagnostic::new(PrintfStringFormatting, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(contents, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/quoted_annotation.rs b/crates/ruff/src/rules/pyupgrade/rules/quoted_annotation.rs index dad06f86c1fb9..5ab670a612565 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/quoted_annotation.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/quoted_annotation.rs @@ -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_text_size::TextRange; @@ -23,7 +23,10 @@ impl AlwaysAutofixableViolation for QuotedAnnotation { pub fn quoted_annotation(checker: &mut Checker, annotation: &str, range: TextRange) { let mut diagnostic = Diagnostic::new(QuotedAnnotation, range); if checker.patch(Rule::QuotedAnnotation) { - 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); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs b/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs index 4b89564af2c1c..f6b2ad65f1021 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/redundant_open_modes.rs @@ -5,7 +5,7 @@ use ruff_text_size::TextSize; use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword}; 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_python_ast::helpers::find_keyword; use ruff_python_ast::source_code::Locator; @@ -116,10 +116,10 @@ fn create_check( ); if patch { if let Some(content) = replacement_value { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( content.to_string(), mode_param.range(), - )); + ))); } else { diagnostic.try_set_fix(|| create_remove_param_fix(locator, expr, mode_param)); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/replace_stdout_stderr.rs b/crates/ruff/src/rules/pyupgrade/rules/replace_stdout_stderr.rs index d39394b88fae3..6f2a6e64e1663 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/replace_stdout_stderr.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/replace_stdout_stderr.rs @@ -38,10 +38,17 @@ fn generate_fix( } else { (stderr, stdout) }; - Ok(Fix::new(vec![ + Ok(Fix::unspecified_edits( Edit::range_replacement("capture_output=True".to_string(), first.range()), - remove_argument(locator, func.start(), second.range(), args, keywords, false)?, - ])) + [remove_argument( + locator, + func.start(), + second.range(), + args, + keywords, + false, + )?], + )) } /// UP022 diff --git a/crates/ruff/src/rules/pyupgrade/rules/replace_universal_newlines.rs b/crates/ruff/src/rules/pyupgrade/rules/replace_universal_newlines.rs index 7fb590ae015dd..8278b99e12330 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/replace_universal_newlines.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/replace_universal_newlines.rs @@ -1,7 +1,7 @@ use ruff_text_size::{TextLen, TextRange}; use rustpython_parser::ast::{Expr, Keyword}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::find_keyword; @@ -35,7 +35,10 @@ pub fn replace_universal_newlines(checker: &mut Checker, func: &Expr, kwargs: &[ let range = TextRange::at(kwarg.start(), "universal_newlines".text_len()); let mut diagnostic = Diagnostic::new(ReplaceUniversalNewlines, range); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("text".to_string(), range)); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "text".to_string(), + range, + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/type_of_primitive.rs b/crates/ruff/src/rules/pyupgrade/rules/type_of_primitive.rs index b0cf937777177..7b49f3969a705 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/type_of_primitive.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/type_of_primitive.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Expr, ExprKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -46,7 +46,10 @@ pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: }; let mut diagnostic = Diagnostic::new(TypeOfPrimitive { primitive }, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement(primitive.builtin(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + primitive.builtin(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/typing_text_str_alias.rs b/crates/ruff/src/rules/pyupgrade/rules/typing_text_str_alias.rs index b9b6b9a0d8377..b4f1fb3f33cb4 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/typing_text_str_alias.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/typing_text_str_alias.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -31,7 +31,10 @@ pub fn typing_text_str_alias(checker: &mut Checker, expr: &Expr) { { let mut diagnostic = Diagnostic::new(TypingTextStrAlias, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement("str".to_string(), expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + "str".to_string(), + expr.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/unicode_kind_prefix.rs b/crates/ruff/src/rules/pyupgrade/rules/unicode_kind_prefix.rs index d07cb4792c6b4..b8b8ce1cd78e7 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unicode_kind_prefix.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unicode_kind_prefix.rs @@ -1,7 +1,7 @@ use ruff_text_size::{TextRange, TextSize}; use rustpython_parser::ast::Expr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -27,10 +27,10 @@ pub fn unicode_kind_prefix(checker: &mut Checker, expr: &Expr, kind: Option<&str if const_kind.to_lowercase() == "u" { let mut diagnostic = Diagnostic::new(UnicodeKindPrefix, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_deletion(TextRange::at( + diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(TextRange::at( expr.start(), TextSize::from(1), - ))); + )))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index b73fc9b6269ab..cfde7270230f4 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -1,7 +1,7 @@ use once_cell::sync::Lazy; use regex::Regex; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::Line; @@ -30,7 +30,10 @@ pub(crate) fn unnecessary_coding_comment(line: &Line, autofix: bool) -> Option { let mut diagnostic = Diagnostic::new(NonPEP604Annotation { fixable }, expr.range()); if fixable && checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&optional(slice), checker.stylist), expr.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } @@ -127,17 +127,17 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s // Invalid type annotation. } ExprKind::Tuple { elts, .. } => { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&union(elts), checker.stylist), expr.range(), - )); + ))); } _ => { // Single argument. - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(slice, checker.stylist), expr.range(), - )); + ))); } } } diff --git a/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs b/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs index 3745d05ae21a6..687e510fff1a4 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs @@ -3,7 +3,7 @@ use std::fmt; use rustpython_parser::ast::{Expr, ExprKind, Operator}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::unparse_expr; @@ -93,10 +93,10 @@ pub fn use_pep604_isinstance(checker: &mut Checker, expr: &Expr, func: &Expr, ar let mut diagnostic = Diagnostic::new(NonPEP604Isinstance { kind }, expr.range()); if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( unparse_expr(&union(elts), checker.stylist), types.range(), - )); + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pyupgrade/rules/yield_in_for_loop.rs b/crates/ruff/src/rules/pyupgrade/rules/yield_in_for_loop.rs index 263d4441a40d1..d60a434c9167a 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/yield_in_for_loop.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/yield_in_for_loop.rs @@ -1,7 +1,7 @@ use rustc_hash::FxHashMap; use rustpython_parser::ast::{Expr, ExprContext, ExprKind, Stmt, StmtKind}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::types::RefEquality; use ruff_python_ast::visitor; @@ -176,7 +176,10 @@ pub fn yield_in_for_loop(checker: &mut Checker, stmt: &Stmt) { if checker.patch(diagnostic.kind.rule()) { let contents = checker.locator.slice(item.iter.range()); let contents = format!("yield from {contents}"); - diagnostic.set_fix(Edit::range_replacement(contents, item.stmt.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + item.stmt.range(), + ))); } checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs index 707fc21db3cf2..af32c935ab390 100644 --- a/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs +++ b/crates/ruff/src/rules/ruff/rules/ambiguous_unicode_character.rs @@ -2,7 +2,7 @@ use once_cell::sync::Lazy; use ruff_text_size::{TextLen, TextRange, TextSize}; use rustc_hash::FxHashMap; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, DiagnosticKind, Edit}; +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, DiagnosticKind, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::source_code::Locator; @@ -1725,10 +1725,10 @@ pub fn ambiguous_unicode_character( ); if settings.rules.enabled(diagnostic.kind.rule()) { if autofix.into() && settings.rules.should_fix(diagnostic.kind.rule()) { - diagnostic.set_fix(Edit::range_replacement( + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( (representant as char).to_string(), char_range, - )); + ))); } diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/ruff/rules/collection_literal_concatenation.rs b/crates/ruff/src/rules/ruff/rules/collection_literal_concatenation.rs index 82b2fe8ca6c1f..2c7d22cdff441 100644 --- a/crates/ruff/src/rules/ruff/rules/collection_literal_concatenation.rs +++ b/crates/ruff/src/rules/ruff/rules/collection_literal_concatenation.rs @@ -1,6 +1,6 @@ use rustpython_parser::ast::{Expr, ExprContext, ExprKind, Operator}; -use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Violation}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::{create_expr, has_comments, unparse_expr}; @@ -108,7 +108,10 @@ pub fn collection_literal_concatenation(checker: &mut Checker, expr: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { if fixable { - diagnostic.set_fix(Edit::range_replacement(contents, expr.range())); + diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + contents, + expr.range(), + ))); } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index f600f03c146f3..74cd611d11530 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -56,7 +56,7 @@ pub fn test_path(path: impl AsRef, settings: &Settings) -> Result { struct MessageHeader { kind: DiagnosticKind, range: TextRange, - fix: Fix, + fix: Option, file_id: usize, noqa_row: TextSize, } diff --git a/crates/ruff_diagnostics/src/diagnostic.rs b/crates/ruff_diagnostics/src/diagnostic.rs index 931c436bac1ef..0b261c3d43140 100644 --- a/crates/ruff_diagnostics/src/diagnostic.rs +++ b/crates/ruff_diagnostics/src/diagnostic.rs @@ -24,7 +24,7 @@ pub struct DiagnosticKind { pub struct Diagnostic { pub kind: DiagnosticKind, pub range: TextRange, - pub fix: Fix, + pub fix: Option, pub parent: Option, } @@ -33,7 +33,7 @@ impl Diagnostic { Self { kind: kind.into(), range, - fix: Fix::empty(), + fix: None, parent: None, } } @@ -41,7 +41,7 @@ impl Diagnostic { /// Set the [`Fix`] used to fix the diagnostic. #[inline] pub fn set_fix>(&mut self, fix: T) { - self.fix = fix.into(); + self.fix = Some(fix.into()); } /// Consumes `self` and returns a new `Diagnostic` with the given `fix`. @@ -57,7 +57,7 @@ impl Diagnostic { #[inline] pub fn try_set_fix>(&mut self, func: impl FnOnce() -> Result) { match func() { - Ok(fix) => self.fix = fix.into(), + Ok(fix) => self.fix = Some(fix.into()), Err(err) => error!("Failed to create fix: {}", err), } } diff --git a/crates/ruff_diagnostics/src/fix.rs b/crates/ruff_diagnostics/src/fix.rs index c06de1fbbed37..463529fb83b2a 100644 --- a/crates/ruff_diagnostics/src/fix.rs +++ b/crates/ruff_diagnostics/src/fix.rs @@ -5,26 +5,23 @@ use serde::{Deserialize, Serialize}; use crate::edit::Edit; /// A collection of [`Edit`] elements to be applied to a source file. -#[derive(Default, Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Fix { edits: Vec, } impl Fix { - /// Create a new [`Fix`] from a vector of [`Edit`] elements. - pub fn new(edits: Vec) -> Self { - Self { edits } - } - - /// Create an empty [`Fix`]. - pub const fn empty() -> Self { - Self { edits: Vec::new() } + /// Create a new [`Fix`] with an unspecified applicability from an [`Edit`] element. + pub fn unspecified(edit: Edit) -> Self { + Self { edits: vec![edit] } } - /// Return `true` if the [`Fix`] contains no [`Edit`] elements. - pub fn is_empty(&self) -> bool { - self.edits.is_empty() + /// Create a new [`Fix`] with unspecified applicability from multiple [`Edit`] elements. + pub fn unspecified_edits(edit: Edit, rest: impl IntoIterator) -> Self { + Self { + edits: std::iter::once(edit).chain(rest.into_iter()).collect(), + } } /// Return the [`TextSize`] of the first [`Edit`] in the [`Fix`]. @@ -42,14 +39,6 @@ impl Fix { } } -impl FromIterator for Fix { - fn from_iter>(iter: T) -> Self { - Self { - edits: Vec::from_iter(iter), - } - } -} - impl From for Fix { fn from(edit: Edit) -> Self { Self { edits: vec![edit] } diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 9677084cfaa79..a2b54e1a8627c 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -215,14 +215,10 @@ pub fn check(contents: &str, options: JsValue) -> Result { message: message.kind.body, location: start_location, end_location, - fix: if message.fix.is_empty() { - None - } else { - Some(ExpandedFix { - message: message.kind.suggestion, - edits: message.fix.into_edits(), - }) - }, + fix: message.fix.map(|fix| ExpandedFix { + message: message.kind.suggestion, + edits: fix.into_edits(), + }), } }) .collect();