Skip to content

Commit

Permalink
Add Applicability to Fix (#4303)
Browse files Browse the repository at this point in the history
Co-authored-by: Micha Reiser <micha@reiser.io>
  • Loading branch information
zanieb and MichaReiser authored May 10, 2023
1 parent d66ce76 commit cf7aa26
Show file tree
Hide file tree
Showing 116 changed files with 296 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod tests {
use crate::autofix::apply_fixes;
use crate::rules::pycodestyle::rules::MissingNewlineAtEndOfFile;

#[allow(deprecated)]
fn create_diagnostics(edit: impl IntoIterator<Item = Edit>) -> Vec<Diagnostic> {
edit.into_iter()
.map(|edit| Diagnostic {
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5336,7 +5336,9 @@ impl<'a> Checker<'a> {
if matches!(child.node, StmtKind::ImportFrom { .. }) {
diagnostic.set_parent(child.start());
}

if let Some(edit) = &fix {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(edit.clone()));
}
diagnostics.push(diagnostic);
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub fn check_noqa(
locator,
));
} else {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!("# noqa: {}", valid_codes.join(", ")),
*range,
Expand Down
10 changes: 8 additions & 2 deletions crates/ruff/src/message/diff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::message::Message;
use colored::{Color, ColoredString, Colorize, Styles};
use ruff_diagnostics::Fix;
use ruff_diagnostics::{Applicability, Fix};
use ruff_python_ast::source_code::{OneIndexed, SourceFile};
use ruff_text_size::{TextRange, TextSize};
use similar::{ChangeTag, TextDiff};
Expand Down Expand Up @@ -47,7 +47,13 @@ impl Display for Diff<'_> {

let diff = TextDiff::from_lines(self.source_code.source_text(), &output);

writeln!(f, "{}", "ℹ Suggested fix".blue())?;
let message = match self.fix.applicability() {
Applicability::Automatic => "Fix",
Applicability::Suggested => "Suggested fix",
Applicability::Manual => "Possible fix",
Applicability::Unspecified => "Suggested fix", // For backwards compatibility, unspecified fixes are 'suggested'
};
writeln!(f, "ℹ {}", message.blue())?;

let (largest_old, largest_new) = diff
.ops()
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def fibonacci(n):

let fib_source = SourceFileBuilder::new("fib.py", fib).finish();

#[allow(deprecated)]
let unused_variable = Diagnostic::new(
UnusedVariable {
name: "x".to_string(),
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/eradicate/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ pub fn commented_out_code(
// Verify that the comment is on its own line, and that it contains 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) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(
locator.full_lines_range(range),
)));
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_stmt(&assertion_error(msg), checker.stylist),
stmt.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn duplicate_handler_exceptions<'a>(
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
if unique_elts.len() == 1 {
unparse_expr(unique_elts[0], checker.stylist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&attribute(obj, value), checker.stylist),
expr.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E
type_.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(elt, checker.stylist),
type_.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
assignment(obj, name, value, checker.stylist),
expr.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub fn unused_loop_control_variable(checker: &mut Checker, target: &Expr, body:
if let Some(binding) = binding {
if binding.kind.is_loop_var() {
if !binding.used() {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
rename,
expr.range(),
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/flake8_commas/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,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) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(diagnostic.range())));
}
diagnostics.push(diagnostic);
Expand Down Expand Up @@ -365,6 +366,7 @@ 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);
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!("{contents},"),
missing_comma.1,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_errmsg/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fn generate_fix(stylist: &Stylist, stmt: &Stmt, exc_arg: &Expr, indentation: &st
}),
stylist,
);
#[allow(deprecated)]
Fix::unspecified_edits(
Edit::insertion(
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn shebang_whitespace(
TextRange::at(range.start(), *n_spaces),
);
if autofix {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(TextRange::at(
range.start(),
*n_spaces,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_logging_format/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"warning".to_string(),
level_call_range,
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff/src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ 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) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(
pass_stmt.range().add_end(index),
)));
Expand Down Expand Up @@ -596,7 +597,7 @@ pub fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) {
})
.collect(),
});

#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&bool_op, checker.stylist),
expr.range(),
Expand All @@ -622,6 +623,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"list".to_string(),
expr.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn traverse_union<'a>(
};

// Replace the parent with its non-duplicate child.
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(
if expr.node == left.node { right } else { left },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl AlwaysAutofixableViolation for QuotedAnnotationInStub {
pub fn quoted_annotation_in_stub(checker: &mut Checker, annotation: &str, range: TextRange) {
let mut diagnostic = Diagnostic::new(QuotedAnnotationInStub, range);
if checker.patch(Rule::QuotedAnnotationInStub) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
annotation.to_string(),
range,
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
Diagnostic::new(TypedArgumentDefaultInStub, default.range());

if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
default.range(),
Expand All @@ -324,6 +325,7 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
Diagnostic::new(TypedArgumentDefaultInStub, default.range());

if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
default.range(),
Expand Down Expand Up @@ -353,6 +355,7 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
Diagnostic::new(ArgumentDefaultInStub, default.range());

if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
default.range(),
Expand All @@ -379,6 +382,7 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
Diagnostic::new(ArgumentDefaultInStub, default.range());

if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
default.range(),
Expand Down Expand Up @@ -410,6 +414,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
value.range(),
Expand Down Expand Up @@ -440,6 +445,7 @@ pub fn annotated_assignment_default_in_stub(

let mut diagnostic = Diagnostic::new(AssignmentDefaultInStub, value.range());
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"...".to_string(),
value.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub fn unittest_assertion(
);
if fixable && checker.patch(diagnostic.kind.rule()) {
if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_stmt(&stmt, checker.stylist),
expr.range(),
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
&& args.is_empty()
&& keywords.is_empty()
{
#[allow(deprecated)]
let fix = Fix::unspecified(Edit::deletion(func.end(), decorator.end()));
pytest_fixture_parentheses(
checker,
Expand Down Expand Up @@ -354,6 +355,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E
.enabled(Rule::PytestFixtureIncorrectParenthesesStyle)
&& checker.settings.flake8_pytest_style.fixture_parentheses
{
#[allow(deprecated)]
let fix = Fix::unspecified(Edit::insertion(
Parentheses::Empty.to_string(),
decorator.end(),
Expand Down Expand Up @@ -423,6 +425,7 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: &
stmt.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
"return".to_string(),
TextRange::at(stmt.start(), "yield".text_len()),
Expand Down Expand Up @@ -495,6 +498,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());
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(range)));
}
checker.diagnostics.push(diagnostic);
Expand All @@ -511,6 +515,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());
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(line_range)));
}
checker.diagnostics.push(diagnostic);
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ fn check_mark_parentheses(checker: &mut Checker, decorator: &Expr, call_path: &C
&& args.is_empty()
&& keywords.is_empty()
{
#[allow(deprecated)]
let fix = Fix::unspecified(Edit::deletion(func.end(), decorator.end()));
pytest_mark_parentheses(checker, decorator, call_path, fix, "", "()");
}
}
_ => {
if checker.settings.flake8_pytest_style.mark_parentheses {
#[allow(deprecated)]
let fix = Fix::unspecified(Edit::insertion("()".to_string(), decorator.end()));
pytest_mark_parentheses(checker, decorator, call_path, fix, "()", "");
}
Expand All @@ -113,6 +115,7 @@ 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()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_deletion(
decorator.range().sub_start(TextSize::from(1)),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
name_range,
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!(
"({})",
Expand Down Expand Up @@ -181,6 +182,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
name_range,
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(
&create_expr(ExprKind::List {
Expand Down Expand Up @@ -222,6 +224,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(
&create_expr(ExprKind::List {
Expand All @@ -244,6 +247,7 @@ 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) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
content,
expr.range(),
Expand Down Expand Up @@ -271,6 +275,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!(
"({})",
Expand All @@ -296,6 +301,7 @@ 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) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
content,
expr.range(),
Expand Down Expand Up @@ -372,6 +378,7 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) {
);

if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&create_expr(value.node.clone()), checker.stylist),
expr.range(),
Expand Down
Loading

0 comments on commit cf7aa26

Please sign in to comment.