Skip to content

Commit

Permalink
Fix a suggestion message when not auto-correctable
Browse files Browse the repository at this point in the history
This PR fixes a suggestion message when not auto-correctable.

RuboCop can specify `AutoCorrect: false`. In this case, even though
auto-correct will not possible, auto-correct will be suggested.

The following sets `AutoCorrect: false` to `Style/StringLiterals`.

```console
% cat .rubocop.yml
Style/StringLiterals:
  AutoCorrect: false

% cat example.rb
# frozen_string_literal: true

puts "hello"
```

## Before

Running rubocop suggests `1 offense auto-correctable`.

```console
% rubocop
(snip)

Inspecting 1 file
C

Offenses:

example.rb:3:6: C: [Correctable] Style/StringLiterals: Prefer
single-quoted strings when you don't need string interpolation or
special symbols.
puts "hello"
     ^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable
```

Running `rubocop -a` suggests `1 more offense can be corrected with rubocop -A` next.

```console
% rubocop -a
(snip)

Inspecting 1 file
C

Offenses:

example.rb:3:6: C: [Correctable] Style/StringLiterals: Prefer
single-quoted strings when you don't need string interpolation or
special symbols.
puts "hello"
     ^^^^^^^

1 file inspected, 1 offense detected, 1 more offense can be corrected with `rubocop -A`
```

Running `rubocop -A` does not auto-correct it.

```console
% rubocop -A
(snip)

Inspecting 1 file
C

Offenses:

example.rb:3:6: C: [Correctable] Style/StringLiterals: Prefer
single-quoted strings when you don't need string interpolation or
special symbols.
puts "hello"
     ^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable
```

There is no change in the code.

```console
% cat example.rb
# frozen_string_literal: true

puts "hello"
```

User cannnot get the suggested behavior.

## After

RuboCop does not suggest auto-correction if `AutoCorrect: false` is configured.

```console
% rubocop
(snip)

Inspecting 1 file
C

Offenses:

example.rb:3:6: C: Style/StringLiterals: Prefer single-quoted strings
when you don't need string interpolation or special symbols.
puts "hello"
     ^^^^^^^

1 file inspected, 1 offense detected
```
  • Loading branch information
koic authored and bbatsov committed Jan 17, 2021
1 parent ff19e9a commit 7bf9d46
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9384](https://github.com/rubocop-hq/rubocop/pull/9384): Fix a suggestion message when not auto-correctable. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def correct(range)
def use_corrector(range, corrector)
if autocorrect?
attempt_correction(range, corrector)
elsif corrector
elsif corrector && cop_config.fetch('AutoCorrect', true)
:uncorrected
else
:unsupported
Expand Down
22 changes: 21 additions & 1 deletion spec/rubocop/cli/cli_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ def on_send(node)
'--display-style-guide',
'example1.rb'])).to eq(1)

output = "#{file}:1:6: C: [Correctable] Security/JSONLoad: " \
output = "#{file}:1:6: C: Security/JSONLoad: " \
"Prefer `JSON.parse` over `JSON.load`. (#{url})"
expect($stdout.string.lines.to_a[-1])
.to eq([output, ''].join("\n"))
Expand Down Expand Up @@ -1784,6 +1784,26 @@ def f
)
end
end

context 'when setting `AutoCorrect: false` for `Style/StringLiterals`' do
before do
create_file('.rubocop.yml', <<~YAML)
Style/StringLiterals:
AutoCorrect: false
YAML
end

it 'does not suggest `1 more offense can be corrected with `rubocop -A` for `Style/StringLiterals`' do
create_file(target_file, <<~RUBY)
# frozen_string_literal: true
a = "Hello"
RUBY

expect(cli.run(['--auto-correct', '--format', 'simple', target_file])).to eq(1)
expect($stdout.string.lines.to_a.last).to eq("1 file inspected, 2 offenses detected\n")
end
end
end

describe '--force-exclusion' do
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,28 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
end
end

describe 'configuration of `AutoCorrect`' do
context 'when setting `AutoCorrect: false` for `Style/StringLiterals`' do
before do
create_file('.rubocop.yml', <<~YAML)
Style/StringLiterals:
AutoCorrect: false
YAML
end

it 'does not suggest `1 offense auto-correctable` for `Style/StringLiterals`' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
a = "Hello"
RUBY

expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
expect($stdout.string.lines.to_a.last).to eq("1 file inspected, 2 offenses detected\n")
end
end
end

describe 'configuration of target Ruby versions' do
context 'when configured with an unknown version' do
it 'fails with an error message' do
Expand Down

0 comments on commit 7bf9d46

Please sign in to comment.