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

Lint/ConstantResolution and Style/RedundantConstantBase do not work well together #11266

Closed
coding-bunny opened this issue Dec 12, 2022 · 1 comment · Fixed by #11270
Closed
Labels

Comments

@coding-bunny
Copy link

Expected behavior

I expect that these 2 Cops would be able to work together and have the ability to distinguish between external gems and project code. My suggestion is that:

  • Lint/ConstantResolution focusses on project code, and ignores gems loaded through the Gemfile
  • Style/RedundantConstantBase focusses on external gems and doesn't check project references.

Actual behavior

When both are enabled, you end up in an infinite chicken/egg problem because both cops do not agree on the correct constant resolution in the source code.

Steps to reproduce the problem

Use the following code snippet:

::RSpec.configure do |config|
  # Enable flags like --only-failures and --next-failure
  config.example_status_persistence_file_path = '.rspec_status'

  # Disable RSpec exposing methods globally on `Module` and `main`
  config.disable_monkey_patching!

  config.expect_with(:rspec) do |c|
    c.syntax = :expect
  end
end

This triggers the following output from RuboCop

spec/spec_helper.rb:12:1: E: [Correctable] Style/RedundantConstantBase: Remove redundant ::.
::RSpec.configure do |config|
^^

But if you apply the change and remove the leading ::, the following is outputted instead:

spec/spec_helper.rb:12:1: E: Lint/ConstantResolution: Fully qualify this constant to avoid possibly ambiguous resolution.
RSpec.configure do |config|
^^^^^

This means it's impossible to find a correct code approach to resolve this unless one of the Cops is disabled.

RuboCop version

$ [bundle exec] rubocop -V
1.40.0 (using Parser 3.1.3.0, rubocop-ast 1.24.0, running on ruby 2.7.6) [x86_64-darwin21]
  - rubocop-performance 1.15.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.15.0
  - rubocop-thread_safety 0.4.4
@mobilutz
Copy link

Thanks @coding-bunny for creating this issue, I planned to do that same today but you beat me to it 😉

@koic koic added the bug label Dec 12, 2022
koic added a commit to koic/rubocop that referenced this issue Dec 14, 2022
…Base`

Fixes rubocop#11266.

This PR fixes a false positive for `Style/RedundantConstantBase`
when enabling `Lint/ConstantResolution`.

It respects `Lint/ConstantResolution` when enabling `Lint/ConstantResolution`.
OTOH, it respects `Style/RedundantConstantBase` when disabling `Lint/ConstantResolution`.

This is to honor the desire to enable the Lint cop which is disabled by default.
bbatsov pushed a commit that referenced this issue Dec 14, 2022
Fixes #11266.

This PR fixes a false positive for `Style/RedundantConstantBase`
when enabling `Lint/ConstantResolution`.

It respects `Lint/ConstantResolution` when enabling `Lint/ConstantResolution`.
OTOH, it respects `Style/RedundantConstantBase` when disabling `Lint/ConstantResolution`.

This is to honor the desire to enable the Lint cop which is disabled by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants