-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Confusing behavior when using inline comment to disable Lint/EmptyBlock #8971
Labels
Comments
8 tasks
koic
added a commit
to koic/rubocop
that referenced
this issue
Oct 30, 2020
…yBlock` Fixes rubocop#8971. This PR fixes the following false alarm for `# rubocop:disable Lint/EmptyBlock` inline comment with `Lint/RedundantCopDisableDirective`. ```ruby % cat example.rb # frozen_string_literal: true assert_equal nil, combinator {}.call # rubocop:disable Lint/EmptyBlock % bundle exec rubocop example.rb Inspecting 1 file W Offenses: example.rb:3:38: W: Lint/RedundantCopDisableDirective: Unnecessary disabling of Lint/EmptyBlock. assert_equal nil, combinator {}.call # rubocop:disable Lint/EmptyBlock ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense auto-correctabl ``` `Lint/EmptyBlock` is `AllowComments: true` by default. `Lint/RedundantCopDisableDirective` was occurring because `# rubocop:disable Lint/EmptyBlock` inline comment was treated as an allowed comment by `Lint/EmptyBlock`. This PR does not treat `# rubocop:disable` as a comment allowed by `AllowComment`.
bbatsov
pushed a commit
that referenced
this issue
Oct 30, 2020
Fixes #8971. This PR fixes the following false alarm for `# rubocop:disable Lint/EmptyBlock` inline comment with `Lint/RedundantCopDisableDirective`. ```ruby % cat example.rb # frozen_string_literal: true assert_equal nil, combinator {}.call # rubocop:disable Lint/EmptyBlock % bundle exec rubocop example.rb Inspecting 1 file W Offenses: example.rb:3:38: W: Lint/RedundantCopDisableDirective: Unnecessary disabling of Lint/EmptyBlock. assert_equal nil, combinator {}.call # rubocop:disable Lint/EmptyBlock ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense auto-correctabl ``` `Lint/EmptyBlock` is `AllowComments: true` by default. `Lint/RedundantCopDisableDirective` was occurring because `# rubocop:disable Lint/EmptyBlock` inline comment was treated as an allowed comment by `Lint/EmptyBlock`. This PR does not treat `# rubocop:disable` as a comment allowed by `AllowComment`.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the following test in one of my projects:
This triggers Lint/EmptyBlock. If I try to disable this using the regular method of disabling cops in code, the result is surprising. I use a comment like so:
Expected behavior
The offense goes away and there is are no more offenses on this line
Actual behavior
RuboCop complains that disabling the cop is not necessary:
Analysis
I think what happens is that Lint/EmptyBlock does not register an offense if there is some comment associated with the empty block. So the mere presence of the comment already causes the offense to go away. The RedundantCopDisableDirective cop then kicks in because no EmptyBlock offense is found anymore.
Steps to reproduce the problem
Create a file with the code above and run RuboCop 1.1.0 on it.
RuboCop version
The text was updated successfully, but these errors were encountered: