From 6a819f24b176aca5d42f174aab2ac8f9c07d9730 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 1 Jun 2024 19:13:38 -0400 Subject: [PATCH] Avoid double negative in SIM103 --- .../test/fixtures/flake8_simplify/SIM103.py | 7 ++++++ .../flake8_simplify/rules/needless_bool.rs | 17 ++++++++++--- ...ify__tests__preview__SIM103_SIM103.py.snap | 25 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM103.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM103.py index e1b868888df865..521f3801c07625 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM103.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM103.py @@ -111,3 +111,10 @@ def f(): if a: return False return True + + +def f(): + if not 10 < a: + return False + + return True diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs index 34a151a5158c85..17bcb2b018967b 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs @@ -202,11 +202,20 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) { } else { // If the return values are inverted, wrap the condition in a `not`. if inverted { - Some(Expr::UnaryOp(ast::ExprUnaryOp { + if let Expr::UnaryOp(ast::ExprUnaryOp { op: ast::UnaryOp::Not, - operand: Box::new(if_test.clone()), - range: TextRange::default(), - })) + operand, + .. + }) = if_test + { + Some((&**operand).clone()) + } else { + Some(Expr::UnaryOp(ast::ExprUnaryOp { + op: ast::UnaryOp::Not, + operand: Box::new(if_test.clone()), + range: TextRange::default(), + })) + } } else if if_test.is_compare_expr() { // If the condition is a comparison, we can replace it with the condition, since we // know it's a boolean. diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM103_SIM103.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM103_SIM103.py.snap index a71b6622c25cfc..88d81a825c72b8 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM103_SIM103.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM103_SIM103.py.snap @@ -213,3 +213,28 @@ SIM103.py:111:5: SIM103 [*] Return the condition `not a` directly 112 |- return False 113 |- return True 111 |+ return not a +114 112 | +115 113 | +116 114 | def f(): + +SIM103.py:117:5: SIM103 [*] Return the condition `10 < a` directly + | +116 | def f(): +117 | if not 10 < a: + | _____^ +118 | | return False +119 | | +120 | | return True + | |_______________^ SIM103 + | + = help: Replace with `return 10 < a` + +ℹ Unsafe fix +114 114 | +115 115 | +116 116 | def f(): +117 |- if not 10 < a: +118 |- return False +119 |- +120 |- return True + 117 |+ return 10 < a