From 789a4cc4ef926fd34a2218f6f50440e8eab16db5 Mon Sep 17 00:00:00 2001 From: nobuyo Date: Fri, 29 Apr 2022 21:26:56 +0900 Subject: [PATCH] [Fix #10536] Fix validation for command-line options combination of `--display-only-fail-level-offenses` and `--auto-correct` --- changelog/fix_validation_for_commandline_options.md | 1 + lib/rubocop/options.rb | 5 +++-- spec/rubocop/options_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_validation_for_commandline_options.md diff --git a/changelog/fix_validation_for_commandline_options.md b/changelog/fix_validation_for_commandline_options.md new file mode 100644 index 00000000000..d49e41b5841 --- /dev/null +++ b/changelog/fix_validation_for_commandline_options.md @@ -0,0 +1 @@ +* [#10536](https://github.com/rubocop/rubocop/issues/10536): Fix validation for command-line options combination of `--display-only-fail-level-offenses` and `--auto-correct`. ([@nobuyo][]) diff --git a/lib/rubocop/options.rb b/lib/rubocop/options.rb index 73512a7f85f..d063f52cf8d 100644 --- a/lib/rubocop/options.rb +++ b/lib/rubocop/options.rb @@ -309,7 +309,7 @@ def validate_compatibility # rubocop:disable Metrics/MethodLength end if display_only_fail_level_offenses_with_autocorrect? - raise OptionArgumentError, '--autocorrect cannot be used with ' \ + raise OptionArgumentError, '--auto-correct cannot be used with ' \ '--display-only-fail-level-offenses' end @@ -402,7 +402,8 @@ def only_includes_redundant_disable? end def display_only_fail_level_offenses_with_autocorrect? - @options[:display_only_fail_level_offenses] && @options[:autocorrect] + @options[:display_only_fail_level_offenses] && + (@options.key?(:auto_correct) || @options.key?(:safe_auto_correct)) end def except_syntax? diff --git a/spec/rubocop/options_spec.rb b/spec/rubocop/options_spec.rb index 9bc6c7ced7c..9a91da27d98 100644 --- a/spec/rubocop/options_spec.rb +++ b/spec/rubocop/options_spec.rb @@ -290,6 +290,15 @@ def abs(path) end end + describe '--display-only-fail-level-offenses' do + it 'fails if given with --auto-correct' do + %w[--auto-correct -a --auto-correct-all -A].each do |o| + expect { options.parse ['--display-only-correctable', o] } + .to raise_error(RuboCop::OptionArgumentError) + end + end + end + describe '--display-only-correctable' do it 'fails if given with --display-only-failed' do expect { options.parse %w[--display-only-correctable --display-only-failed] }