From f9c2f7b25b7d631154222a57dd94f64e2310708b Mon Sep 17 00:00:00 2001 From: vasco Date: Wed, 14 May 2025 08:20:28 +0200 Subject: [PATCH 1/3] add fix safety section --- .../rules/flake8_simplify/rules/needless_bool.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 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 9b73053450e2c..792e9ca042c2b 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 @@ -32,16 +32,22 @@ use crate::fix::snippet::SourceCodeSnippet; /// /// Or, given: /// ```python -/// if x > 0: -/// return True -/// return False +/// 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 /// ``` /// +/// ## Fix safety +/// +/// The fix is marked as unsafe because it could remove comments. +/// /// ## References /// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) #[derive(ViolationMetadata)] From f2274d300f66290d00da3a4ab8ec9695180dbfe7 Mon Sep 17 00:00:00 2001 From: vasco Date: Wed, 14 May 2025 08:38:17 +0200 Subject: [PATCH 2/3] corrections --- .../rules/flake8_simplify/rules/needless_bool.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 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 792e9ca042c2b..aa54afdb5d0ee 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,15 +19,17 @@ use crate::fix::snippet::SourceCodeSnippet; /// ## Example /// Given: /// ```python -/// if x > 0: -/// return True -/// else: -/// return False +/// def foo(x: int) -> bool: +/// if x > 0: +/// return True +/// else: +/// return False /// ``` /// /// Use instead: /// ```python -/// return x > 0 +/// def foo(x: int) -> bool: +/// return x > 0 /// ``` /// /// Or, given: @@ -46,7 +48,8 @@ use crate::fix::snippet::SourceCodeSnippet; /// /// ## Fix safety /// -/// The fix is marked as unsafe because it could remove comments. +/// This fix is marked as unsafe because it may change the program’s behavior if the condition does not +/// return a proper Boolean. Additionally, the fix could remove comments. /// /// ## References /// - [Python documentation: Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) From e45a6bb4172f4f3990ea0bb6dd74755fcf5e0d1c Mon Sep 17 00:00:00 2001 From: vasco Date: Wed, 14 May 2025 19:16:20 +0200 Subject: [PATCH 3/3] delete sentence about comments and add new sentence about comparison functions. --- .../src/rules/flake8_simplify/rules/needless_bool.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 aa54afdb5d0ee..38d79fa0d4b24 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 @@ -49,7 +49,9 @@ use crate::fix::snippet::SourceCodeSnippet; /// ## Fix safety /// /// This fix is marked as unsafe because it may change the program’s behavior if the condition does not -/// return a proper Boolean. Additionally, the fix could remove comments. +/// 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)