diff --git a/changelog/fix_with_fail_level_a_ignore_non_correctable_offenses.md b/changelog/fix_with_fail_level_a_ignore_non_correctable_offenses.md new file mode 100644 index 000000000000..88c735bbb5e9 --- /dev/null +++ b/changelog/fix_with_fail_level_a_ignore_non_correctable_offenses.md @@ -0,0 +1 @@ +* [#12091](https://github.com/rubocop/rubocop/pull/12091): With `--fail-level A` ignore non-correctable offenses at :info severity. ([@naveg][]) diff --git a/docs/modules/ROOT/pages/usage/basic_usage.adoc b/docs/modules/ROOT/pages/usage/basic_usage.adoc index 1cae5d09664f..36bc30587518 100644 --- a/docs/modules/ROOT/pages/usage/basic_usage.adoc +++ b/docs/modules/ROOT/pages/usage/basic_usage.adoc @@ -196,7 +196,7 @@ $ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName | Inspect files in order of modification time and stops after first file with offenses. | `--fail-level` -| Minimum xref:configuration.adoc#severity[severity] for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use `A` or `autocorrect` if you'd like them to trigger failure. +| Minimum xref:configuration.adoc#severity[severity] for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use `A` or `autocorrect` if you'd like any autocorrectable offense to trigger failure, regardless of severity. | `--force-exclusion` | Force excluding files specified in the configuration `Exclude` even if they are explicitly passed as arguments. diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index 0f49dabc3c53..3200035ae182 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -421,10 +421,10 @@ def formatter_set end def considered_failure?(offense) - # For :autocorrect level, any offense - corrected or not - is a failure. return false if offense.disabled? - return true if @options[:fail_level] == :autocorrect + # For :autocorrect level, any correctable offense is a failure, regardless of severity + return true if @options[:fail_level] == :autocorrect && offense.correctable? !offense.corrected? && offense.severity >= minimum_severity_to_fail end @@ -461,7 +461,9 @@ def minimum_severity_to_fail @minimum_severity_to_fail ||= begin # Unless given explicitly as `fail_level`, `:info` severity offenses do not fail name = @options[:fail_level] || :refactor - RuboCop::Cop::Severity.new(name) + + # autocorrect is a fake level - use the default + RuboCop::Cop::Severity.new(name == :autocorrect ? :refactor : name) end end diff --git a/spec/rubocop/cli/options_spec.rb b/spec/rubocop/cli/options_spec.rb index 59062c0db689..883954e249f5 100644 --- a/spec/rubocop/cli/options_spec.rb +++ b/spec/rubocop/cli/options_spec.rb @@ -1797,6 +1797,29 @@ def expect_offense_detected expect_offense_detected end + context 'when the cop has the "info" severity' do + before do + create_file(target_file, <<~RUBY) + Long::Line::Not::Autocorrectable + RUBY + + create_file('.rubocop.yml', <<~YAML) + Layout/LineLength: + Max: 10 + Severity: info + YAML + end + + it 'succeeds when option is autocorrect and the offense is not autocorrectable' do + expect(cli.run(['--fail-level', 'autocorrect', + '--only', 'Layout/LineLength', + target_file])).to eq(0) + expect($stderr.string).to eq('') + expect($stdout.string.include?('1 file inspected, 1 offense detected')).to be(true) + expect($stdout.string.include?('Layout/LineLength')).to be(true) + end + end + context 'with --display-only-fail-level-offenses' do it 'outputs offense message when fail-level is less than the severity' do expect(cli.run(['--fail-level', 'refactor',