diff --git a/changelog/fix_allow_ambigious_block_association_to_match_source.md b/changelog/fix_allow_ambigious_block_association_to_match_source.md new file mode 100644 index 00000000000..70cd36ac9a6 --- /dev/null +++ b/changelog/fix_allow_ambigious_block_association_to_match_source.md @@ -0,0 +1 @@ +* [#10969](https://github.com/rubocop/rubocop/issues/10969): Fix a false negative for `AllowedPatterns` of `Lint/AmbiguousBlockAssociation` when using a method chain. ([@jcalvert][]) diff --git a/lib/rubocop/cop/lint/ambiguous_block_association.rb b/lib/rubocop/cop/lint/ambiguous_block_association.rb index 8ea895fb0ec..1f316e01c6f 100644 --- a/lib/rubocop/cop/lint/ambiguous_block_association.rb +++ b/lib/rubocop/cop/lint/ambiguous_block_association.rb @@ -81,7 +81,7 @@ def ambiguous_block_association?(send_node) def allowed_method_pattern?(node) node.assignment? || node.operator_method? || node.method?(:[]) || allowed_method?(node.last_argument.method_name) || - matches_allowed_pattern?(node.last_argument.method_name) + matches_allowed_pattern?(node.last_argument.send_node.source) end def message(send_node) diff --git a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb index d77adcff4da..9c009b1da7c 100644 --- a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb @@ -104,13 +104,16 @@ end context 'when AllowedPatterns is enabled' do - let(:cop_config) { { 'AllowedPatterns' => [/change/] } } + let(:cop_config) { { 'AllowedPatterns' => [/change/, /receive\(.*?\)\.twice/] } } it 'does not register an offense for an allowed method' do expect_no_offenses(<<~RUBY) - expect { order.expire }.to change { order.events } expect { order.expire }.to not_change { order.events } RUBY + + expect_no_offenses(<<~RUBY) + expect(order).to receive(:complete).twice { OrderCount.update! } + RUBY end it 'registers an offense for other methods' do