Rubocop: Layout/RescueEnsureAlignment incorrect behaviour when used as assignment

Created on 11 Apr 2019  路  12Comments  路  Source: rubocop-hq/rubocop

Layout/RescueEnsureAlignment does not support assignment


Expected behavior

Should consider indentation difference from first non-whitespace character in line containing begin.

values = begin
  get_values_array
rescue
  []
end

Actual behavior

(for actual use case, example is simplified)

app/models/concerns/demographics.rb:21:5: C: Layout/RescueEnsureAlignment: rescue at 21, 4 is not aligned with begin at 19, 13.
    rescue ActiveRecord::StatementInvalid => error
    ^^^^^^

Steps to reproduce the problem

Follow example.

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.64.0 (using Parser 2.6.0.0, running on ruby 2.4.5 x86_64-darwin18)

Most helpful comment

Should probably behave in the same way as Layout/EndAlignment by offering the EnforcedStyleAlignWith: variable option.

All 12 comments

Should probably behave in the same way as Layout/EndAlignment by offering the EnforcedStyleAlignWith: variable option.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This bot makes no sense. Why are you closing legitimate issues? To create to appearance that the project is maintained? If you have lots of bugs and nobody's fixing them, then you _should_ have a high issue count.

The alignment that the cop accepts here is hilariously bad :/

values = begin
  get_values_array
         rescue
           []
end

This seems related to #6774 and #6254 and appears to have been introduced in #6246 (the old behaviour of having rescue/ensure match end gave good results here)

I have a similar example of bad output but not from assignment:

  coerce_input(lambda do |value, _ctx|
    URI(value)
  rescue URI::InvalidURIError
    nil
  end)

# Was autocorrected to:

  coerce_input(lambda do |value, _ctx|
    URI(value)
               rescue URI::InvalidURIError
                 nil
  end)

I'm still seeing this. I agree with @rhys-vdw that it should have an option similar to the Layout/EndAlignment

Agree with the previous commentators, this is burning us too, even in 0.75.1.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

FYI issue is still there

There's now Layout/BeginEndAlignment which, when enabled, should make the examples given here be flagged and/or corrected correctly.

via https://github.com/rubocop-hq/rubocop/pull/7531#issuecomment-602966770

I'm curious if this fix extends to cases with memoize, where in rubocop 0.77.0 it prefers this:

  private memoize def do_something
    "Something"
                  rescue NameError
                    nil
  end

but without memoize, Rubocop acts sensibly and allows this:

  private def do_something
    "Something"
  rescue NameError
    nil
  end

The originally reported expected behavior is now the actual behavior with default configuration. Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings