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 #8950] Add IgnoredMethods and IgnoredClasses to Lint/NumberConversion #8956

Merged
merged 1 commit into from Oct 29, 2020

Conversation

dvandersluis
Copy link
Member

Fixes #8950, to an extent. There are going to inherently be a number of false positives possible from this cop since it applies to basically any receiver of to_i, etc. other than Time and DateTime, which were hardcoded. Since this cop is disabled by default we probably don't have a lot of external coverage here (searching github for Lint/NumberConversion returns a lot of inline disables though!)

Instead, the classes to ignore are now configurable, and IgnoredMethods was added as well (so the rails duration helpers could be allowed in configuration). This of course isn't going to help when assigned to a variable, because those are checked as well:

x = 10.minutes
x.to_i # will be registered

Another potential issue is that Integer(10.minutes) will work but Integer(10.minutes, 10), which is the default suggestion/correction, does not work when the first argument is not a string. Should I change that as well to only add the base if the first arg is a known string?


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.

@koic
Copy link
Member

koic commented Oct 28, 2020

I think this approach is fine. For #8950, default.yml of rubocop-rails (or .rubocop.yml of user) can the following settings:

Lint/NumberConversion:
  IgnoredMethods:
    - seconds
    - second
    - hours
    - hour
    (snip)

And the following methods can be added.
https://github.com/rails/rails/blob/v6.0.3.4/activesupport/lib/active_support/core_ext/numeric/time.rb

@@ -0,0 +1 @@
* [#8950](https://github.com/rubocop-hq/rubocop/pull/8950): Add `IgnoredMethods` and `IgnoredClasses` to `Lint/NumberConversion`. ([@dvandersluis][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [#8950](https://github.com/rubocop-hq/rubocop/pull/8950): Add `IgnoredMethods` and `IgnoredClasses` to `Lint/NumberConversion`. ([@dvandersluis][])
* [#8950](https://github.com/rubocop-hq/rubocop/issues/8950): Add `IgnoredMethods` and `IgnoredClasses` to `Lint/NumberConversion`. ([@dvandersluis][])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this but FYI this is coming from the rake changelog:* tasks. I assume it's done like this because it's impossible to tell whether the reference is an issue or PR, but github redirects pull/xxx to issues/xxx if it's actually an issue so I assume that's why @marcandre did it this way.

Copy link
Contributor

@marcandre marcandre Oct 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree it doesn't actually matter. Still, I'll change the script so it writes issues/nnn when there is [Fix #nnn] in the commit message, and pulls/x if there isn't (assuming that the link will point to the PR to be created). #8963

@dvandersluis
Copy link
Member Author

Oh nice great suggestion @koic, I’ll make a PR to rubocop-rails tomorrow!

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 28, 2020

It'd be nice if we updated the cop description with explanation of its limitations and why we had to disable it by default.

…/NumberConversion`.

- Updated the hardcoded classes to ignore to be configurable instead
@dvandersluis
Copy link
Member Author

@bbatsov added to the cop description. Should I make it not use a base with Integer() if not given a string?

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 29, 2020

I guess that would miss all cases where the first argument is not a literal, so probably we're better off not doing so, at least for now. Thanks for tackling this!

@bbatsov bbatsov merged commit 8dc26d7 into rubocop:master Oct 29, 2020
@dvandersluis dvandersluis deleted the issue/8950 branch January 18, 2021 20:42
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.

Lint/NumberConversion suggests 30.minutes.to_i be turned into Integer(30.minutes, 10), which is invalid
4 participants