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 endExpected 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 endActual 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 endSteps to reproduce the problem
Style/RedundantSelf
bin/rubocop --only Style/RedundantSelf -a
$ [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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4