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