Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10016] Fix an incorrect auto-correct for Style/SoleNestedConditional #10020

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#10016](https://github.com/rubocop/rubocop/issues/10016): Fix an incorrect auto-correct for `Style/SoleNestedConditional` with `Style/NegatedIf`. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Expand Up @@ -38,6 +38,10 @@ class SoleNestedConditional < Base

MSG = 'Consider merging nested conditions into outer `%<conditional_type>s` conditions.'

def self.autocorrect_incompatible_with
[Style::NegatedIf]
end

def on_if(node)
return if node.ternary? || node.else? || node.elsif?

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -326,6 +326,26 @@ def foo
RUBY
end

it 'corrects `Style/SoleNestedConditional` with `Style/NegatedIf`' do
source = <<~RUBY
if !foo.nil?
if foo.do_something == bar
baz
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/NegatedIf,Style/SoleNestedConditional'
])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
if !foo.nil? && (foo.do_something == bar)
baz
end
RUBY
end

it 'corrects `Style/SoleNestedConditional` with `Style/InverseMethods` and `Style/IfUnlessModifier`' do
source = <<~RUBY
unless foo.to_s == 'foo'
Expand Down