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

False positive for Rails/HelperInstanceVariable when using block-level disabling of Rails department #1365

Open
pdobb opened this issue Sep 18, 2024 · 2 comments

Comments

@pdobb
Copy link

pdobb commented Sep 18, 2024

The presence of a block-level disabling of the entire Rails department causes any instance variables in same file to report:
Rails/HelperInstanceVariable: Do not use instance variables in helpers.

@ivar = 1  # <-- Invalidly reports: Rails/HelperInstanceVariable error

# rubocop:disable Rails
1
# rubocop:enable Rails

Expected behavior

I expect block-level disabling of the entire Rails department should be valid.

Actual behavior

Invalid reporting of an offense from Rails/HelperInstanceVariable cop.

rubocop -d rubocop.rb
For /Users/paul/dev/minesweeper_alliance: configuration from /Users/paul/dev/minesweeper_alliance/.rubocop.yml
configuration from /Users/paul/.gem/ruby/3.3.4/gems/rubocop-rails-2.26.1/config/default.yml
configuration from /Users/paul/.gem/ruby/3.3.4/gems/rubocop-rails-2.26.1/config/default.yml
Default configuration from /Users/paul/.gem/ruby/3.3.4/gems/rubocop-1.66.1/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/paul/dev/minesweeper_alliance/rubocop.rb
W

Offenses:

rubocop.rb:3:1: C: Rails/HelperInstanceVariable: Do not use instance variables in helpers.
@ivar = 1
^^^^^
rubocop.rb:5:1: W: [Correctable] Lint/RedundantCopDisableDirective: Unnecessary disabling of Rails department.
# rubocop:disable Rails
^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 1 offense autocorrectable
Finished in 0.2639049999997951 seconds

Steps to reproduce the problem

Simple example, though it is quite contrived. I put the following into a file and then disabled all but rubocop-rails and removed all of my config.

@ivar = 1  # <-- Invalidly reports: Rails/HelperInstanceVariable error

# rubocop:disable Rails
1
# rubocop:enable Rails

Here are a couple of ways that work to avoid the Rails/HelperInstanceVariable offense:

# Works fine if you pick any single cop to disable instead:
@ivar = 1

# rubocop:disable Rails/WhereRange
1
# rubocop:enable Rails/WhereRange
# Works fine if you disable just the one line, instead of a block
@ivar = 1

1 # rubocop:disable Rails

RuboCop version

$ rubocop -V
1.66.1 (using Parser 3.3.5.0, rubocop-ast 1.32.3, running on ruby 3.3.4) [arm64-darwin23]
  - rubocop-rails 2.26.1
@pdobb pdobb changed the title Block-level disabling of Rails department causes Rails/HelperInstanceVariable offense on ivars False positive for Rails/HelperInstanceVariable when using block-level disabling of Rails department Sep 18, 2024
@pdobb
Copy link
Author

pdobb commented Sep 18, 2024

Similar to this old issue: #1161

@Earlopain
Copy link
Contributor

Earlopain commented Sep 18, 2024

Old message, disregard

It seems like the department-level disable causes a cop to forget it's only supposed to look at a subset of files. This is reproducable with plain RuboCop and the following:

# .rubocop.yml
AllCops:
  DisabledByDefault: true
  Exclude:
    - test.rb

Style/HashSyntax:
  Enabled: true
# test.rb
{ :a => :b }

# rubocop:disable Style
{ :a => :b }
# rubocop:enable Style

The same also applies to rubocop:disable all

Where it then reports a offense for the first hash. I also notice the following warning which is unexpected:

An AutocorrectNotice must be defined in your RuboCop config (from file: /home/user/code/test/test.rb)

From Style/Copyright.

Above I was running rubocop while passing in the filename which changes behaviour. I can still reproduce the original issue but the original message was nonsense.

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

No branches or pull requests

2 participants