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

Infinity loop in Style/YodaCondition autofix #4412

Closed
ShockwaveNN opened this issue May 24, 2017 · 6 comments
Closed

Infinity loop in Style/YodaCondition autofix #4412

ShockwaveNN opened this issue May 24, 2017 · 6 comments
Labels

Comments

@ShockwaveNN
Copy link
Contributor

ShockwaveNN commented May 24, 2017

Steps to reproduce the problem

  1. Create temp.rb file
p 'a' if :a == :b
  1. Run rubocop -a temp.rb. This cause exception
    Infinite loop detected in /path/test.rb

RuboCop version

$ rubocop -V
0.49.0 (using Parser 2.4.0.0, running on ruby 2.1.10 x86_64-linux)
@bbatsov
Copy link
Collaborator

bbatsov commented May 24, 2017

Seems we didn't account for situations where both sides of an expression are some literals. :-)

@bbatsov bbatsov added the bug label May 24, 2017
@martinmosegaard
Copy link

martinmosegaard commented May 24, 2017

I can also reproduce it with this code:

def test_yoda
  list = [{ number: 1 }]
  list.count { |elm| elm.number == 1 } > 0
end
  • It reports a violation, although I would not like to reverse any operands
  • It crashes with check_for_infinite_loop in autocorrect mode

timrogers pushed a commit to gocardless/bump that referenced this issue May 25, 2017
There is a bug in this version, Rubocop 0.49.0, fixed by rubocop/rubocop#4412, which causes it to be broken when the condition has literals on both sides, as in sidekiq_web.ru.

This disabled the cop, and we'll re-enable it when we update.
tagliala added a commit to diowa/icare that referenced this issue May 25, 2017
@smakagon
Copy link
Contributor

@bbatsov my bad, missed that case. Thanks guys for fixing that really quick.

tagliala added a commit to diowa/icare that referenced this issue May 26, 2017
@eagletmt
Copy link
Contributor

I've found another false-positive case self == var and this case is also fixed by the commit 5700720 .

@bbatsov Could you release a new version? This bug looks critical to me.

@bbatsov
Copy link
Collaborator

bbatsov commented May 26, 2017

I'll likely do this after the weekend. I don't view the bug as critical as it affects a single cop which can easily be disabled.

tagliala added a commit to diowa/icare that referenced this issue May 27, 2017
@dankohn
Copy link

dankohn commented May 29, 2017

The following code produces YodaCondition errors in both directions:

module StringRefinements
  refine String do
    def met?
      self == 'Met'
    end
  end
end

Here are the errors:

rubocop app/models/concerns/string_refinements.rb 
Inspecting 1 file
C
1  Style/YodaCondition
--
1  Total



Offenses:

app/models/concerns/string_refinements.rb:6:7: C: Style/YodaCondition: Reverse the order of the operands 'Met' == self. (https://en.wikipedia.org/wiki/Yoda_conditions)
      'Met' == self
      ^^^^^^^^^^^^^

1 file inspected, 1 offense detected

$ rubocop app/models/concerns/string_refinements.rb 
Inspecting 1 file
C
1  Style/YodaCondition
--
1  Total



Offenses:

app/models/concerns/string_refinements.rb:6:7: C: Style/YodaCondition: Reverse the order of the operands self == 'Met'. (https://en.wikipedia.org/wiki/Yoda_conditions)
      self == 'Met'
      ^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants