Skip to content

Commit

Permalink
Merge pull request #1341 from smangelsdorf/symbol-proc-crash
Browse files Browse the repository at this point in the history
[Fix #1340] Handle call with no receiver correctly in Style/SymbolProc cop
  • Loading branch information
bbatsov committed Sep 18, 2014
2 parents 6e22cbd + 62f3ae5 commit 59434c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#1326](https://github.com/bbatsov/rubocop/issues/1326): Fix problem in `SpaceInsideParens` with detecting space inside parentheses used for grouping expressions. ([@jonas054][])
* [#1335](https://github.com/bbatsov/rubocop/issues/1335): Restrict URI schemes permitted by `LineLength` when `AllowURI` is enabled. ([@smangelsdorf][])
* [#1339](https://github.com/bbatsov/rubocop/issues/1339): Handle `eql?` and `equal?` in `OpMethod`. ([@bbatsov][])
* [#1340](https://github.com/bbatsov/rubocop/issues/1340): Fix crash in `Style/SymbolProc` cop when the block calls a method with no explicit receiver. ([@smangelsdorf][])

## 0.26.0 (03/09/2014)

Expand Down
33 changes: 19 additions & 14 deletions lib/rubocop/cop/style/symbol_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,9 @@ def on_block(node)
return if ignored_method?(bmethod_name)
# File.open(file) { |f| f.readlines }
return if bargs
# something { |x, y| ... }
return unless block_args.children.size == 1
return unless block_body && block_body.type == :send

receiver, method_name, args = *block_body

# method in block must be invoked on a lvar without args
return if args
return if receiver.type != :lvar

block_arg_name, = *block_args.children.first
receiver_name, = *receiver

return if block_arg_name != receiver_name
return unless can_shorten?(block_args, block_body)

_receiver, method_name, _args = *block_body
add_offense(node,
:expression,
format(MSG,
Expand All @@ -65,6 +53,23 @@ def ignored_methods
def ignored_method?(name)
ignored_methods.include?(name.to_s)
end

def can_shorten?(block_args, block_body)
# something { |x, y| ... }
return false unless block_args.children.size == 1
return false unless block_body && block_body.type == :send

receiver, _method_name, args = *block_body

# method in block must be invoked on a lvar without args
return false if args
return false unless receiver && receiver.type == :lvar

block_arg_name, = *block_args.children.first
receiver_name, = *receiver

block_arg_name == receiver_name
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/style/symbol_proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@
corrected = autocorrect_source(cop, ['coll.map { |s| s.upcase }'])
expect(corrected).to eq 'coll.map(&:upcase)'
end

it 'does not crash with a bare method call' do
run = -> { inspect_source(cop, ['coll.map { |s| bare_method }']) }
expect(&run).not_to raise_error
end
end

0 comments on commit 59434c1

Please sign in to comment.