Skip to content

Commit

Permalink
[Fix rubocop#2029] Style/RedundantReturn auto-corrects implicit hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Jul 16, 2015
1 parent 5070df5 commit 914b9b2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#2015](https://github.com/bbatsov/rubocop/issues/2015): Fix bug occurring for auto-correction of a misaligned `end` in a file with only one method. ([@jonas054][])
* Allow string interpolation segments inside single quoted string literals when double quotes are preferred. ([@segiddins][])
* [#2026](https://github.com/bbatsov/rubocop/issues/2026): Allow `Time.current` when style is "acceptable".([@palkan][])
* [#2029](https://github.com/bbatsov/rubocop/issues/2029): Fix bug where `Style/RedundantReturn` auto-corrects returning implicit hashes to invalid syntax. ([@rrosenblum][])

## 0.32.1 (24/06/2015)

Expand Down
23 changes: 20 additions & 3 deletions lib/rubocop/cop/style/redundant_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,33 @@ def autocorrect(node)
next
end

return_value, = *node
if node.children.size > 1
kids = node.children.map { |child| child.loc.expression }
corrector.insert_before(kids.first, '[')
corrector.insert_after(kids.last, ']')
add_brackets(corrector, node)
elsif return_value.hash_type?
add_braces(corrector, return_value) unless braces?(return_value)
end
return_kw = range_with_surrounding_space(node.loc.keyword, :right)
corrector.remove(return_kw)
end
end

def braces?(arg)
arg.loc.begin
end

def add_brackets(corrector, node)
kids = node.children.map { |child| child.loc.expression }
corrector.insert_before(kids.first, '[')
corrector.insert_after(kids.last, ']')
end

def add_braces(corrector, node)
kids = node.children.map { |child| child.loc.expression }
corrector.insert_before(kids.first, '{')
corrector.insert_after(kids.last, '}')
end

def arguments?(args)
return false if args.empty?
return true if args.size > 1
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/redundant_return_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@
new_source = autocorrect_source(cop, src)
expect(new_source).to eq(result_src)
end

it 'auto-corrects removes return when using an explicit hash' do
src = ['def func',
' return {:a => 1, :b => 2}',
'end'].join("\n")
result_src = ['def func',
' {:a => 1, :b => 2}', # :a => 1, :b => 2 is not valid Ruby
'end'].join("\n")
new_source = autocorrect_source(cop, src)
expect(new_source).to eq(result_src)
end

it 'auto-corrects by making an implicit hash explicit' do
src = ['def func',
' return :a => 1, :b => 2',
'end'].join("\n")
result_src = ['def func',
' {:a => 1, :b => 2}', # :a => 1, :b => 2 is not valid Ruby
'end'].join("\n")
new_source = autocorrect_source(cop, src)
expect(new_source).to eq(result_src)
end
end

context 'when multi-value returns are allowed' do
Expand Down

0 comments on commit 914b9b2

Please sign in to comment.