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

Strange auto-correction with unwanted repetitive comments and unwanted repetitive conditions #9160

Closed
yujideveloper opened this issue Dec 3, 2020 · 2 comments · Fixed by #9195
Labels

Comments

@yujideveloper
Copy link
Contributor

yujideveloper commented Dec 3, 2020

I got a strange auto-correction with unwanted repetitive comments and unwanted repetitive conditions when I ran rubocop -a on the following codes.

# frozen_string_literal: true

def foo
    # NOTE: comment
    if a? && b?
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
    end
  end

Expected behavior

# frozen_string_literal: true

def foo
  # NOTE: comment
  if a? && b? && !c? && !c?
    puts 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message'
  end
end

Actual behavior

I got the following results and strange auto-correction.

warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.8-compliant syntax, but you are running 2.5.7.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
For /home/vagrant/example: Default configuration from /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rubocop-1.5.1/config/default.yml
Inspecting 1 file
Scanning /home/vagrant/example/example.rb
W

Offenses:

example.rb:4:1: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 0 instead of 2).
# NOTE: comment
^^^^^^^^^^^^^^^
example.rb:4:3: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 2 instead of 0).
  # NOTE: comment
  ^^^^^^^^^^^^^^^
example.rb:4:5: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 4 instead of 2).
    # NOTE: comment
    ^^^^^^^^^^^^^^^
example.rb:5:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
    if a? && b?
^^^^
example.rb:5:3: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 2 instead of 0).
  # frozen_string_literal: true
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:6:1: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 0 instead of 2).
# NOTE: comment
^^^^^^^^^^^^^^^
example.rb:6:12: C: [Corrected] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:6:142: C: [Corrected] Layout/ExtraSpacing: Unnecessary spacing detected.
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
                                                                                                                                             ^
example.rb:6:144: C: [Corrected] Style/IfUnlessModifier: Modifier form of unless makes the line too long.
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
                                                                                                                                               ^^^^^^
example.rb:6:144: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
                                                                                                                                               ^^^^^^
example.rb:7:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.
if a? && b? && !c?

example.rb:7:3: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 2 instead of 0).
  # frozen_string_literal: true
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:8:3: W: [Corrected] Layout/DefEndAlignment: end at 8, 2 is not aligned with def at 3, 0.
  end
  ^^^
example.rb:8:3: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
      unless c?
  ^^^^
example.rb:8:5: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
    unless c?
    ^^^^^^
example.rb:8:7: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
      unless c?
      ^^^^^^
example.rb:9:14: C: [Corrected] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:10:1: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 0 instead of 2).
# NOTE: comment
^^^^^^^^^^^^^^^
example.rb:10:7: W: [Corrected] Layout/EndAlignment: end at 10, 6 is not aligned with unless at 8, 4.
      end
      ^^^
example.rb:11:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.
if a? && b? && !c? && !c?

example.rb:11:3: W: [Corrected] Layout/EndAlignment: end at 11, 2 is not aligned with if at 7, 0.
  end
  ^^^
example.rb:11:3: C: Style/GuardClause: Use a guard clause (return unless a? && b? && !c? && !c?) instead of wrapping the code inside a conditional expression.
  if a? && b? && !c? && !c?
  ^^
example.rb:12:3: C: [Corrected] Layout/IndentationWidth: Use 2 (not 8) spaces for indentation.
          puts 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message'
  ^^^^^^^^
example.rb:12:121: C: Layout/LineLength: Line is too long. [139/120]
    puts 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message'
                                                                                                                        ^^^^^^^^^^^^^^^^^^^
example.rb:13:5: W: [Corrected] Layout/EndAlignment: end at 13, 4 is not aligned with if at 11, 0.
    end
    ^^^

1 file inspected, 25 offenses detected, 23 offenses corrected
Finished in 0.5210478680000961 seconds
# frozen_string_literal: true

def foo
  # NOTE: comment
  # frozen_string_literal: true
  # NOTE: comment
  # frozen_string_literal: true
  # NOTE: comment
  # frozen_string_literal: true
  # NOTE: comment
  if a? && b? && !c? && !c?
    puts 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message'
  end
end

Steps to reproduce the problem

Run rubocop -a on the following codes.

# frozen_string_literal: true

def foo
    # NOTE: comment
    if a? && b?
      puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"  unless c?
    end
  end

RuboCop version

$ [bundle exec] rubocop -V
warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.8-compliant syntax, but you are running 2.5.7.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1.5.1 (using Parser 2.7.2.0, rubocop-ast 1.3.0, running on ruby 2.5.7 x86_64-linux)
@yujideveloper yujideveloper changed the title Strange auto-correction with unwanted repetitive comments Strange auto-correction with unwanted repetitive comments and unwanted repetitive conditions Dec 3, 2020
@koic koic added the bug label Dec 3, 2020
@pdobb
Copy link
Contributor

pdobb commented Dec 3, 2020

Note: I tried reproducing this and found it to be an issue in 1.5.1, but not in 1.4.2.

@koic
Copy link
Member

koic commented Dec 3, 2020

This issue can be reproduced with rubocop -a --only Style/SoleNestedConditional,Style/IfUnlessModifier that smallest cops combination.

koic added a commit to koic/rubocop that referenced this issue Dec 9, 2020
…dConditional`

Fixes rubocop#9160.

This PR fixes an incorrect auto-correct for `Style/IfUnlessModifier`
and `Style/SoleNestedConditional` when auto-correction conflicts for
guard condition.
bbatsov pushed a commit that referenced this issue Dec 9, 2020
…ional`

Fixes #9160.

This PR fixes an incorrect auto-correct for `Style/IfUnlessModifier`
and `Style/SoleNestedConditional` when auto-correction conflicts for
guard condition.
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