Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Style/RedundantBegin
Browse files Browse the repository at this point in the history
This PR fixes an incorrect auto-correct for `Style/RedundantBegin`
when using multi-line `if` in `begin` block.

And fixes standardrb/standard#289 that is causing
the following infinite loop error due to `Style/RedundantBegin` with
`Style/MultilineMemoization`.

```ruby
% cat example.rb
@memo ||= begin
  if condition
    do_something
  end
end

% bundle exec rubocop -a --only Style/RedundantBegin,Style/MultilineMemoization
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/MultilineMemoization: Wrap
multiline memoization blocks in begin and end.
@memo ||= (if condition ...
^^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:11: C: [Corrected] Style/RedundantBegin: Redundant begin
block detected.
@memo ||= begin
          ^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/289/example.rb and caused
by Style/MultilineMemoization
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:285:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in
`loop'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'
```
  • Loading branch information
koic committed May 7, 2021
1 parent 5ba77b9 commit b20bf22
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9777](https://github.com/rubocop/rubocop/pull/9777): Fix an incorrect auto-correct for `Style/RedundantBegin` when using multi-line `if` in `begin` block. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_begin.rb
Expand Up @@ -110,7 +110,7 @@ def replace_begin_with_statement(corrector, offense_range, node)
first_child = node.children.first

source = first_child.source
source = "(#{source})" if first_child.if_type?
source = "(#{source})" if first_child.if_type? && first_child.modifier_form?

corrector.replace(offense_range, source)
corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -553,6 +553,25 @@ def verify_section
expect(File.read('example.rb')).to eq(corrected)
end

it 'corrects `Style/RedundantBegin` with `Style/MultilineMemoization`' do
source = <<~RUBY
@memo ||= begin
if condition
do_something
end
end
RUBY
create_file('example.rb', source)
expect(cli.run(['-a', '--only', 'Style/RedundantBegin,Style/MultilineMemoization'])).to eq(0)
corrected = <<~RUBY
@memo ||= if condition
do_something
end
#{trailing_whitespace * 10}
RUBY
expect(File.read('example.rb')).to eq(corrected)
end

it 'corrects LineEndConcatenation offenses leaving the ' \
'RedundantInterpolation offense unchanged' do
# If we change string concatenation from plus to backslash, the string
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/redundant_begin_spec.rb
Expand Up @@ -248,6 +248,23 @@ def method
RUBY
end

it 'registers and corrects an offense when using multi-line `if` in `begin` block' do
expect_offense(<<~RUBY)
var ||= begin
^^^^^ Redundant `begin` block detected.
if condition
foo
end
end
RUBY

expect_correction(<<~RUBY)
var ||= if condition
foo
end\n
RUBY
end

it 'does not register an offense when using `begin` with multiple statement for or assignment' do
expect_no_offenses(<<~RUBY)
var ||= begin
Expand Down

0 comments on commit b20bf22

Please sign in to comment.