diff --git a/changelog/fix_a_false_negative_for_lint_redundant_cop_disable_directive.md b/changelog/fix_a_false_negative_for_lint_redundant_cop_disable_directive.md new file mode 100644 index 00000000000..ed7ae581a88 --- /dev/null +++ b/changelog/fix_a_false_negative_for_lint_redundant_cop_disable_directive.md @@ -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][]) diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index 24bd4cf36ce..be69b220df3 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -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 @@ -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) @@ -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) diff --git a/spec/rubocop/cli/options_spec.rb b/spec/rubocop/cli/options_spec.rb index a9c0840584d..1dfc282ea50 100644 --- a/spec/rubocop/cli/options_spec.rb +++ b/spec/rubocop/cli/options_spec.rb @@ -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