From 91a30458f809d4f1f2c187f9be8439d82f24b64c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 24 Oct 2022 21:23:08 +0900 Subject: [PATCH] [Fix #11117] Fix a false positive for `Style/BlockDelimiters` Fixes #11117. This PR fixes a false positive for `Style/BlockDelimiters` when specifying `EnforcedStyle: semantic` and using a single line block with {} followed by a safe navigation method call. --- changelog/fix_a_false_positive_for_block_delimiters.md | 1 + lib/rubocop/cop/style/block_delimiters.rb | 2 +- spec/rubocop/cop/style/block_delimiters_spec.rb | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_positive_for_block_delimiters.md diff --git a/changelog/fix_a_false_positive_for_block_delimiters.md b/changelog/fix_a_false_positive_for_block_delimiters.md new file mode 100644 index 00000000000..2e6ecbfc787 --- /dev/null +++ b/changelog/fix_a_false_positive_for_block_delimiters.md @@ -0,0 +1 @@ +* [#11117](https://github.com/rubocop/rubocop/issues/11117): Fix a false positive for `Style/BlockDelimiters` when specifying `EnforcedStyle: semantic` and using a single line block with {} followed by a safe navigation method call. ([@koic][]) diff --git a/lib/rubocop/cop/style/block_delimiters.rb b/lib/rubocop/cop/style/block_delimiters.rb index ccf2685d352..61121431e3c 100644 --- a/lib/rubocop/cop/style/block_delimiters.rb +++ b/lib/rubocop/cop/style/block_delimiters.rb @@ -425,7 +425,7 @@ def return_value_used?(node) if node.parent.begin_type? return_value_used?(node.parent) else - node.parent.assignment? || node.parent.send_type? + node.parent.assignment? || node.parent.call_type? end end diff --git a/spec/rubocop/cop/style/block_delimiters_spec.rb b/spec/rubocop/cop/style/block_delimiters_spec.rb index 7087930697c..030262f2c83 100644 --- a/spec/rubocop/cop/style/block_delimiters_spec.rb +++ b/spec/rubocop/cop/style/block_delimiters_spec.rb @@ -183,6 +183,10 @@ expect_no_offenses('detect { true }...other') end + it 'accepts a single line block with {} followed by a safe navigation method call' do + expect_no_offenses('ary.map { |e| foo(e) }&.bar') + end + it 'accepts a multi-line functional block with do-end if it is a known procedural method' do expect_no_offenses(<<~RUBY) foo = bar.tap do |x|