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

Do not report Yoda condition problem where both values are de facto constants (__FILE__ == $0) #7569

Closed
matkoniecz opened this issue Dec 15, 2019 · 0 comments · Fixed by #7583

Comments

@matkoniecz
Copy link

matkoniecz commented Dec 15, 2019

Yoda conditions should not be reported where both values are treated as constants.
I think that $0 can be assumed to be constant, and modifying it reported as a separate cop.


Expected behavior

In a reasonable program $0 is not going to be changed and both $0 and __FILE__ can be treated as read-only. So Yoda condition report should not be triggered for them.

Actual behavior

C: Style/YodaCondition: Reverse the order of the operands __FILE__ == $0.
if __FILE__ == $0

Steps to reproduce the problem

puts "test" if __FILE__ == $0

RuboCop version

rubocop -V
`0.77.0 (using Parser 2.6.5.0, running on ruby 2.6.5 x86_64-linux-gnu)`

it was after

gem2.6 install --user --update rubocop
Fetching rubocop-0.77.0.gem
Successfully installed rubocop-0.77.0
1 gem installed
koic added a commit to koic/rubocop that referenced this issue Dec 25, 2019
Fixes rubocop#7569.

This PR makes `Style/YodaCondition` cop accept `__FILE__ == $0`

I think that both operands `__FILE__ == $0` can be assumed to be
read-only, as mentioned in rubocop#7569. If users try to assign `__FILE__`,
an error will occur. Also, it is not usually assigned to `$0` by users.

Therefore, this PR will make `Style/YodaCondition` cop accept both
`__FILE__` and `$0` on the left side.

Also, this PR will change only an idiom `__FILE__ == $0`.
bbatsov pushed a commit that referenced this issue Dec 27, 2019
Fixes #7569.

This PR makes `Style/YodaCondition` cop accept `__FILE__ == $0`

I think that both operands `__FILE__ == $0` can be assumed to be
read-only, as mentioned in #7569. If users try to assign `__FILE__`,
an error will occur. Also, it is not usually assigned to `$0` by users.

Therefore, this PR will make `Style/YodaCondition` cop accept both
`__FILE__` and `$0` on the left side.

Also, this PR will change only an idiom `__FILE__ == $0`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant