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/UnneededDisable reports a warning if rubocop:disable all is used inside a block with already disabled cop #3580

Closed
ojab opened this issue Oct 7, 2016 · 5 comments

Comments

@ojab
Copy link
Contributor

ojab commented Oct 7, 2016

If # rubocop:disable all is used inside of code block with some cops already disabled, Lint/UselessAssignment reports a warning. For example for code:

# rubocop:disable Style/ParallelAssignment
# rubocop:disable all
x, y = 1, 2

Actual behavior

$ rubocop /tmp/1.rb
Inspecting 1 file
W

Offenses:

/tmp/1.rb:1:1: W: Lint/UnneededDisable: Unnecessary disabling of Style/ParallelAssignment.
# rubocop:disable Style/ParallelAssignment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/tmp/1.rb:2:1: W: Lint/UnneededDisable: Unnecessary disabling of Style/ParallelAssignment.
# rubocop:disable all
^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

Expected behavior

No warnings reported, # rubocop:disable all is used to disable all remaining cops, not all initially enabled cops.

RuboCop version

$ rubocop -V
0.43.0 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-darwin16)
@the-bass
Copy link

the-bass commented Oct 8, 2016

It's not the Lint/UselessAssignment cop that reports the warning but Lint/UnneededDisable, which, according to the comments in the code, was just build for this very case. Excerpt:

# This cop detects instances of rubocop:disable comments that can be
# removed without causing any offenses to be reported.

So I guess this is the expected behavior. You can disable the cop by simply adding

Lint/UnneededDisable:
  Enabled: false

to your .rubocop.yml.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 8, 2016

Damn, I didn't really read the offense output. :-) Not a bug indeed.

@bbatsov bbatsov closed this as completed Oct 8, 2016
@ojab ojab changed the title Lint/UselessAssignmentreports a warning if rubocop:disable all is used inside a block with already disabled cop Lint/UnneededDisable reports a warning if rubocop:disable all is used inside a block with already disabled cop Oct 8, 2016
@ojab
Copy link
Contributor Author

ojab commented Oct 8, 2016

Ouch, yeah, subject mentioned a wrong cop.

I still think that there is an issue: right now there is no way to disable all cops in situations like:

class Whatever
  # So so code
  # rubocop:disable Cop1,Cop2,Cop3
  def method_one
  end

  def method_two
  end

  # Code is total garbage
  # rubocop:disable all
  def method_three
  end
end

which is quite useful during cleanups (i. e. we do not want to introduce new offenses in the first part and don't care about second part because no one has touched it yet).

AFAIU right now we can:
a) Disable Lint/UnneededDisable via magic comment
b) Enable all disabled cops before rubocop:disable all
c) List all cops one by one instead of all
Less than elegant, I'd say.

As I've said in the first comment, I expect # rubocop:disable all to disable all remaining cops, not all initially enabled cops.

And, even if my reading of # rubocop:disable all expected outcome is wrong, Lint/UnneededDisable should not emit two warnings: one of the magic comments is in fact necessary to disable Cop.

@jonas054
Copy link
Collaborator

Absolutely. Lint/UnneededDisable should only report Unnecessary disabling of all cops for rubocop:disable all when it's not needed. Saying Unnecessary disabling of Style/ParallelAssignment for that line makes no sense. Reopening.

@jonas054 jonas054 reopened this Nov 13, 2016
@the-bass
Copy link

the-bass commented Mar 3, 2017

The cop Lint/UnneededDisable ...

...detects instances of rubocop:disable comments that can be removed without causing any offenses to be reported.

(see https://github.com/bbatsov/rubocop/blob/ec3123fc3454b080e1100e35480c6466d1240fff/lib/rubocop/cop/lint/unneeded_disable.rb#L6)

This is clearly the case in a file with

# rubocop:disable Style/ParallelAssignment
# rubocop:disable all
x, y = 1, 2

So

Expected behavior: No warnings reported

should not be the expected behavior. If this cop wouldn't detect an offense here, I'd call it a bug.

jonas054 added a commit to jonas054/rubocop that referenced this issue Mar 17, 2017
Add proper handling of two scenarios. The first is that a
`rubocop:disable all` comment precedes a comment disabling
a specific cop. The second is the other way around.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants