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

Conversation

koic
Copy link
Member

@koic koic commented Sep 16, 2020

This PR fixes the following error for Lint/UselessTimes when using empty block argument.

% 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'

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@dvandersluis
Copy link
Member

dvandersluis commented Sep 16, 2020

Maybe instead of updating autocorrect_block, the condition can be updated here to include when the node has an empty body: https://github.com/rubocop-hq/rubocop/blob/354da8b53a41183948f6a6c09053ffee937b7d5e/lib/rubocop/cop/lint/useless_times.rb#L52-L53

Then the remove node logic won't have to be duplicated.

@koic
Copy link
Member Author

koic commented Sep 16, 2020

Sure! I updated this PR. Thank you for your suggestion!

@koic koic force-pushed the fix_an_error_for_lint_useless_times branch from 354da8b to ded829b Compare September 16, 2020 18:10
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'
```
@koic koic force-pushed the fix_an_error_for_lint_useless_times branch from ded829b to 8e8a320 Compare September 17, 2020 01:32
@bbatsov bbatsov merged commit 001ddc1 into rubocop:master Sep 17, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 17, 2020

Thanks!

@koic koic deleted the fix_an_error_for_lint_useless_times branch September 17, 2020 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants