Skip to content

Commit

Permalink
Replace uses of From<Edit> for Fix in set_fix with `Fix::unspecif…
Browse files Browse the repository at this point in the history
…ied(...)`
  • Loading branch information
zanieb committed May 3, 2023
1 parent 6856b6a commit e3bac78
Show file tree
Hide file tree
Showing 86 changed files with 464 additions and 326 deletions.
3 changes: 2 additions & 1 deletion crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,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;
Expand All @@ -114,7 +115,7 @@ mod tests {
// The choice of rule here is arbitrary.
kind: MissingNewlineAtEndOfFile.into(),
range: edit.range(),
fix: Some(edit.into()),
fix: Some(Fix::unspecified(edit)),
parent: None,
})
.collect()
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/eradicate/rules.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -58,10 +58,10 @@ 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(TextRange::at(
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(TextRange::at(
range.start(),
line.text_len(),
)));
))));
}
Some(diagnostic)
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
)));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/flake8_commas/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_logging_format/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 10 additions & 5 deletions crates/ruff/src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit e3bac78

Please sign in to comment.