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

Rubocop Style/RedundantSelf changes the semantics of breakouted method arguments when autocorrecting #9613

Closed
takayamaki opened this issue Mar 18, 2021 · 0 comments
Labels

Comments

@takayamaki
Copy link

@takayamaki takayamaki commented Mar 18, 2021

Hi.

I encounted similer case of #5581.

I'm workaround it with a disable comment now.

Thank you.

Minimum example

before

def sample_method((a, b)) # This method expects Array that has 2 elements as argument.
  self.a = a
  self.b.some_method_call b
end

after

def sample_method((a, b))
  self.a = a
  b.some_method_call b
end

Concrete example

I read an example of Shrine gem documentation and tried this.

[7, 16] in /home/user_name/github/foo_app/app/controllers/file_uploads_controller.rb
    7: 
    8:   private
    9: 
   10:   def use_rack_response((status, headers, body))
   11:     self.status = status
   12:     headers.merge! headers
   13:     self.response_body = body
   14:     byebug
=> 15:   end
   16: end
(byebug) headers
{"Content-Type"=>"application/json; charset=utf-8", "Cache-Control"=>"no-store", "Content-Length"=>"753"}
(byebug) self.headers
{"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Download-Options"=>"noopen", "X-Permitted-Cross-Domain-Policies"=>"none", "Referrer-Policy"=>"strict-origin-when-cross-origin"}
(byebug)

Expected behavior

RuboCop auto correction should not change the semantics of the code sample.

Actual behavior

RuboCop auto correction changes the semantics of the code sample.

Steps to reproduce the problem

~/tmp$ bundle ex rubocop -V
1.11.0 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 3.0.0 x86_64-linux)
~/tmp$ cat sample.rb
# frozen_string_literal: true

def sample_method((a, b))
  self.a = a
  self.b.some_method_call b
end

~/tmp$ bundle ex rubocop -a sample.rb 
Inspecting 1 file
C

Offenses:

sample.rb:5:3: C: [Corrected] Style/RedundantSelf: Redundant self detected.
  self.b.some_method_call b
  ^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

Tip: Based on detected gems, the following RuboCop extension libraries might be helpful:
  * rubocop-rspec (http://github.com/rubocop/rubocop-rspec)

You can opt out of this message by adding the following to your config (see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions for more options):
  AllCops:
    SuggestExtensions: false
~/tmp$ 

RuboCop version

~/tmp$ bundle ex rubocop -V
1.11.0 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 3.0.0 x86_64-linux)
@koic koic added the bug label Mar 18, 2021
koic added a commit to koic/rubocop that referenced this issue Mar 18, 2021
Fixes rubocop#9613.

This PR fixes a false positive for `Style/RedundantSelf`
when a self receiver on an lvalue of mlhs arguments.
bbatsov added a commit that referenced this issue Mar 19, 2021
Fixes #9613.

This PR fixes a false positive for `Style/RedundantSelf`
when a self receiver on an lvalue of mlhs arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants