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

(PUP-11993) The rest of the Style/Redundant* cops! #9283

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

AriaXLi
Copy link
Contributor

@AriaXLi AriaXLi commented Mar 6, 2024

No description provided.

@AriaXLi AriaXLi requested review from a team as code owners March 6, 2024 19:30
This commit enables the Style/RedundantInterpolation cop and fixes
35 (unsafe) autocorrectable offenses.
This commit enables the Style/RedundantSelf cop and fixes
675 autocorrectable offenses.
This commit enables the Style/RedundantReturn cop and fixes
538 autocorrectable offenses.
@AriaXLi AriaXLi changed the title (PUP-11993) Even more style cops! (PUP-11993) The rest of the Style/Redundant* cops! Mar 6, 2024
@joshcooper
Copy link
Contributor

For posterity, the Redundant/Self is auto-correct safe, but there is a subtle issue to be aware of. If the method being called is a writer/accessor, then you have to use self. For example, here's a class Foo with an accessor name:

❯ cat setter.rb 
# frozen_string_literal: true

class Foo
  attr_accessor :name

  def noself(arg)
    name = arg
  end

  def useself(arg)
    self.name = arg
  end
end

f = Foo.new
f.noself('a')
puts "name '#{f.name}'"
f.useself('a')
puts "name '#{f.name}'"

Note the noself and useself methods behave differently:

❯ bx ruby setter.rb
name ''
name 'a'

This occurs because the noself method actually creates a temporary variable named name instead of calling the accessor!!

Rubocop does detect that the noself method isn't doing what it should and we have that cop enabled:

❯ bx rubocop setter.rb
...
setter.rb:7:5: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - name.
    name = arg
    ^^^^

And rubocop seems to have left alone places where a writer/accessor are being called:

❯ git grep -E 'self\.\w+ = ' lib | head
lib/puppet/application.rb:      self.run_status = nil
lib/puppet/application.rb:      self.run_status = :stop_requested
lib/puppet/application.rb:      self.run_status = :restart_requested
lib/puppet/application/face_base.rb:    self.render_as = format.to_sym
lib/puppet/file_serving/base.rb:    self.path = path
lib/puppet/file_serving/base.rb:    self.links = links if links
lib/puppet/file_serving/base.rb:    self.relative_path = relative_path if relative_path
lib/puppet/file_serving/base.rb:    self.source = source if source
lib/puppet/file_serving/fileset.rb:    self.ignore = []
lib/puppet/file_serving/fileset.rb:    self.links = :manage
...

So I think the RedundantSelf changes are ok.

@joshcooper joshcooper merged commit 62b1cd6 into puppetlabs:main Mar 6, 2024
9 checks passed
@joshcooper joshcooper added the maintenance Maintenance chores are excluded from changelogs label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Maintenance chores are excluded from changelogs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants