From 7e5c19385c02b1f5a3b28b32467d0ea14d3b185e Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sat, 11 Feb 2023 06:39:09 +0100 Subject: [PATCH] Rename return-bool-condition-directly to needless-bool --- README.md | 2 +- crates/ruff/src/checkers/ast.rs | 6 +----- crates/ruff/src/registry.rs | 2 +- crates/ruff/src/rules/flake8_simplify/mod.rs | 2 +- .../ruff/src/rules/flake8_simplify/rules/ast_if.rs | 14 ++++++-------- crates/ruff/src/rules/flake8_simplify/rules/mod.rs | 3 +-- ...__flake8_simplify__tests__SIM103_SIM103.py.snap | 12 ++++++------ 7 files changed, 17 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e65529b44c..e1a83da78e 100644 --- a/README.md +++ b/README.md @@ -1218,7 +1218,7 @@ For more, see [flake8-simplify](https://pypi.org/project/flake8-simplify/) on Py | ---- | ---- | ------- | --- | | SIM101 | duplicate-isinstance-call | Multiple `isinstance` calls for `{name}`, merge into a single call | 🛠 | | SIM102 | nested-if-statements | Use a single `if` statement instead of nested `if` statements | 🛠 | -| SIM103 | return-bool-condition-directly | Return the condition `{condition}` directly | 🛠 | +| SIM103 | needless-bool | Return the condition `{condition}` directly | 🛠 | | SIM105 | use-contextlib-suppress | Use `contextlib.suppress({exception})` instead of try-except-pass | | | SIM107 | return-in-try-except-finally | Don't use `return` in `try`/`except` and `finally` | | | SIM108 | use-ternary-operator | Use ternary operator `{contents}` instead of if-else-block | 🛠 | diff --git a/crates/ruff/src/checkers/ast.rs b/crates/ruff/src/checkers/ast.rs index 82f398e492..25e3ae8b31 100644 --- a/crates/ruff/src/checkers/ast.rs +++ b/crates/ruff/src/checkers/ast.rs @@ -1490,11 +1490,7 @@ where self.current_stmt_parent().map(Into::into), ); } - if self - .settings - .rules - .enabled(&Rule::ReturnBoolConditionDirectly) - { + if self.settings.rules.enabled(&Rule::NeedlessBool) { flake8_simplify::rules::return_bool_condition_directly(self, stmt); } if self.settings.rules.enabled(&Rule::UseTernaryOperator) { diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index 0dc874533a..df40b4a3d6 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -255,7 +255,7 @@ ruff_macros::define_rule_mapping!( SIM115 => rules::flake8_simplify::rules::OpenFileWithContextHandler, SIM101 => rules::flake8_simplify::rules::DuplicateIsinstanceCall, SIM102 => rules::flake8_simplify::rules::NestedIfStatements, - SIM103 => rules::flake8_simplify::rules::ReturnBoolConditionDirectly, + SIM103 => rules::flake8_simplify::rules::NeedlessBool, SIM105 => rules::flake8_simplify::rules::UseContextlibSuppress, SIM107 => rules::flake8_simplify::rules::ReturnInTryExceptFinally, SIM108 => rules::flake8_simplify::rules::UseTernaryOperator, diff --git a/crates/ruff/src/rules/flake8_simplify/mod.rs b/crates/ruff/src/rules/flake8_simplify/mod.rs index 950be23d50..78fa29ffb7 100644 --- a/crates/ruff/src/rules/flake8_simplify/mod.rs +++ b/crates/ruff/src/rules/flake8_simplify/mod.rs @@ -14,7 +14,7 @@ mod tests { #[test_case(Rule::DuplicateIsinstanceCall, Path::new("SIM101.py"); "SIM101")] #[test_case(Rule::NestedIfStatements, Path::new("SIM102.py"); "SIM102")] - #[test_case(Rule::ReturnBoolConditionDirectly, Path::new("SIM103.py"); "SIM103")] + #[test_case(Rule::NeedlessBool, Path::new("SIM103.py"); "SIM103")] #[test_case(Rule::UseContextlibSuppress, Path::new("SIM105.py"); "SIM105")] #[test_case(Rule::ReturnInTryExceptFinally, Path::new("SIM107.py"); "SIM107")] #[test_case(Rule::UseTernaryOperator, Path::new("SIM108.py"); "SIM108")] 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 3a1bc8e765..2ddcf7eb5d 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs @@ -39,26 +39,24 @@ impl Violation for NestedIfStatements { } define_violation!( - pub struct ReturnBoolConditionDirectly { + pub struct NeedlessBool { pub condition: String, pub fixable: bool, } ); -impl Violation for ReturnBoolConditionDirectly { +impl Violation for NeedlessBool { const AUTOFIX: Option = Some(AutofixKind::new(Availability::Sometimes)); #[derive_message_formats] fn message(&self) -> String { - let ReturnBoolConditionDirectly { condition, .. } = self; + let NeedlessBool { condition, .. } = self; format!("Return the condition `{condition}` directly") } fn autofix_title_formatter(&self) -> Option String> { - let ReturnBoolConditionDirectly { fixable, .. } = self; + let NeedlessBool { fixable, .. } = self; if *fixable { - Some(|ReturnBoolConditionDirectly { condition, .. }| { - format!("Replace with `return {condition}`") - }) + Some(|NeedlessBool { condition, .. }| format!("Replace with `return {condition}`")) } else { None } @@ -288,7 +286,7 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) { && (matches!(test.node, ExprKind::Compare { .. }) || checker.is_builtin("bool")); let mut diagnostic = Diagnostic::new( - ReturnBoolConditionDirectly { condition, fixable }, + NeedlessBool { condition, fixable }, Range::from_located(stmt), ); if fixable && checker.patch(diagnostic.kind.rule()) { diff --git a/crates/ruff/src/rules/flake8_simplify/rules/mod.rs b/crates/ruff/src/rules/flake8_simplify/rules/mod.rs index 638ec033ed..48fcf4bbc0 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/mod.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/mod.rs @@ -6,8 +6,7 @@ pub use ast_expr::{use_capital_environment_variables, UseCapitalEnvironmentVaria pub use ast_for::{convert_for_loop_to_any_all, ConvertLoopToAll, ConvertLoopToAny}; pub use ast_if::{ nested_if_statements, return_bool_condition_directly, use_dict_get_with_default, - use_ternary_operator, DictGetWithDefault, NestedIfStatements, ReturnBoolConditionDirectly, - UseTernaryOperator, + use_ternary_operator, DictGetWithDefault, NeedlessBool, NestedIfStatements, UseTernaryOperator, }; pub use ast_ifexp::{ explicit_false_true_in_ifexpr, explicit_true_false_in_ifexpr, twisted_arms_in_ifexpr, diff --git a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM103_SIM103.py.snap b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM103_SIM103.py.snap index 48a9568e3a..bdf4a0593b 100644 --- a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM103_SIM103.py.snap +++ b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM103_SIM103.py.snap @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs expression: diagnostics --- - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: a fixable: true location: @@ -23,7 +23,7 @@ expression: diagnostics column: 20 parent: ~ - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: a == b fixable: true location: @@ -43,7 +43,7 @@ expression: diagnostics column: 20 parent: ~ - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: b fixable: true location: @@ -63,7 +63,7 @@ expression: diagnostics column: 20 parent: ~ - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: b fixable: true location: @@ -83,7 +83,7 @@ expression: diagnostics column: 24 parent: ~ - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: a fixable: false location: @@ -95,7 +95,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - ReturnBoolConditionDirectly: + NeedlessBool: condition: a fixable: false location: