-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
Calling an self.method
in a sentence modifier (... if self.method
) should not be considered redundant if the left part is executing a variable assignment in non-plain form.
Example:
def foo
# should be good
if self.bar == '1'
bar = 'one'
else
bar = self.bar
end if self.bar.present?
bar
end
Expected behavior
🚨 This cop should not be safe for autocorrection.
Personally i would prefer to correct the code, so reporting an offense is not bad, but the autocorrect removes the self.
and breaks the code.
This is an example on how to manually correct this offense:
def foo
# moving the assignment to the very beginning
# of the sentence modifier fixes the offense
bar = if self.bar == '1'
'one'
else
'other'
end if self.bar.present?
bar
end
Actual behavior
This is what the autocorrect does:
def foo
# this breaks as the local `bar` variable is always nil
# when evaluating the right side of the sentence modifier
if self.bar == '1'
bar = 'one'
else
bar = self.bar
end if bar.present?
bar
end
Steps to reproduce the problem
- enable the cop (and AutoCorrect) in .rubocop.yml
Style/RedundantSelf
- run the autocorrect
bin/rubocop --only Style/RedundantSelf -a
RuboCop version
$ [bundle exec] rubocop -V
1.75.6 (using Parser 3.3.8.0, rubocop-ast 1.44.1, analyzing as Ruby 2.7, running on ruby 3.2.2) [arm64-darwin22]
- rubocop-rails 2.32.0
- rubocop-performance 1.25.0