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

Layout/RescueEnsureAlignment issue with nested begin #6603

Closed
dreyks opened this issue Dec 26, 2018 · 8 comments
Closed

Layout/RescueEnsureAlignment issue with nested begin #6603

dreyks opened this issue Dec 26, 2018 · 8 comments

Comments

@dreyks
Copy link
Contributor

dreyks commented Dec 26, 2018

Looks similar to #6433

Expected behavior

No error emitted

Actual behavior

Cop expects rescue to be aligned with begin

Steps to reproduce the problem

def some_method
  @ivar ||= begin
     1
  rescue
    2
  end
end

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

$ [bundle exec] rubocop -V
0.61.1 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin17)
@dreyks dreyks changed the title Layout/RescueEnsureAlignment Layout/RescueEnsureAlignment issue with nested begin Dec 26, 2018
@jaredbeck
Copy link
Contributor

I believe this cop is working as intended. Here are some options you have:

# A
v ||= begin
        1
      rescue
        2
      end

# B
def one_two
  1
rescue
  2
end

v ||= one_two

# C
v ||=
  begin
    1
  rescue
    2
  end

I prefer B, personally. Hope that helps!

@jaredbeck
Copy link
Contributor

Tests improved by #6606

@dreyks
Copy link
Contributor Author

dreyks commented Dec 27, 2018

Hm... so the following code while being contrived and generally not good is perfectly fine with rubocop

case something
when something_else
  whatever = case nested
  when no_matter
    ret
  end
end

the nested when is not required to be placed relative to outer indentations

why is rescue different then?

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 29, 2018

@dreyks Likely an oversight.

Generally some alignment cops have configurable styles to allow one of the two common styles of alignment, but this is not one of them (yet). I guess someone can tackle this down the road.

@dreyks
Copy link
Contributor Author

dreyks commented Dec 29, 2018

ok cool. I might look into it if I have some spare time

jaredbeck added a commit to jaredbeck/rubocop that referenced this issue Dec 31, 2018
Add a test to demonstrate the correct styles of a begin-rescue as the
RHS of an assignment.
@oehlschl
Copy link

oehlschl commented Jan 1, 2019

Here after upgrading from 0.58 to 0.61, as this cop triggered many new failures for previously allowed syntax:

result = begin
  # do something
rescue
  # do something else
end

Since this was previously valid, and since a similar format is allowed for Ruby 2.5 do/rescue blocks (per #6437), I'll also put in a vote for either loosening or parameterizing this.

@oehlschl
Copy link

oehlschl commented Jan 1, 2019

Also, FWIW, I'm guessing you can close this in favor of #6254, which seems like one of the more popular threads for this issue/request.

@Drenmi
Copy link
Collaborator

Drenmi commented Feb 5, 2019

Closing as duplicate of #6254. Thanks @oehlschl! 🙇

@Drenmi Drenmi closed this as completed Feb 5, 2019
bbatsov pushed a commit that referenced this issue Mar 17, 2019
Add a test to demonstrate the correct styles of a begin-rescue as the
RHS of an assignment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants