Skip to content

Commit

Permalink
Ignore unused block parameter if it is a keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Volker Tietz committed Mar 30, 2016
1 parent 5c6f9de commit 8f01fbe
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#2983](https://github.com/bbatsov/rubocop/pull/2983): `Style/AlignParameters` message was clarified for `with_fixed_indentation` style. ([@dylanahsmith][])
* [#2314](https://github.com/bbatsov/rubocop/pull/2314): Ignore `UnusedBlockArgument` for keyword arguments. ([@volkert][])

## 0.39.0 (27/03/2016)

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,10 @@ Lint/UnusedMethodArgument:
AllowUnusedKeywordArguments: false
IgnoreEmptyMethods: true

# Checks for unused block arguments.
Lint/UnusedBlockArgument:
AllowUnusedKeywordArguments: false

##################### Performance ############################

Performance/RedundantMerge:
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/lint/unused_block_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class UnusedBlockArgument < Cop

def check_argument(variable)
return unless variable.block_argument?
return if variable.keyword_argument? &&
cop_config && cop_config['AllowUnusedKeywordArguments']

if cop_config['IgnoreEmptyBlocks']
_send, _args, body = *variable.scope.node
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ def interrupt
' super(arg1, arg2) { |response| }',
' end',
'end'])
create_file('.rubocop.yml', ['Lint/UnusedBlockArgument:',
' IgnoreEmptyBlocks: true'])
expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
expect($stdout.string)
.to eq(['',
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
context 'when offense was corrected' do
before do
allow(@cop).to receive(:autocorrect?).and_return(true)
allow(@cop).to receive(:autocorrect).and_return(->(corrector) {})
allow(@cop).to receive(:autocorrect).and_return(->(_corrector) {})
end

it 'is set to true' do
Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/cop/lint/unused_block_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require 'spec_helper'

describe RuboCop::Cop::Lint::UnusedBlockArgument, :config do
subject(:cop) { described_class.new }
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'AllowUnusedKeywordArguments' => false } }

context 'inspection' do
before do
Expand Down Expand Up @@ -144,6 +145,31 @@
end
end

context 'when an optional keyword argument is unused', ruby: 2 do
let(:source) { <<-END }
define_method(:foo) do |bar: 'default'|
puts 'bar'
end
END

it 'registers an offense but does not suggest underscore-prefix' do
expect(cop.offenses.size).to eq(1)
expect(cop.highlights).to eq(['bar'])
expect(cop.offenses.first.message).to eq(
'Unused block argument - `bar`. ' \
"You can omit the argument if you don't care about it."
)
end

context 'and AllowUnusedKeywordArguments set' do
let(:cop_config) { { 'AllowUnusedKeywordArguments' => true } }

it 'does not care' do
expect(cop.offenses).to be_empty
end
end
end

context 'when a method argument is not used' do
let(:source) { <<-END }
def some_method(foo)
Expand Down

0 comments on commit 8f01fbe

Please sign in to comment.