From 68559fc17d492f335dbab93214f6862b6b8c5515 Mon Sep 17 00:00:00 2001 From: Vasco Schiavo <115561717+VascoSch92@users.noreply.github.com> Date: Wed, 14 May 2025 20:24:15 +0200 Subject: [PATCH] [`flake8-simplify`] add fix safety section (`SIM103`) (#18086) The PR add the `fix safety` section for rule `SIM103` (#15584 ) ### Unsafe Fix Example ```python class Foo: def __eq__(self, other): return 1 def foo(): if Foo() == 1: return True return False def foo_fix(): return Foo() == 1 print(foo()) # True print(foo_fix()) # 1 ``` ### Note I updated the code snippet example, because I thought it was cool to have a correct example, i.e., that I can paste inside the playground and it works :-) --- .../flake8_simplify/rules/needless_bool.rs | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) 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 9b73053450..38d79fa0d4 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 @@ -19,28 +19,39 @@ use crate::fix::snippet::SourceCodeSnippet; /// ## Example /// Given: /// ```python -/// if x > 0: -/// return True -/// else: +/// def foo(x: int) -> bool: +/// if x > 0: +/// return True +/// else: +/// return False +/// ``` +/// +/// Use instead: +/// ```python +/// def foo(x: int) -> bool: +/// return x > 0 +/// ``` +/// +/// Or, given: +/// ```python +/// def foo(x: int) -> bool: +/// if x > 0: +/// return True /// return False /// ``` /// /// Use instead: /// ```python -/// return x > 0 +/// def foo(x: int) -> bool: +/// return x > 0 /// ``` /// -/// Or, given: -/// ```python -/// if x > 0: -/// return True -/// return False -/// ``` +/// ## Fix safety /// -/// Use instead: -/// ```python -/// return x > 0 -/// ``` +/// This fix is marked as unsafe because it may change the program’s behavior if the condition does not +/// return a proper Boolean. While the fix will try to wrap non-boolean values in a call to bool, +/// custom implementations of comparison functions like `__eq__` can avoid the bool call and still +/// lead to altered behavior. /// /// ## References /// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing)