Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Lint/UselessTimes #8739

Merged
merged 1 commit into from Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
* [#8726](https://github.com/rubocop-hq/rubocop/issues/8726): Fix a false positive for `Naming/VariableNumber` when naming multibyte character variable name. ([@koic][])
* [#8730](https://github.com/rubocop-hq/rubocop/issues/8730): Fix an error for `Lint/UselessTimes` when there is a blank line in the method definition. ([@koic][])
* [#8740](https://github.com/rubocop-hq/rubocop/issues/8740): Fix a false positive for `Style/HashAsLastArrayItem` when the hash is in an implicit array. ([@dvandersluis][])
* [#8739](https://github.com/rubocop-hq/rubocop/issues/8739): Fix an error for `Lint/UselessTimes` when using empty block argument. ([@koic][])

### Changes

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/useless_times.rb
Expand Up @@ -49,7 +49,7 @@ def on_send(node)
add_offense(node, message: format(MSG, count: count)) do |corrector|
next unless own_line?(node)

if count < 1
if never_process?(count, node)
remove_node(corrector, node)
elsif !proc_name.empty?
autocorrect_block_pass(corrector, node, proc_name)
Expand All @@ -61,6 +61,10 @@ def on_send(node)

private

def never_process?(count, node)
count < 1 || node.block_type? && node.body.nil?
end

def remove_node(corrector, node)
corrector.remove(range_by_whole_lines(node.loc.expression, include_final_newline: true))
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/lint/useless_times_spec.rb
Expand Up @@ -61,6 +61,21 @@
RUBY
end

it 'registers an offense and corrects when 1.times with empty block argument' do
expect_offense(<<~RUBY)
def foo
1.times do
^^^^^^^^^^ Useless call to `1.times` detected.
end
end
RUBY

expect_correction(<<~RUBY)
def foo
end
RUBY
end

it 'registers an offense and corrects when there is a blank line in the method definition' do
expect_offense(<<~RUBY)
def foo
Expand Down