Skip to content

Commit

Permalink
Allow autocorrect with display-only-fail-level-offenses
Browse files Browse the repository at this point in the history
The `--display-only-fail-level-offenses` option uses
`considered_failure?` to determine what to print. The same method is
used to determine the exit code of `rubocop`, so the two options should
be entirely safe to use together.

This change is useful when using rubocop as part of a pre-commit hook.
When the hook fails becuase rubocop exited with a non-zero status, we
can use `--display-only-fail-level-offenses` to avoid having the output
cluttered up with non-blocking offenses.
  • Loading branch information
naveg authored and bbatsov committed Dec 22, 2023
1 parent 74abd36 commit 685a7f8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog/change_allow_autocorrect_with.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12535](https://github.com/rubocop/rubocop/pull/12535): Allow --autocorrect with --display-only-fail-level-offenses. ([@naveg][])
8 changes: 0 additions & 8 deletions lib/rubocop/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,6 @@ def validate_compatibility # rubocop:disable Metrics/MethodLength
raise OptionArgumentError, '-C/--cache argument must be true or false'
end

if display_only_fail_level_offenses_with_autocorrect?
raise OptionArgumentError, '--autocorrect cannot be used with ' \
'--display-only-fail-level-offenses.'
end
validate_auto_gen_config
validate_autocorrect
validate_display_only_failed
Expand Down Expand Up @@ -460,10 +456,6 @@ def only_includes_redundant_disable?
(@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any?
end

def display_only_fail_level_offenses_with_autocorrect?
@options.key?(:display_only_fail_level_offenses) && @options.key?(:autocorrect)
end

def except_syntax?
@options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any?
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ def abs(path)
end

describe '--display-only-fail-level-offenses' do
it 'fails if given with an autocorrect argument' do
%w[--fix-layout -x --autocorrect -a --autocorrect-all -A].each do |o|
expect { options.parse ['--display-only-correctable', o] }
.to raise_error(RuboCop::OptionArgumentError)
%w[--fix-layout -x --autocorrect -a --autocorrect-all -A].each do |o|
it 'fails if given with an autocorrect argument' do
expect { options.parse ['--display-only-fail-level-offenses', o] }
.not_to raise_error(RuboCop::OptionArgumentError)
end
end
end
Expand Down

0 comments on commit 685a7f8

Please sign in to comment.