diff --git a/CHANGELOG.md b/CHANGELOG.md index e715858899a..512cb0073ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) ## 0.68.0 (2019-04-29) diff --git a/lib/rubocop/cop/style/next.rb b/lib/rubocop/cop/style/next.rb index 45e50c314a4..0fbd19a5038 100644 --- a/lib/rubocop/cop/style/next.rb +++ b/lib/rubocop/cop/style/next.rb @@ -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 diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index 498e96a642c..737135d9b5e 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -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