Skip to content

Commit

Permalink
[Fix rubocop#11204] Fix a false negative for `Lint/RedundantCopDisabl…
Browse files Browse the repository at this point in the history
…eDirective`

Fixes rubocop#11204.

This PR fixes a false negative for `Lint/RedundantCopDisableDirective`
when using `--except` command line option.
  • Loading branch information
koic committed Dec 22, 2022
1 parent 154031e commit 11ade37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
@@ -0,0 +1 @@
* [#11204](https://github.com/rubocop/rubocop/issues/11204): Fix a false negative for `Lint/RedundantCopDisableDirective` when using `--except` command line option. ([@koic][])
13 changes: 10 additions & 3 deletions lib/rubocop/runner.rb
Expand Up @@ -27,6 +27,11 @@ def initialize(path, offenses_by_iteration, loop_start: -1)
# @api private
MAX_ITERATIONS = 200

# @api private
REDUNDANT_COP_DISABLE_DIRECTIVE_RULES = %w[
Lint/RedundantCopDisableDirective RedundantCopDisableDirective Lint
].freeze

attr_reader :errors, :warnings
attr_writer :aborting

Expand Down Expand Up @@ -194,7 +199,9 @@ def team_for_redundant_disables(file, offenses, source)
end

def check_for_redundant_disables?(source)
!source.disabled_line_ranges.empty? && !filtered_run?
return false if source.disabled_line_ranges.empty? || except_redundant_cop_disable_directive?

!@options[:only]
end

def redundant_cop_disable_directive(file)
Expand All @@ -205,8 +212,8 @@ def redundant_cop_disable_directive(file)
yield cop if cop.relevant_file?(file)
end

def filtered_run?
@options[:except] || @options[:only]
def except_redundant_cop_disable_directive?
@options[:except] && (@options[:except] & REDUNDANT_COP_DISABLE_DIRECTIVE_RULES).any?
end

def file_started(file)
Expand Down
29 changes: 26 additions & 3 deletions spec/rubocop/cli/options_spec.rb
Expand Up @@ -916,27 +916,50 @@ def on_send(node)
end
end

context 'when one cop plus one namespace are given' do
context 'when two cop plus one namespace are given' do
it 'runs all cops except the given' do
# The disable comment should not be reported as unnecessary (even if
# it is) since --except overrides configuration.
create_file('example.rb', ['# rubocop:disable LineLength', 'if x== 0 ', "\ty = 3", 'end'])
expect(cli.run(['--format', 'offenses',
'--except', 'Style/IfUnlessModifier,Lint',
'--except', 'Style/IfUnlessModifier,Lint/UselessAssignment,Layout',
'example.rb'])).to eq(1)
# NOTE: No Lint/UselessAssignment offense.
expect($stdout.string)
.to eq(<<~RESULT)
1 Lint/MissingCopEnableDirective
1 Lint/RedundantCopDisableDirective
1 Migration/DepartmentName
1 Style/FrozenStringLiteralComment
1 Style/NumericPredicate
--
5 Total in 1 files
RESULT
end
end

context 'when one cop plus `Lint/RedundantCopDisableDirective` are given' do
it 'runs all cops except the given' do
create_file('example.rb', ['# rubocop:disable LineLength', 'if x== 0 ', "\ty = 3", 'end'])
expect(cli.run(['--format', 'offenses',
'--except', 'Style/IfUnlessModifier,Lint/RedundantCopDisableDirective',
'example.rb'])).to eq(1)
expect($stdout.string)
.to eq(<<~RESULT)
1 Layout/IndentationStyle
1 Layout/IndentationWidth
1 Layout/SpaceAroundOperators
1 Layout/TrailingWhitespace
1 Lint/MissingCopEnableDirective
1 Lint/UselessAssignment
1 Migration/DepartmentName
1 Style/FrozenStringLiteralComment
1 Style/NumericPredicate
--
7 Total in 1 files
9 Total in 1 files
RESULT
end
Expand Down

0 comments on commit 11ade37

Please sign in to comment.