Skip to content

Commit

Permalink
Merge pull request #10420 from koic/fix_an_error_for_lint_useless_times
Browse files Browse the repository at this point in the history
[Fix #10415] Fix an error for `Lint/UselessTimes`
  • Loading branch information
koic committed Feb 16, 2022
2 parents 02fba0d + cabb4c2 commit 9f82e0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_useless_times.md
@@ -0,0 +1 @@
* [#10415](https://github.com/rubocop/rubocop/issues/10415): Fix an error for `Lint/UselessTimes` when using `1.times` with method chain. ([@koic][])
22 changes: 13 additions & 9 deletions lib/rubocop/cop/lint/useless_times.rb
Expand Up @@ -51,20 +51,24 @@ def on_send(node)
node = node.block_node if node.block_literal?

add_offense(node, message: format(MSG, count: count)) do |corrector|
next unless own_line?(node)

if never_process?(count, node)
remove_node(corrector, node)
elsif !proc_name.empty?
autocorrect_block_pass(corrector, node, proc_name)
else
autocorrect_block(corrector, node)
end
next if !own_line?(node) || node.parent&.send_type?

autocorrect(corrector, count, node, proc_name)
end
end

private

def autocorrect(corrector, count, node, proc_name)
if never_process?(count, node)
remove_node(corrector, node)
elsif !proc_name.empty?
autocorrect_block_pass(corrector, node, proc_name)
else
autocorrect_block(corrector, node)
end
end

def never_process?(count, node)
count < 1 || (node.block_type? && node.body.nil?)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/useless_times_spec.rb
Expand Up @@ -59,6 +59,17 @@
RUBY
end

it 'registers an offense and corrects with 1.times with method chain' do
expect_offense(<<~RUBY)
1.times.reverse_each do
^^^^^^^ Useless call to `1.times` detected.
foo
end
RUBY

expect_no_corrections
end

it 'registers an offense and corrects when 1.times with empty block argument' do
expect_offense(<<~RUBY)
def foo
Expand Down

0 comments on commit 9f82e0f

Please sign in to comment.