From 366e54f27f37e2db15b88e48e4169186cb0192ce Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Thu, 31 Oct 2024 15:22:28 +0100 Subject: [PATCH 1/4] Docs: clarify that this rule is opinionated --- .../rules/if_else_block_instead_of_if_exp.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs index d113bd46c9efc..8d25b572db554 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs @@ -18,6 +18,11 @@ use crate::fix::edits::fits; /// `if`-`else`-blocks that assign a value to a variable in both branches can /// be expressed more concisely by using a ternary or binary operator. /// +/// Note that some users find this rule is not suitable as ternary +/// operators restrict [code coverage] for tools that use line profiling +/// and that they can make code harder to parse, especially with complex +/// conditions. +/// /// ## Example /// /// ```python @@ -51,6 +56,7 @@ use crate::fix::edits::fits; /// - [Python documentation: Conditional expressions](https://docs.python.org/3/reference/expressions.html#conditional-expressions) /// /// [preview]: https://docs.astral.sh/ruff/preview/ +/// [code coverage]: https://github.com/nedbat/coveragepy/issues/509 #[violation] pub struct IfElseBlockInsteadOfIfExp { /// The ternary or binary expression to replace the `if`-`else`-block. From 19e66f90dd32edac8562b203c7838a088c4b79b8 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 31 Oct 2024 17:17:30 +0000 Subject: [PATCH 2/4] tweaks --- .../rules/if_else_block_instead_of_if_exp.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs index 8d25b572db554..cd1a284e12d55 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs @@ -18,11 +18,6 @@ use crate::fix::edits::fits; /// `if`-`else`-blocks that assign a value to a variable in both branches can /// be expressed more concisely by using a ternary or binary operator. /// -/// Note that some users find this rule is not suitable as ternary -/// operators restrict [code coverage] for tools that use line profiling -/// and that they can make code harder to parse, especially with complex -/// conditions. -/// /// ## Example /// /// ```python @@ -51,6 +46,11 @@ use crate::fix::edits::fits; /// ```python /// z = cond or other_cond /// ``` +/// ## Known issues +/// This is an opinionated style rule that may not always be to everyone's +/// taste, especially if the code makes use of complex `if` conditions. +/// Ternary operators can also make it harder to measure [code coverage] +/// with tools that use line profiling. /// /// ## References /// - [Python documentation: Conditional expressions](https://docs.python.org/3/reference/expressions.html#conditional-expressions) From 1d87cb1c67b2583b323d7aea95e8ee5f9fe0f439 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 31 Oct 2024 17:18:28 +0000 Subject: [PATCH 3/4] Update if_else_block_instead_of_if_exp.rs --- .../flake8_simplify/rules/if_else_block_instead_of_if_exp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs index cd1a284e12d55..73f11775a32ac 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs @@ -48,7 +48,7 @@ use crate::fix::edits::fits; /// ``` /// ## Known issues /// This is an opinionated style rule that may not always be to everyone's -/// taste, especially if the code makes use of complex `if` conditions. +/// taste, especially for code that makes use of complex `if` conditions. /// Ternary operators can also make it harder to measure [code coverage] /// with tools that use line profiling. /// From 056ba9bb6f37b33a6e9a2db98c22ea5f0143ccf3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 31 Oct 2024 17:19:35 +0000 Subject: [PATCH 4/4] Update if_else_block_instead_of_if_exp.rs --- .../flake8_simplify/rules/if_else_block_instead_of_if_exp.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs index 73f11775a32ac..81c86bf760a96 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_if_exp.rs @@ -46,6 +46,7 @@ use crate::fix::edits::fits; /// ```python /// z = cond or other_cond /// ``` +/// /// ## Known issues /// This is an opinionated style rule that may not always be to everyone's /// taste, especially for code that makes use of complex `if` conditions.