Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/RedundantBegin autocorrect breaks rhs begin-end #8871

Closed
tsugimoto opened this issue Oct 9, 2020 · 1 comment · Fixed by #8874
Closed

Style/RedundantBegin autocorrect breaks rhs begin-end #8871

tsugimoto opened this issue Oct 9, 2020 · 1 comment · Fixed by #8874
Labels

Comments

@tsugimoto
Copy link
Contributor

Auto-correcting Style/RedundantBegin with RuboCop v0.93 breaks code if the value of subject begin-end block is used as a method call parameter or as rhs of a binop.

No offense if value of subject begin-end block is rhs of an assignment, e.g. @var ||= begin ...

# frozen_string_literal: true

puts begin
  'foo'
  'bar'
end

def f
  'hoge' || begin
    'fuga'
    'piyo'
  end
end

puts f

Expected behavior

These code should not offense nor auto-correct.

Actual behavior

Auto-correction breaks code.

Steps to reproduce the problem

$ ruby main.rb 
bar
hoge
$ rubocop --cache false --auto-correct main.rb --only Style/RedundantBegin
Inspecting 1 file
C

Offenses:

main.rb:3:6: C: [Corrected] Style/RedundantBegin: Redundant begin block detected.
puts begin
     ^^^^^
main.rb:9:13: C: [Corrected] Style/RedundantBegin: Redundant begin block detected.
  'hoge' || begin
            ^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected
$ ruby main.rb 

piyo
$ 

RuboCop version

$ rubocop -V
0.93.0 (using Parser 2.7.2.0, rubocop-ast 0.7.1, running on ruby 2.6.3 x86_64-darwin18)
@tsugimoto
Copy link
Contributor Author

Technically, single line begin-end blocks may or may not break, but they actually look some kind of redundant...

# frozen_string_literal: true

puts begin
  'foo'
end

def f
  'hoge' && begin
    'fuga'
  end
end

puts f

@koic koic added the bug label Oct 9, 2020
koic added a commit to koic/rubocop that referenced this issue Oct 9, 2020
Fixes rubocop#8871.

This PR fixes a false positive for `Style/RedundantBegin`
when using `begin` for method argument or part of conditions.

`begin` keyword may be redundant when using only one expression
in `begin` of each issue case.
However, since it is the unintended case for rubocop#8822, I think it
can be implemented as an enhancement different from this bug fix.
bbatsov pushed a commit that referenced this issue Oct 9, 2020
Fixes #8871.

This PR fixes a false positive for `Style/RedundantBegin`
when using `begin` for method argument or part of conditions.

`begin` keyword may be redundant when using only one expression
in `begin` of each issue case.
However, since it is the unintended case for #8822, I think it
can be implemented as an enhancement different from this bug fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants