From e0a507e48ebad7bc400f76290535673efece9229 Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Fri, 23 Jun 2023 17:54:43 -0500 Subject: [PATCH] Add Applicability to flake8_simplify (#5348) --- .../rules/flake8_simplify/rules/ast_bool_op.rs | 18 ++++++------------ .../rules/flake8_simplify/rules/ast_expr.rs | 6 ++---- .../src/rules/flake8_simplify/rules/ast_if.rs | 15 +++++---------- .../rules/flake8_simplify/rules/ast_ifexp.rs | 12 ++++-------- .../flake8_simplify/rules/ast_unary_op.rs | 12 ++++-------- .../rules/flake8_simplify/rules/ast_with.rs | 3 +-- .../rules/suppressible_exception.rs | 3 +-- ...ake8_simplify__tests__SIM201_SIM201.py.snap | 6 +++--- ...ake8_simplify__tests__SIM910_SIM910.py.snap | 10 +++++----- 9 files changed, 31 insertions(+), 54 deletions(-) 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 ebf3fbc28537e..4bf4e77d08fda 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 @@ -418,8 +418,7 @@ pub(crate) 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. - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&bool_op), expr.range(), ))); @@ -530,8 +529,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { }; node.into() }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&in_expr), expr.range(), ))); @@ -583,8 +581,7 @@ pub(crate) fn expr_and_not_expr(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( "False".to_string(), expr.range(), ))); @@ -638,8 +635,7 @@ pub(crate) fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) { expr.range(), ); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( "True".to_string(), expr.range(), ))); @@ -762,8 +758,7 @@ pub(crate) fn expr_or_true(checker: &mut Checker, expr: &Expr) { edit.range(), ); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(edit)); + diagnostic.set_fix(Fix::suggested(edit)); } checker.diagnostics.push(diagnostic); } @@ -780,8 +775,7 @@ pub(crate) fn expr_and_false(checker: &mut Checker, expr: &Expr) { edit.range(), ); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(edit)); + diagnostic.set_fix(Fix::suggested(edit)); } 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 45424d41a9213..d1a570a0f2dff 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_expr.rs @@ -174,8 +174,7 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) { range: TextRange::default(), }; let new_env_var = node.into(); - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&new_env_var), slice.range(), ))); @@ -229,8 +228,7 @@ pub(crate) fn dict_get_with_none_default(checker: &mut Checker, expr: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::automatic(Edit::range_replacement( expected, expr.range(), ))); 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 8a3105919d63f..9466328880464 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs @@ -385,8 +385,7 @@ pub(crate) fn nested_if_statements( <= checker.settings.line_length }) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(edit)); + diagnostic.set_fix(Fix::suggested(edit)); } } Err(err) => error!("Failed to fix nested if: {err}"), @@ -457,8 +456,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) { value: Some(test.clone()), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().stmt(&node.into()), stmt.range(), ))); @@ -480,8 +478,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) { value: Some(Box::new(node1.into())), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().stmt(&node2.into()), stmt.range(), ))); @@ -621,8 +618,7 @@ pub(crate) fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: O ); if checker.patch(diagnostic.kind.rule()) { if !has_comments(stmt, checker.locator) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( contents, stmt.range(), ))); @@ -978,8 +974,7 @@ pub(crate) fn use_dict_get_with_default( ); if checker.patch(diagnostic.kind.rule()) { if !has_comments(stmt, checker.locator) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( contents, stmt.range(), ))); 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 ffbc5d20326fb..eee0b0d1d1a58 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_ifexp.rs @@ -162,8 +162,7 @@ pub(crate) fn explicit_true_false_in_ifexpr( ); if checker.patch(diagnostic.kind.rule()) { if matches!(test, Expr::Compare(_)) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&test.clone()), expr.range(), ))); @@ -179,8 +178,7 @@ pub(crate) fn explicit_true_false_in_ifexpr( keywords: vec![], range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&node1.into()), expr.range(), ))); @@ -223,8 +221,7 @@ pub(crate) fn explicit_false_true_in_ifexpr( operand: Box::new(node), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&node1.into()), expr.range(), ))); @@ -275,8 +272,7 @@ pub(crate) fn twisted_arms_in_ifexpr( orelse: Box::new(node), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&node3.into()), expr.range(), ))); 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 a24505d9e7d1c..f0385b7560f17 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 @@ -178,8 +178,7 @@ pub(crate) fn negation_with_equal_op( comparators: comparators.clone(), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::automatic(Edit::range_replacement( checker.generator().expr(&node.into()), expr.range(), ))); @@ -231,8 +230,7 @@ pub(crate) fn negation_with_not_equal_op( comparators: comparators.clone(), range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&node.into()), expr.range(), ))); @@ -260,8 +258,7 @@ pub(crate) fn double_negation(checker: &mut Checker, expr: &Expr, op: UnaryOp, o ); if checker.patch(diagnostic.kind.rule()) { if checker.semantic().in_boolean_test() { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(operand), expr.range(), ))); @@ -277,8 +274,7 @@ pub(crate) fn double_negation(checker: &mut Checker, expr: &Expr, op: UnaryOp, o keywords: vec![], range: TextRange::default(), }; - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&node1.into()), expr.range(), ))); diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_with.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_with.rs index d3c1642a70cd1..8029e2a97bcc7 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_with.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_with.rs @@ -148,8 +148,7 @@ pub(crate) fn multiple_with_statements( <= checker.settings.line_length }) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(edit)); + diagnostic.set_fix(Fix::suggested(edit)); } } Err(err) => error!("Failed to fix nested with: {err}"), 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 9d5d984f88ba3..a025d176011f5 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -130,8 +130,7 @@ pub(crate) fn suppressible_exception( ); let handler_line_begin = checker.locator.line_start(handler.start()); let remove_handler = Edit::deletion(handler_line_begin, handler.end()); - #[allow(deprecated)] - Ok(Fix::unspecified_edits( + Ok(Fix::suggested_edits( import_edit, [replace_try, remove_handler], )) diff --git a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM201_SIM201.py.snap b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM201_SIM201.py.snap index f7a55e6697e23..cff98673a54cf 100644 --- a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM201_SIM201.py.snap +++ b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM201_SIM201.py.snap @@ -10,7 +10,7 @@ SIM201.py:2:4: SIM201 [*] Use `a != b` instead of `not a == b` | = help: Replace with `!=` operator -ℹ Suggested fix +ℹ Fix 1 1 | # SIM201 2 |-if not a == b: 2 |+if a != b: @@ -27,7 +27,7 @@ SIM201.py:6:4: SIM201 [*] Use `a != b + c` instead of `not a == b + c` | = help: Replace with `!=` operator -ℹ Suggested fix +ℹ Fix 3 3 | pass 4 4 | 5 5 | # SIM201 @@ -46,7 +46,7 @@ SIM201.py:10:4: SIM201 [*] Use `a + b != c` instead of `not a + b == c` | = help: Replace with `!=` operator -ℹ Suggested fix +ℹ Fix 7 7 | pass 8 8 | 9 9 | # SIM201 diff --git a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM910_SIM910.py.snap b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM910_SIM910.py.snap index 66f8de5edb7c2..ec9679148666a 100644 --- a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM910_SIM910.py.snap +++ b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM910_SIM910.py.snap @@ -11,7 +11,7 @@ SIM910.py:2:1: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)` | = help: Replace `{}.get(key, None)` with `{}.get(key)` -ℹ Suggested fix +ℹ Fix 1 1 | # SIM910 2 |-{}.get(key, None) 2 |+{}.get(key) @@ -29,7 +29,7 @@ SIM910.py:5:1: SIM910 [*] Use `{}.get("key")` instead of `{}.get("key", None)` | = help: Replace `{}.get("key", None)` with `{}.get("key")` -ℹ Suggested fix +ℹ Fix 2 2 | {}.get(key, None) 3 3 | 4 4 | # SIM910 @@ -48,7 +48,7 @@ SIM910.py:20:9: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)` | = help: Replace `{}.get(key, None)` with `{}.get(key)` -ℹ Suggested fix +ℹ Fix 17 17 | {}.get("key", False) 18 18 | 19 19 | # SIM910 @@ -68,7 +68,7 @@ SIM910.py:24:5: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)` | = help: Replace `{}.get(key, None)` with `{}.get(key)` -ℹ Suggested fix +ℹ Fix 21 21 | pass 22 22 | 23 23 | # SIM910 @@ -86,7 +86,7 @@ SIM910.py:27:1: SIM910 [*] Use `({}).get(key)` instead of `({}).get(key, None)` | = help: Replace `({}).get(key, None)` with `({}).get(key)` -ℹ Suggested fix +ℹ Fix 24 24 | a = {}.get(key, None) 25 25 | 26 26 | # SIM910