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

Auto-correcting with Style/IfUnlessModifier and Style/SoleNestedConditional breaks code #10016

Closed
ghost opened this issue Aug 16, 2021 · 0 comments · Fixed by #10021
Closed
Labels

Comments

@ghost
Copy link

ghost commented Aug 16, 2021

Style/IfUnlessModifier and Style/SoleNestedConditional produce incorrect code when auto-correcting the following code:

if !error_list.nil?
  if error_list.first == 'Bad error'
    warn 'Error'
    warn 'Error'
  end
end

The following similar bits of code do not have the same problem:

# Before auto-correct
unless error_list.nil?
  if error_list.first == 'Bad error'
    warn 'Error'
    warn 'Error'
  end
end

# After auto-correct (CORRECT)
if !error_list.nil? && (error_list.first == 'Bad error')
  warn 'Error'
  warn 'Error'
end
# Before auto-correct
if !error_list.nil?
  if error_list.first == 'Bad error'
    warn 'Error'
  end
end

# After auto-correct (CORRECT)
warn 'Error' if !error_list.nil? && (error_list.first == 'Bad error')

Expected behavior

rubocop --auto-correct should produce the following code:

if !error_list.nil? && (error_list.first == 'Bad error')
  warn 'Error'
  warn 'Error'
end

Actual behavior

Rubocop produces the following incorrect code. The code now throws an error if error_list is nil.

unless error_list.nil? && (error_list.first == 'Bad error')
  warn 'Error'
  warn 'Error'
end

Steps to reproduce the problem

Run rubocop --auto-correct --debug code.rb on the following code.

# frozen_string_literal: true

error_list = nil

if !error_list.nil?
  if error_list.first == 'Bad error'
    warn 'Error'
    warn 'Error'
  end
end

Output on my machine:

For tmp: Default configuration from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.19.0/config/default.yml
Inspecting 1 file
Scanning code.rb
Loading cache from .cache/rubocop_cache/ab7574e02bf1ddf27d5908048ef79f0a1f430219/0536d18c9eb040c724be8df71fd74a67730b58ef/a690a3cbd7835d1574c4423e161c138242455f2c
W

Offenses:

code.rb:5:1: C: [Corrected] Style/NegatedIf: Favor unless over if for negative conditions.
if !error_list.nil? ...
^^^^^^^^^^^^^^^^^^^
code.rb:6:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
    warn 'Error'
^^^^
code.rb:6:3: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
  if error_list.first == 'Bad error'
  ^^
code.rb:7:5: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
    warn 'Error'
    ^^^^^^^^^^^^
code.rb:8:3: W: [Corrected] Layout/EndAlignment: end at 8, 2 is not aligned with unless at 5, 0.
  end
  ^^^

1 file inspected, 5 offenses detected, 5 offenses corrected
Finished in 0.658765800006222 seconds

RuboCop version

rubocop -V
1.19.0 (using Parser 3.0.2.0, rubocop-ast 1.10.0, running on ruby 2.7.4 x64-mingw32)
@koic koic added the bug label Aug 16, 2021
koic added a commit to koic/rubocop that referenced this issue Aug 16, 2021
…edConditional`

Fixes rubocop#10016.

This PR fixes an incorrect auto-correct for `Style/SoleNestedConditional`
with `Style/NegatedIf`.
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Aug 16, 2021
…` and `Style/NegatedIf`/`Style/NegatedUnless`.
koic added a commit that referenced this issue Aug 17, 2021
[Fix #10016] Fix conflict between `Style/SoleNestedConditional` and `Style/NegatedIf` or `Style/NegatedUnless`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant