diff --git a/changelog/fix_wrap_in_parens_before_replacing_unless.md b/changelog/fix_wrap_in_parens_before_replacing_unless.md new file mode 100644 index 000000000000..c833c98146ee --- /dev/null +++ b/changelog/fix_wrap_in_parens_before_replacing_unless.md @@ -0,0 +1 @@ +* [#9350](https://github.com/rubocop-hq/rubocop/pull/9350): Wrap in parens before replacing `unless` with `if` and `!`. ([@magneland][]) diff --git a/lib/rubocop/cop/style/sole_nested_conditional.rb b/lib/rubocop/cop/style/sole_nested_conditional.rb index 17024e963c87..b53f2d80c7a4 100644 --- a/lib/rubocop/cop/style/sole_nested_conditional.rb +++ b/lib/rubocop/cop/style/sole_nested_conditional.rb @@ -63,13 +63,13 @@ def offending_branch?(branch) end def autocorrect(corrector, node, if_branch) + corrector.wrap(node.condition, '(', ')') if node.condition.or_type? + if node.unless? corrector.replace(node.loc.keyword, 'if') corrector.insert_before(node.condition, '!') end - corrector.wrap(node.condition, '(', ')') if node.condition.or_type? - and_operator = if_branch.unless? ? ' && !' : ' && ' if if_branch.modifier_form? correct_for_guard_condition_style(corrector, node, if_branch, and_operator)