Skip to content

Commit 24765a0

Browse files
marcandrebbatsov
authored andcommitted
[Fix #9175] Fix status for offenses that are not correctable.
This addresses a few things: 1) Calling `add_offense` or `add_offense { |corrector| # don't auto-correct }` should be handled the same way. An empty corrector is now replaced by `nil`. 2) Tweaks how the status is handled. Status `:uncorrected` is reserved for cases where there is a corrector but it is not applied. 3) Old style cops' `auto_correct` method is now called even when run without auto-correction. This way we can know if they can auto-correct or not. It also simplifies logic. I should have done 3) during my refactor (I don't see how it could be incompatible)
1 parent 0146f4f commit 24765a0

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

lib/rubocop/cop/base.rb

+17-15
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,6 @@ def apply_correction(corrector)
288288
@current_corrector&.merge!(corrector) if corrector
289289
end
290290

291-
def correction_strategy
292-
return :unsupported unless correctable?
293-
return :uncorrected unless autocorrect?
294-
295-
:attempt_correction
296-
end
297-
298291
### Reserved for Commissioner:
299292

300293
def current_offense_locations
@@ -341,33 +334,42 @@ def reset_investigation
341334

342335
# @return [Symbol, Corrector] offense status
343336
def correct(range)
344-
status = correction_strategy
345-
346337
if block_given?
347338
corrector = Corrector.new(self)
348339
yield corrector
349-
if !corrector.empty? && !self.class.support_autocorrect?
340+
if corrector.empty?
341+
corrector = nil
342+
elsif !self.class.support_autocorrect?
350343
raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
351344
end
352345
end
353346

354-
status = attempt_correction(range, corrector) if status == :attempt_correction
347+
[use_corrector(range, corrector), corrector]
348+
end
355349

356-
[status, corrector]
350+
# @return [Symbol] offense status
351+
def use_corrector(range, corrector)
352+
if autocorrect?
353+
attempt_correction(range, corrector)
354+
elsif corrector
355+
:uncorrected
356+
else
357+
:unsupported
358+
end
357359
end
358360

359361
# @return [Symbol] offense status
360362
def attempt_correction(range, corrector)
361-
if corrector && !corrector.empty?
363+
if corrector
362364
status = :corrected
363365
elsif disable_uncorrectable?
364366
corrector = disable_uncorrectable(range)
365367
status = :corrected_with_todo
366368
else
367-
return :uncorrected
369+
return :unsupported
368370
end
369371

370-
apply_correction(corrector) if corrector
372+
apply_correction(corrector)
371373
status
372374
end
373375

lib/rubocop/cop/cop.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def call(corrector)
2727
def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
2828
@v0_argument = node_or_range
2929
range = find_location(node_or_range, location)
30-
if block.nil? && !autocorrect?
30+
if block.nil? && !support_autocorrect?
3131
super(range, message: message, severity: severity)
3232
else
3333
super(range, message: message, severity: severity) do |corrector|
@@ -136,7 +136,7 @@ def emulate_v0_callsequence(corrector)
136136
end
137137

138138
def correction_lambda
139-
return unless correction_strategy == :attempt_correction && support_autocorrect?
139+
return unless support_autocorrect?
140140

141141
dedup_on_node(@v0_argument) do
142142
autocorrect(@v0_argument)

spec/rubocop/cli/cli_autocorrect_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ def self.some_method(foo, bar: 1)
14101410
C: 2: 34: [Correctable] Style/Semicolon: Do not use semicolons to terminate expressions.
14111411
W: 3: 27: [Correctable] Lint/UnusedMethodArgument: Unused method argument - bar.
14121412
1413-
1 file inspected, 3 offenses detected, 3 more offenses can be corrected with `rubocop -A`
1413+
1 file inspected, 3 offenses detected
14141414
RESULT
14151415
end
14161416

0 commit comments

Comments
 (0)