Skip to content

Commit

Permalink
[Fix rubocop#9713] Fix autocorrection for block local variables in `L…
Browse files Browse the repository at this point in the history
…int/UnusedBlockArgument`

Closes rubocop#9713
  • Loading branch information
tejasbubane committed Apr 20, 2021
1 parent 76bc0a0 commit b63aa80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9713](https://github.com/rubocop/rubocop/issues/9713): Fix autocorrection for block local variables in `Lint/UnusedBlockArgument`. ([@tejasbubane][])
8 changes: 7 additions & 1 deletion lib/rubocop/cop/lint/unused_block_argument.rb
Expand Up @@ -67,11 +67,17 @@ def autocorrect(corrector, node)
end

def check_argument(variable)
return if allowed_block?(variable) || allowed_keyword_argument?(variable)
return if allowed_block?(variable) ||
allowed_keyword_argument?(variable) ||
used_block_local?(variable)

super
end

def used_block_local?(variable)
variable.explicit_block_local_variable? && !variable.assignments.empty?
end

def allowed_block?(variable)
!variable.block_argument? || (ignore_empty_blocks? && empty_block?(variable))
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/unused_block_argument_spec.rb
Expand Up @@ -214,6 +214,17 @@
RUBY
end
end

context 'and the variable is used' do
it 'does not register offense' do
expect_no_offenses(<<~RUBY)
1.times do |index; x|
x = 10
puts index
end
RUBY
end
end
end

context 'when a lambda block takes arguments' do
Expand Down

0 comments on commit b63aa80

Please sign in to comment.