Skip to content

Commit

Permalink
[Fix rubocop#4802] Detect all UnneededCopEnableDirective offenses
Browse files Browse the repository at this point in the history
After all other cops are finished we invoke the
Lint/UnneededCopDisableDirective cop, since it depends on the results of the
other cops. If it's an --auto-correct run, correcting
UnneededCopDisableDirective offenses can introduce new
UnneededCopEnableDirective offenses. For this reason we must do one more
inspection after UnneededCopDisableDirective has corrected a file.
  • Loading branch information
jonas054 authored and bbatsov committed Sep 4, 2019
1 parent b9d983f commit dbf2f90
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* [#7290](https://github.com/rubocop-hq/rubocop/issues/7290): Handle inner conditional inside `else` in `Style/ConditionalAssignment`. ([@jonas054][])
* [#5788](https://github.com/rubocop-hq/rubocop/issues/5788): Allow block arguments on separate lines if line would be too long in `Layout/MultilineBlockLayout`. ([@jonas054][])
* [#7305](https://github.com/rubocop-hq/rubocop/issues/7305): Register `Style/BlockDelimiters` offense when block result is assigned to an attribute. ([@mvz][])
* [#4802](https://github.com/rubocop-hq/rubocop/issues/4802): Don't leave any `Lint/UnneededCopEnableDirective` offenses undetected/uncorrected. ([@jonas054][])

### Changes

Expand Down
22 changes: 14 additions & 8 deletions lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def add_unneeded_disables(file, offenses, source)
if cop.relevant_file?(file)
cop.check(offenses, source.disabled_line_ranges, source.comments)
offenses += cop.offenses
autocorrect_unneeded_disables(source, cop)
offenses += autocorrect_unneeded_disables(file, source, cop,
offenses)
end
end
offenses
end

offenses.sort.reject(&:disabled?).freeze
Expand All @@ -172,14 +172,20 @@ def filtered_run?
@options[:except] || @options[:only]
end

def autocorrect_unneeded_disables(source, cop)
def autocorrect_unneeded_disables(file, source, cop, offenses)
cop.processed_source = source

Cop::Team.new(
RuboCop::Cop::Registry.new,
nil,
@options
).autocorrect(source.buffer, [cop])
team = Cop::Team.new(RuboCop::Cop::Registry.new, nil, @options)
team.autocorrect(source.buffer, [cop])

return [] unless team.updated_source_file?

# Do one extra inspection loop if any unneeded disables were
# removed. This is done in order to find rubocop:enable directives that
# have now become useless.
_source, new_offenses = do_inspection_loop(file,
get_processed_source(file))
new_offenses - offenses
end

def file_started(file)
Expand Down
10 changes: 7 additions & 3 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ def func
class A
# rubocop:disable Metrics/MethodLength
def func
# rubocop:enable Metrics/MethodLength
x = foo # rubocop:disable Lint/UselessAssignment,Style/For
# rubocop:disable all
# rubocop:disable Style/ClassVars
Expand All @@ -768,10 +769,13 @@ def func
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
C: 3: 1: Style/Documentation: Missing top-level class documentation comment.
W: 4: 3: [Corrected] Lint/UnneededCopDisableDirective: Unnecessary disabling of Metrics/MethodLength.
W: 6: 54: [Corrected] Lint/UnneededCopDisableDirective: Unnecessary disabling of Style/For.
W: 8: 5: [Corrected] Lint/UnneededCopDisableDirective: Unnecessary disabling of Style/ClassVars.
C: 5: 1: [Corrected] Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body beginning.
C: 5: 1: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
W: 5: 22: [Corrected] Lint/UnneededCopEnableDirective: Unnecessary enabling of Metrics/MethodLength.
W: 7: 54: [Corrected] Lint/UnneededCopDisableDirective: Unnecessary disabling of Style/For.
W: 9: 5: [Corrected] Lint/UnneededCopDisableDirective: Unnecessary disabling of Style/ClassVars.
1 file inspected, 6 offenses detected, 5 offenses corrected
1 file inspected, 9 offenses detected, 8 offenses corrected
RESULT
corrected = <<~RUBY
# frozen_string_literal: true
Expand Down

0 comments on commit dbf2f90

Please sign in to comment.