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

Wrong auto correction Style/SoleNestedConditional #9233

Closed
thinca opened this issue Dec 15, 2020 · 1 comment · Fixed by #9338
Closed

Wrong auto correction Style/SoleNestedConditional #9233

thinca opened this issue Dec 15, 2020 · 1 comment · Fixed by #9338
Assignees
Labels

Comments

@thinca
Copy link

thinca commented Dec 15, 2020

I got a wrong auto-correction with Style/SoleNestedConditional cop and without Style/IfUnlessModifier cop.

.rubocop.yml

AllCops:
  NewCops: disable

Style/IfUnlessModifier:
  Enabled: false

test.rb

# an example file

def func
  # foo
  foo

  # hoge
  if foo
    if bar
      buz
    end
  end
end

Run:

$ rubocop -a --debug --only Style/SoleNestedConditional test.rb

Expected behavior

# an example file

def func
  # foo
  foo

  # hoge
  if foo && bar
    buz
  end
end

Actual behavior

Output:

For /Users/thinca/tmp/rubocop-test: configuration from /Users/thinca/tmp/rubocop-test/.rubocop.yml
Default configuration from /Users/thinca/tmp/rubocop-test/vendor/bundle/ruby/2.7.0/gems/rubocop-1.6.1/config/default.yml
Inspecting 1 file
Scanning /Users/thinca/tmp/rubocop-test/test.rb
Loading cache from /Users/thinca/.cache/rubocop_cache/fd9e92937dd32541de61425da45d7dfa0c54bb9e/5c3a0012aae88f33b221aa9bbc6dcfba8f2100c7/9f77710eeb664a239190bf981ba7f5ff6c702a02
C

Offenses:

test.rb:9:5: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
    if bar
    ^^

1 file inspected, 1 offense detected, 1 offense corrected
Finished in 0.12683500000275671 seconds

test.rb

# an example file

def func
  # foo
  foo

  # hoge
  # an example file
# foo
# hoge
if foo && bar
      buz
    end
end
  • Extra comments are appears
  • Strange indentation

Steps to reproduce the problem

$ mkdir -p ~/tmp/rubocop-test
$ cd ~/tmp/rubocop-test
$ (put .rubocop.yml and test.rb. rubocop is already installed.)
$ rubocop -a --debug --only Style/SoleNestedConditional test.rb

RuboCop version

$ rubocop -V
1.6.1 (using Parser 2.7.2.0, rubocop-ast 1.3.0, running on ruby 2.7.2 x86_64-darwin19)
@koic koic added the bug label Dec 15, 2020
@koic
Copy link
Member

koic commented Dec 15, 2020

Thank you for the feedback. Part of auto-correction of indentation will be left to other Layout cop yet (Layout/IndentationWidth cop), but the auto-corrected comment is clearly incorrect.

@Darhazer Darhazer self-assigned this Jan 6, 2021
Darhazer added a commit that referenced this issue Jan 6, 2021
…ocorrection

Previously it would gather all comments up to the root node, bringing non-relevant comments to the if node.
bbatsov pushed a commit that referenced this issue Jan 7, 2021
…ocorrection

Previously it would gather all comments up to the root node, bringing non-relevant comments to the if node.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants