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 #7842] Add AcceptImplicitNamespaces to Lint/RaiseException #7846

Conversation

koic
Copy link
Member

@koic koic commented Apr 3, 2020

Fixes #7842.

This PR adds AcceptImplicitNamespaces option to Lint/RaiseException.

Users can specify and accept an implicit namespace as follows:

Lint/RaiseException:
  AllowImplicitNamespaces:
    - 'Gem'
# good
module Gem
  def self.foo
    raise Exception
  end
end

Gem.foo #=> Gem::Exception

Gem is specified by default.
https://ruby-doc.org/stdlib-2.7.0/libdoc/rubygems/rdoc/Gem/Exception.html

This way does not prevent all false positives, but it expect some cases can be solved.


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 koic mentioned this pull request Apr 3, 2020
20 tasks
@koic koic force-pushed the add_allow_implicit_namespaces_to_lint_raise_exception branch from b7ba87b to 26c1149 Compare April 3, 2020 18:34
@@ -1567,6 +1567,8 @@ Lint/RaiseException:
StyleGuide: '#raise-exception'
Enabled: pending
VersionAdded: '0.81'
AllowImplicitNamespaces:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be named "AllowedImplicitNamespaces".

@@ -5,33 +5,64 @@ module Cop
module Lint
# This cop checks for `raise` or `fail` statements which are
# raising `Exception` class.
# You can specify a module name that will be an implicit namespace
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably the explanation should be extended to mention the cop normally ignores namespaced "Exception", but might cause false positives if some module has its own Exception class. This would make it easier for people to understand the need for the extra config. That being said - I think it's a very bad idea to name base classes "Exception" and I'd rather name them "(BaseError)" or something along those lines. Hiding built-in constants is a recipe for trouble IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for reviewing. I extended the explanation.

@koic koic force-pushed the add_allow_implicit_namespaces_to_lint_raise_exception branch from 26c1149 to 5bf74df Compare April 4, 2020 11:42
…tion`

Fixes rubocop#7842.

This PR adds `AcceptImplicitNamespaces` option to `Lint/RaiseException`.

Users can specify and accept an implicit namespace as follows:

```yaml
Lint/RaiseException:
  AllowImplicitNamespaces:
    - 'Gem'
```

```ruby
# good
module Gem
  def self.foo
    raise Exception
  end
end

Gem.foo #=> Gem::Exception
```

`Gem` is specified by default.
https://ruby-doc.org/stdlib-2.7.0/libdoc/rubygems/rdoc/Gem/Exception.html

This way does not prevent all false positives, but it expect some cases
can be solved.
@koic koic force-pushed the add_allow_implicit_namespaces_to_lint_raise_exception branch from 5bf74df to 59e0823 Compare April 4, 2020 15:34
@bbatsov bbatsov merged commit 42ac781 into rubocop:master Apr 13, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 13, 2020

Looks good!

@koic koic deleted the add_allow_implicit_namespaces_to_lint_raise_exception branch April 13, 2020 12:54
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/RaiseException not aware of namespaced "Exception" classes
2 participants