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

Autocorrecting a Style/RedundantSelfAssignmentBranch offence breaks existing code #10011

Closed
hugovandevliert opened this issue Aug 13, 2021 · 0 comments · Fixed by #10012
Closed
Labels

Comments

@hugovandevliert
Copy link

hugovandevliert commented Aug 13, 2021

While upgrading our codebase to RuboCop 1.19.0 and running bundle exec rubocop -a, I noticed some failing specs. I believe the Style/RedundantSelfAssignmentBranch cop autocorrections should be marked unsafe. See the example below.


Expected behavior

bundle exec rubocop -a should not break existing code.

Actual behavior

bundle exec rubocop -a broke some existing code.

Steps to reproduce the problem

Let's say we have the following (rather odd) piece of code in test.rb:

class Test
  def foo
    @foo = if @foo_set
      @foo
    else
      @foo_set = true
      @foo = 'bar'
    end
  end
end

test = Test.new
puts test.foo == 'bar' # should be true
puts test.foo == 'bar' # should be true

Running this file will give the following output:

true
true

RuboCop will display the following message on line 4:

Style/RedundantSelfAssignmentBranch: Remove the self-assignment branch.

Executing bundle exec rubocop -a test.rb will update the code to this:

class Test
  def foo
    @foo = unless @foo_set
      @foo_set = true
      @foo = 'bar'
    end
  end
end

test = Test.new
puts test.foo == 'bar' # should be true
puts test.foo == 'bar' # should be true

Running this file will give a different output:

true
false

RuboCop version

1.19.0 (using Parser 3.0.2.0, rubocop-ast 1.10.0, running on ruby 2.7.4 x86_64-darwin20)
  - rubocop-performance 1.11.4
  - rubocop-rails 2.11.3
  - rubocop-rspec 2.4.0
@koic koic added the bug label Aug 13, 2021
koic added a commit to koic/rubocop that referenced this issue Aug 13, 2021
…gnmentBranch`

Fixes rubocop#10011.

This PR fixes a false positive for `Style/RedundantSelfAssignmentBranch`
when using instance variable, class variable, and global variable.
bbatsov pushed a commit that referenced this issue Aug 17, 2021
…ranch`

Fixes #10011.

This PR fixes a false positive for `Style/RedundantSelfAssignmentBranch`
when using instance variable, class variable, and global variable.
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.

2 participants