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 a false positive for Lint/RedundantRequireStatement #11931

Conversation

koic
Copy link
Member

@koic koic commented Jun 6, 2023

Follow up #11099 (comment)

This PR fixes a false positive for Lint/RedundantRequireStatement when using PP.pp.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

Follow up rubocop#11099 (comment)

This PR fixes a false positive for `Lint/RedundantRequireStatement`
when using `PP.pp`.
@bobf
Copy link

bobf commented Jun 6, 2023

@koic Tried your branch on my project, works when require 'pp' is in the same file as PP.pp usage, but in my case I require all dependencies for my gem in the gem's root (i.e. lib/my_gem.rb, and PP.pp is called in lib/my_gem/my_module/my_class.rb).

Is this intended behaviour ? I prefer to keep all require statements in the same place when building a gem, since all namespace will be impacted regardless of where the require call happens.

Edit: Real world examples if that's helpful:
https://github.com/bobf/rspec-documentation/blob/main/lib/rspec_documentation.rb#L20
https://github.com/bobf/rspec-documentation/blob/main/lib/rspec_documentation/formatters/ruby.rb#L20

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 12, 2023

@bobf Hmm, that's a bit tricky for that particular cop. Perhaps it's just best to stick to the disable and we can ignore this PR together as it only adds complexity for little added value. I'd suggest enhancing the cop documentation to highlight better its limitations instead.

@koic Any thoughts?

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 19, 2023

I'm leaning towards just ignoring the additional problem, as it's outside of RuboCop's scope. I'm wondering if we should just ignore the special handing for PP.pp altogether.

@bobf
Copy link

bobf commented Jun 19, 2023

@bbatsov It's no bother for me to add a rubocop:disable directive on this one use case, it's only the second time I've ever deliberately required pp so I can definitely live with this being an exceptional case. If anything it clarifies that I'm doing it deliberately so somebody reading the code might assume that I'm doing something slightly unusual.

@koic
Copy link
Member Author

koic commented Jun 21, 2023

For now, the implementation of this PR appears simple and seems capable of preventing false positives for PP.pp within the same file.

I'd suggest enhancing the cop documentation to highlight better its limitations instead.

Yes, I can certainly enhance the documentation. However, I'm wondering if it would be better to set AutoCorrect: false along with mentioning it in the documentation.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 21, 2023

Yes, I can certainly enhance the documentation. However, I'm wondering if it would be better to set AutoCorrect: false along with mentioning it in the documentation.

Not a bad idea.

@bbatsov bbatsov merged commit 04ea207 into rubocop:master Jun 23, 2023
28 checks passed
@koic koic deleted the fix_a_false_positive_for_lint_redundant_require_statement branch June 23, 2023 05:55
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 23, 2023

I'll merge this in its current state, as I'm about the cut a release. The rest of the stuff we discussed can happen later.

koic added a commit to koic/rubocop that referenced this pull request Oct 8, 2023
Follow up rubocop#11931 (comment).

This PR marks `Lint/RedundantRequireStatement` as unsafe autocorrect because if `require 'pp'` is
removed from one file, `NameError` can be encountered when another file uses `PP.pp`.
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