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

Add new cop Lint/UselessTimes #8702

Merged
merged 1 commit into from Sep 12, 2020

Conversation

dvandersluis
Copy link
Member

@dvandersluis dvandersluis commented Sep 11, 2020

This cop looks for pointless uses of Integer#times and replaces or removes them as necessary (I found some of these in my codebase at work, sadly). When the integer is <= 0, the block will never be yielded to, so the entire thing can be removed. When the integer is 1, the block will only ever yield once, so the 1.times call can (generally) be replaced with the block body.

Auto-correction is enabled in most cases, but because Integer#times returns its receiver (and the replacement won't), I've marked it as unsafe.

The cop will remove 0.times and -5.times (etc.) calls, since they will never do anything. The cop will replace 1.times with its block, and if the block arg is used at all, replace it with 1.

Auto-correction will not be attempted in a couple more complex cases, such as mutating the block arg within the block, or calling Integer#times not on its own line. See the tests for more details.


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.

This cop looks for pointless uses of `Integer#times` and replaces or removes them as necessary. When the integer is <= 0, the block will never be yielded to, so the entire thing can be removed. When the integer is 1, the block will only ever yield once, so the `1.times` call can be replaced with the block body.
@dvandersluis dvandersluis changed the title Add new cop Lint/UselessTimes. Add new cop Lint/UselessTimes Sep 11, 2020
@bbatsov bbatsov merged commit fcb8c83 into rubocop:master Sep 12, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 12, 2020

Great work!

jmkoni pushed a commit to standardrb/standard that referenced this pull request Sep 16, 2020
…en fixed

* Update Rubocop from
  [0.90](https://github.com/rubocop-hq/rubocop/releases/tag/v0.90.0)
  to
  [0.91](https://github.com/rubocop-hq/rubocop/releases/tag/v0.91.0),
  enabling:
  * [`Lint/UselessTimes`](rubocop/rubocop#8702)
  * [`Layout/BeginEndAlignment`](rubocop/rubocop#8628)
  * [`Lint/ConstantDefinitionInBlock`](rubocop/rubocop#8707)
  * [`Lint/IdentityComparison`](rubocop/rubocop#8699)
  re-enabling after bug fixes:
  * [`Bundler/DuplicatedGem`](rubocop/rubocop#8666)
  * [`Naming/BinaryOperatorParameterName`](rubocop/rubocop#8664)
@dvandersluis dvandersluis deleted the lint/useless-times branch January 18, 2021 20:34
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

2 participants