From 3cba8231ee4ba4ce948b88cf51c6e9bf7c09b4b7 Mon Sep 17 00:00:00 2001 From: Dmytro Savochkin Date: Sun, 9 Aug 2020 13:50:30 +0300 Subject: [PATCH] [Fix #8497] Fix Style/IfUnlessModifier to handle if-end condition in method argument list --- CHANGELOG.md | 1 + lib/rubocop/cop/mixin/statement_modifier.rb | 2 +- spec/rubocop/cop/style/if_unless_modifier_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 523c69ecbec..2a57774a073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#8480](https://github.com/rubocop-hq/rubocop/issues/8480): Tweak callback list of `Lint/MissingSuper`. ([@marcandre][]) * [#8481](https://github.com/rubocop-hq/rubocop/pull/8481): Fix autocorrect for elements with newlines in `Style/SymbolArray` and `Style/WordArray`. ([@biinari][]) * [#8475](https://github.com/rubocop-hq/rubocop/issues/8475): Fix a false positive for `Style/HashAsLastArrayItem` when there are duplicate hashes in the array. ([@wcmonty][]) +* [#8497](https://github.com/rubocop-hq/rubocop/issues/8497): Fix `Style/IfUnlessModifier` to add parentheses when converting if-end condition inside a parenthesized method argument list. ([@dsavochkin][]) ### Changes diff --git a/lib/rubocop/cop/mixin/statement_modifier.rb b/lib/rubocop/cop/mixin/statement_modifier.rb index 587b8ff0286..4fe2be136e3 100644 --- a/lib/rubocop/cop/mixin/statement_modifier.rb +++ b/lib/rubocop/cop/mixin/statement_modifier.rb @@ -72,7 +72,7 @@ def parenthesize?(node) return true if parent.assignment? || parent.operator_keyword? return true if %i[array pair].include?(parent.type) - node.parent.send_type? && !node.parent.parenthesized? + node.parent.send_type? end def max_line_length diff --git a/spec/rubocop/cop/style/if_unless_modifier_spec.rb b/spec/rubocop/cop/style/if_unless_modifier_spec.rb index cf5fa83560c..4cbb342b335 100644 --- a/spec/rubocop/cop/style/if_unless_modifier_spec.rb +++ b/spec/rubocop/cop/style/if_unless_modifier_spec.rb @@ -457,7 +457,7 @@ def f end context 'if-end is argument to a parenthesized method call' do - it "doesn't add redundant parentheses" do + it 'adds parentheses because otherwise it would cause SyntaxError' do expect_offense(<<~RUBY) puts("string", if a ^^ Favor modifier `if` usage when having a single-line body. [...] @@ -466,7 +466,7 @@ def f RUBY expect_correction(<<~RUBY) - puts("string", 1 if a) + puts("string", (1 if a)) RUBY end end