From a115da9ba06064b1aaef4ae80f221ede9e28e71e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 22 Apr 2022 16:47:51 +0900 Subject: [PATCH] [Fix #10566] Fix a falase positive for `Lint/AmbiguousBlockAssociation` Fixes #10566. This PR fixes a false positive for `Lint/AmbiguousBlockAssociation` when using proc is used as a last argument. --- ...ix_a_false_positive_for_lint_ambiguous_block_association.md | 1 + lib/rubocop/cop/lint/ambiguous_block_association.rb | 3 ++- spec/rubocop/cop/lint/ambiguous_block_association_spec.rb | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_positive_for_lint_ambiguous_block_association.md diff --git a/changelog/fix_a_false_positive_for_lint_ambiguous_block_association.md b/changelog/fix_a_false_positive_for_lint_ambiguous_block_association.md new file mode 100644 index 00000000000..a6ab77e8039 --- /dev/null +++ b/changelog/fix_a_false_positive_for_lint_ambiguous_block_association.md @@ -0,0 +1 @@ +* [#10566](https://github.com/rubocop/rubocop/issues/10566): Fix a false positive for `Lint/AmbiguousBlockAssociation` when using proc is used as a last argument. ([@koic][]) diff --git a/lib/rubocop/cop/lint/ambiguous_block_association.rb b/lib/rubocop/cop/lint/ambiguous_block_association.rb index 3f0a4770d77..2aba21e409a 100644 --- a/lib/rubocop/cop/lint/ambiguous_block_association.rb +++ b/lib/rubocop/cop/lint/ambiguous_block_association.rb @@ -44,7 +44,8 @@ def on_send(node) return unless node.arguments? return unless ambiguous_block_association?(node) - return if node.parenthesized? || node.last_argument.lambda? || allowed_method?(node) + return if node.parenthesized? || node.last_argument.lambda? || node.last_argument.proc? || + allowed_method?(node) message = message(node) diff --git a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb index e39507128c9..ec498a63b59 100644 --- a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb @@ -22,6 +22,8 @@ it_behaves_like 'accepts', 'Proc.new { puts "proc" }' it_behaves_like 'accepts', 'expect { order.save }.to(change { orders.size })' it_behaves_like 'accepts', 'scope :active, -> { where(status: "active") }' + it_behaves_like 'accepts', 'scope :active, proc { where(status: "active") }' + it_behaves_like 'accepts', 'scope :active, Proc.new { where(status: "active") }' it_behaves_like('accepts', 'assert_equal posts.find { |p| p.title == "Foo" }, results.first') it_behaves_like('accepts', 'assert_equal(posts.find { |p| p.title == "Foo" }, results.first)') it_behaves_like('accepts', 'assert_equal(results.first, posts.find { |p| p.title == "Foo" })')