From 6889e9cb2b47922f8c6f1de3c1af8498022f3aae Mon Sep 17 00:00:00 2001 From: Nick Hrynuik Date: Thu, 25 Jun 2020 21:44:37 -0400 Subject: [PATCH] Fix RedundantParentheses yielding hash literal --- CHANGELOG.md | 2 ++ lib/rubocop/cop/style/redundant_parentheses.rb | 8 +++++++- spec/rubocop/cop/style/redundant_parentheses_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7de2ab0aed6..dfd8931641d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#8195](https://github.com/rubocop-hq/rubocop/issues/8195): Fix an error for `Style/RedundantFetchBlock` when using `#fetch` with empty block. ([@koic][]) * [#8193](https://github.com/rubocop-hq/rubocop/issues/8193): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using `[\b]`. ([@owst][]) * [#8205](https://github.com/rubocop-hq/rubocop/issues/8205): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using a leading escaped `]`. ([@owst][]) +* [#8208](https://github.com/rubocop-hq/rubocop/issues/8208): Fix `Style/RedundantParentheses` with hash literal as first argument to `yield`. ([@karlwithak][]) ### Changes @@ -4631,3 +4632,4 @@ [@avrusanov]: https://github.com/avrusanov [@mauro-oto]: https://github.com/mauro-oto [@fatkodima]: https://github.com/fatkodima +[@karlwithak]: https://github.com/karlwithak diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 1039892597c..de2055bc609 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -205,7 +205,9 @@ def only_begin_arg?(args) end def first_argument?(node) - first_send_argument?(node) || first_super_argument?(node) + first_send_argument?(node) || + first_super_argument?(node) || + first_yield_argument?(node) end def_node_matcher :first_send_argument?, <<~PATTERN @@ -216,6 +218,10 @@ def first_argument?(node) ^(super equal?(%0) ...) PATTERN + def_node_matcher :first_yield_argument?, <<~PATTERN + ^(yield equal?(%0) ...) + PATTERN + def call_chain_starts_with_int?(begin_node, send_node) recv = first_part_of_call_chain(send_node) recv&.int_type? && (parent = begin_node.parent) && diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index cdc1ba3c69f..42d6fffce67 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -278,4 +278,12 @@ def x }) RUBY end + + it 'accepts parentheses in yield call with hash' do + expect_no_offenses(<<~RUBY) + yield ({ + foo: bar, + }) + RUBY + end end