Skip to content

Commit

Permalink
Fix an error for Lint/UselessTimes
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Lint/UselessTimes`
when using empty block argument.

```console
% cat example.rb
def foo
  1.times do
  end
end

% rubocop --only Lint/UselessTimes -d
(snip)

Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/bar/example.rb
An error occurred while Lint/UselessTimes cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/bar/example.rb:2:2.
undefined method `source' for nil:NilClass
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.91.0/
lib/rubocop/cop/lint/useless_times.rb:76:in `autocorrect_block'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.91.0/
lib/rubocop/cop/lint/useless_times.rb:57:in `block in on_send'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.91.0/
lib/rubocop/cop/base.rb:333:in `correct'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.91.0/
lib/rubocop/cop/base.rb:126:in `add_offense'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.91.0/
lib/rubocop/cop/lint/useless_times.rb:49:in `on_send'
```
  • Loading branch information
koic committed Sep 17, 2020
1 parent f1ed705 commit 8e8a320
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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

0 comments on commit 8e8a320

Please sign in to comment.