Skip to content

Commit 6414db1

Browse files
smakagonbbatsov
authored andcommitted
[Fix #4227] Address an AmbiguousBlockAssociation false positive (#4228)
1 parent 022e732 commit 6414db1

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [#4226](https://github.com/bbatsov/rubocop/pull/4226): Show in `--help` output that `--stdin` takes a file name argument. ([@jonas054][])
2424
* [#4217](https://github.com/bbatsov/rubocop/pull/4217): Fix false positive in `Rails/FilePath` cop with non string argument. ([@soutaro][])
2525
* [#4106](https://github.com/bbatsov/rubocop/pull/4106): Make `Style/TernaryParentheses` unsafe autocorrect detector aware of literals and constants. ([@drenmi][])
26+
* [#4228](https://github.com/bbatsov/rubocop/pull/4228): Fix false positive in `Lint/AmbiguousBlockAssociation` cop. ([@smakagon][])
2627

2728
## 0.48.0 (2017-03-26)
2829

lib/rubocop/cop/lint/ambiguous_block_association.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def on_send(node)
3232
return if node.parenthesized? || allowed_method?(node)
3333
return if lambda_argument?(node.first_argument)
3434

35-
return unless method_with_block?(node.first_argument)
36-
first_param = node.first_argument.children.first
37-
return unless method_as_param?(first_param)
35+
return unless method_with_block?(node.last_argument)
36+
last_param = node.last_argument.children.first
37+
return unless method_as_param?(last_param)
3838

39-
add_offense(node, :expression, message(first_param, node.method_name))
39+
add_offense(node, :expression, message(last_param, node.method_name))
4040
end
4141

4242
private

spec/rubocop/cop/lint/ambiguous_block_association_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
it_behaves_like 'accepts', 'foo = lambda do |diagnostic|;end'
3131
it_behaves_like 'accepts', 'Proc.new { puts "proc" }'
3232
it_behaves_like('accepts', 'expect { order.save }.to(change { orders.size })')
33+
it_behaves_like(
34+
'accepts',
35+
'assert_equal posts.find { |p| p.title == "Foo" }, results.first'
36+
)
37+
it_behaves_like(
38+
'accepts',
39+
'assert_equal(posts.find { |p| p.title == "Foo" }, results.first)'
40+
)
41+
it_behaves_like(
42+
'accepts',
43+
'assert_equal(results.first, posts.find { |p| p.title == "Foo" })'
44+
)
3345
it_behaves_like(
3446
'accepts',
3547
'allow(cop).to receive(:on_int) { raise RuntimeError }'

0 commit comments

Comments
 (0)