From 8d4f95afc380adfece0ded3016e92234fd9b73dd Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 26 Nov 2020 21:02:37 +0900 Subject: [PATCH] [Fix #9105] Fix an incorrect auto-correct for `Style/RedundantCondition` Fixes #9105 This PR fixes an incorrect auto-correct for `Style/RedundantCondition` when using operator method in `else`. --- ...t_autocorrect_for_style_redundant_condition.md | 1 + lib/rubocop/cop/style/redundant_condition.rb | 3 ++- .../rubocop/cop/style/redundant_condition_spec.rb | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_incorrect_autocorrect_for_style_redundant_condition.md diff --git a/changelog/fix_an_incorrect_autocorrect_for_style_redundant_condition.md b/changelog/fix_an_incorrect_autocorrect_for_style_redundant_condition.md new file mode 100644 index 00000000000..92c66185285 --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_style_redundant_condition.md @@ -0,0 +1 @@ +* [#9105](https://github.com/rubocop-hq/rubocop/issues/9105): Fix an incorrect auto-correct for `Style/RedundantCondition` when using operator method in `else`. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_condition.rb b/lib/rubocop/cop/style/redundant_condition.rb index d048a1fe611..39a6eb0caca 100644 --- a/lib/rubocop/cop/style/redundant_condition.rb +++ b/lib/rubocop/cop/style/redundant_condition.rb @@ -131,7 +131,8 @@ def require_parentheses?(node) end def without_argument_parentheses_method?(node) - node.send_type? && !node.arguments.empty? && !node.parenthesized? + node.send_type? && + !node.arguments.empty? && !node.parenthesized? && !node.operator_method? end end end diff --git a/spec/rubocop/cop/style/redundant_condition_spec.rb b/spec/rubocop/cop/style/redundant_condition_spec.rb index 3e2ad9db831..9fb062ff1a6 100644 --- a/spec/rubocop/cop/style/redundant_condition_spec.rb +++ b/spec/rubocop/cop/style/redundant_condition_spec.rb @@ -82,6 +82,21 @@ RUBY end + it 'registers an offense and corrects when using operator method in `else`' do + expect_offense(<<~RUBY) + if b + ^^^^ Use double pipes `||` instead. + b + else + c + d + end + RUBY + + expect_correction(<<~RUBY) + b || c + d + RUBY + end + it 'registers an offense and corrects complex one liners' do expect_offense(<<~RUBY) if b