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

Style/RedundantSelf doesn't consider pattern matching variables #10106

Closed
kotovalexarian opened this issue Sep 21, 2021 · 0 comments · Fixed by #10108
Closed

Style/RedundantSelf doesn't consider pattern matching variables #10106

kotovalexarian opened this issue Sep 21, 2021 · 0 comments · Fixed by #10108
Assignees
Labels

Comments

@kotovalexarian
Copy link

kotovalexarian commented Sep 21, 2021

Style/RedundantSelf doesn't consider pattern matching variables. It treats the following code as offense, while removing self would change the behavior:

case cdr
in Integer => bar
  self.bar + bar
end

Also I want to notice that this code will be automatically corrected by RuboCop, which is a serious issue.


Expected behavior

$ rubocop --debug main.rb
For /home/kotovalexarian: configuration from /home/kotovalexarian/.rubocop.yml
Default configuration from /home/kotovalexarian/.rvm/gems/ruby-3.0.2@arcamark/gems/rubocop-1.21.0/config/default.yml
Use parallel by default.
Running parallel inspection
Inspecting 1 file
Scanning /home/kotovalexarian/main.rb
Loading cache from /home/kotovalexarian/.cache/rubocop_cache/1659dbf77b85bb74d511f7d88e7efa9320f1354c/6d7a3b621ca1730e04accd938619e4bdab66cfb1/a12a29925874af02e20456d83e6d5b54d11f7cee
.

1 file inspected, no offenses detected
Finished in 0.15581881400066777 seconds

Actual behavior

$ rubocop --debug main.rb
For /home/kotovalexarian: configuration from /home/kotovalexarian/.rubocop.yml
Default configuration from /home/kotovalexarian/.rvm/gems/ruby-3.0.2@arcamark/gems/rubocop-1.21.0/config/default.yml
Use parallel by default.
Running parallel inspection
Loading cache from /home/kotovalexarian/.cache/rubocop_cache/1659dbf77b85bb74d511f7d88e7efa9320f1354c/6d7a3b621ca1730e04accd938619e4bdab66cfb1/9a472dafb77012ac390f31cfdb34eb67a85f31ab
Inspecting 1 file
Scanning /home/kotovalexarian/main.rb
Loading cache from /home/kotovalexarian/.cache/rubocop_cache/1659dbf77b85bb74d511f7d88e7efa9320f1354c/6d7a3b621ca1730e04accd938619e4bdab66cfb1/9a472dafb77012ac390f31cfdb34eb67a85f31ab
C

Offenses:

main.rb:14:7: C: [Correctable] Style/RedundantSelf: Redundant self detected.
      self.bar + bar
      ^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable
Finished in 0.10107574600078806 seconds

Steps to reproduce the problem

Install Ruby 3.0.2 and RuboCop 1.12.0.

Create file .rubocop.yml with the following content:

AllCops:
  TargetRubyVersion: 3.0
  NewCops: enable

Create file main.rb with the following content:

# frozen_string_literal: true

##
# Foo class.
#
class Foo
  def bar
    100
  end

  def car(cdr)
    case cdr
    in Integer => bar
      self.bar + bar
    end
  end
end

raise unless Foo.new.cdr(23) == 123

RuboCop version

$ rubocop -V 
1.21.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-linux)
@dvandersluis dvandersluis self-assigned this Sep 21, 2021
@dvandersluis dvandersluis linked a pull request Sep 22, 2021 that will close this issue
8 tasks
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Sep 23, 2021
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