Skip to content

Commit

Permalink
[Fix rubocop#4197] Fix false positive in Style/RedundantSelf with p…
Browse files Browse the repository at this point in the history
…arallel assignment

This cop would incorrectly mark a `self` as redundant when used in the
lhs expression of a parallel assignment.

This change fixes that.
  • Loading branch information
Drenmi committed Mar 30, 2017
1 parent 7a2ade6 commit 3e922be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#4179](https://github.com/bbatsov/rubocop/pull/4179): Prevent `Rails/Blank` from breaking when LHS of `or` is a naked falsiness check. ([@rrosenblum][])
* [#4172](https://github.com/bbatsov/rubocop/pull/4172): Fix false positives in `Style/MixinGrouping` cop. ([@drenmi][])
* [#4185](https://github.com/bbatsov/rubocop/pull/4185): Make `Lint/NestedMethodDefinition` aware of `#*_exec` class of methods. ([@drenmi][])
* [#4197](https://github.com/bbatsov/rubocop/pull/4197): Fix false positive in `Style/RedundantSelf` cop with parallel assignment. ([@drenmi][])

## 0.48.0 (2017-03-26)

Expand Down
5 changes: 2 additions & 3 deletions lib/rubocop/cop/style/redundant_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def on_def(node)
add_scope(node)
end

def on_defs(node)
add_scope(node)
end
alias on_defs on_def

def on_args(node)
node.children.each { |arg| on_argument(arg) }
Expand All @@ -90,6 +88,7 @@ def on_lvasgn(node)

def on_send(node)
return unless node.self_receiver? && regular_method_call?(node)
return if node.parent && node.parent.mlhs_type?

return if @allowed_send_nodes.include?(node) ||
@local_variables_scopes[node].include?(node.method_name)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/redundant_self_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
expect(cop.offenses).to be_empty
end

it 'accepts a self receiver on an lvalue of a parallel assignment' do
src = 'a, self.b = c, d'
inspect_source(cop, src)
expect(cop.offenses).to be_empty
end

it 'accepts a self receiver on an lvalue of an or-assignment' do
src = 'self.logger ||= Rails.logger'
inspect_source(cop, src)
Expand Down

0 comments on commit 3e922be

Please sign in to comment.