Skip to content

Commit ba66b6f

Browse files
committed
[Fix #626] Fix a false positive for Rails/CompactBlank
Fixes #626. Fix a false positive for `Rails/CompactBlank` when using the receiver of `blank?` is not a block variable.
1 parent ba2a7e3 commit ba66b6f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#626](https://github.com/rubocop/rubocop-rails/issues/626): Fix a false positive for `Rails/CompactBlank` when using the receiver of `blank?` is not a block variable. ([@koic][])

lib/rubocop/cop/rails/compact_blank.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ def bad_method?(node)
7373
return true if reject_with_block_pass?(node)
7474

7575
if (arguments, receiver_in_block = reject_with_block?(node.parent))
76-
return arguments.length == 1 || use_hash_value_block_argument?(arguments, receiver_in_block)
76+
return use_single_value_block_argument?(arguments, receiver_in_block) ||
77+
use_hash_value_block_argument?(arguments, receiver_in_block)
7778
end
7879

7980
false
8081
end
8182

83+
def use_single_value_block_argument?(arguments, receiver_in_block)
84+
arguments.length == 1 && arguments[0].source == receiver_in_block.source
85+
end
86+
8287
def use_hash_value_block_argument?(arguments, receiver_in_block)
8388
arguments.length == 2 && arguments[1].source == receiver_in_block.source
8489
end

spec/rubocop/cop/rails/compact_blank_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@
118118
collection.reject { |k, v| k.empty? }
119119
RUBY
120120
end
121+
122+
it 'does not register an offense when using the receiver of `blank?` is not a block variable' do
123+
expect_no_offenses(<<~RUBY)
124+
def foo(arg)
125+
collection.reject { |_| arg.blank? }
126+
end
127+
RUBY
128+
end
121129
end
122130

123131
context 'Rails <= 6.0', :rails60 do

0 commit comments

Comments
 (0)