Skip to content

Commit

Permalink
[Fix #6738] Prevent auto-correct conflict of Style/Next and `Style/…
Browse files Browse the repository at this point in the history
…SafeNavigation` (#7006)

Fixes #6738

Solves problem like below:

  ```ruby
  until x
    if foo
      foo.some_method do
        y
      end
    end
  end
  ```

  auto-corrects badly;

  ```ruby
  # frozen_string_literal: true

  until x
    next unless foofoo&.some_method do
      y
    end
  end
  ```
  • Loading branch information
hoshinotsuyoshi authored and bbatsov committed Apr 30, 2019
1 parent dea47fb commit 36673f0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
* [#6998](https://github.com/rubocop-hq/rubocop/pull/6998): Fix autocorrect of `Naming/RescuedExceptionsVariableName` to also rename all references to the variable. ([@Darhazer][])
* [#6992](https://github.com/rubocop-hq/rubocop/pull/6992): Fix unknown default configuration for `Layout/IndentFirstParameter` cop. ([@drenmi][])
* [#6972](https://github.com/rubocop-hq/rubocop/issues/6972): Fix a false positive for `Style/MixinUsage` when using inside block and `if` condition is after `include`. ([@koic][])
* [#6738](https://github.com/rubocop-hq/rubocop/issues/6738): Prevent auto-correct conflict of `Style/Next` and `Style/SafeNavigation`. ([@hoshinotsuyoshi][])
* [#6847](https://github.com/rubocop-hq/rubocop/pull/6847): Fix `Style/BlockDelimiters` to properly check if the node is chaned when `braces_for_chaining` is set. ([@att14][])

## 0.68.0 (2019-04-29)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/next.rb
Expand Up @@ -54,6 +54,10 @@ class Next < Cop
MSG = 'Use `next` to skip iteration.'.freeze
EXIT_TYPES = %i[break return].freeze

def self.autocorrect_incompatible_with
[Style::SafeNavigation]
end

def investigate(_processed_source)
# When correcting nested offenses, we need to keep track of how much
# we have adjusted the indentation of each line
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -467,6 +467,36 @@ def verify_section
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects Style/Next and Style/SafeNavigation offenses' do
create_file('.rubocop.yml', <<-YAML.strip_indent)
AllCops:
TargetRubyVersion: 2.3
YAML
source = <<-'RUBY'.strip_indent
until x
if foo
foo.some_method do
y
end
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct',
'--only', 'Style/Next,Style/SafeNavigation'
])).to eq(0)
corrected = <<-'RUBY'.strip_indent
until x
next unless foo
foo.some_method do
y
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects `Lint/Lambda` and `Lint/UnusedBlockArgument` offenses' do
source = <<-'RUBY'.strip_indent
c = -> event do
Expand Down

0 comments on commit 36673f0

Please sign in to comment.