Rubocop: Layout/EndAlignment does not flag misaligned begin..rescue..end block

Created on 18 Feb 2019  路  16Comments  路  Source: rubocop-hq/rubocop

Layout/EndAlignment cop should treat a begin..rescue block similar to if..else

Expected behavior

Expected RuboCop to flag the incorrect alignment of end with keyword begin or rescue.
Expected to get following output:

rubocop test.rb
W

Offenses:

test.rb:8:7: C: Layout/IndentationWidth: Use 2 (not -4) spaces for indentation.
      raisable_method
      ^^^^
test.rb:11:5: W: Layout/EndAlignment: end at 11, 4 is not aligned with begin at 7, 10.
    end
    ^^^

1 file inspected, 2 offenses detected

Actual behavior

rubocop test.rb
.

1 file inspected, no offenses detected

Steps to reproduce the problem

# test.rb
# frozen_string_literal: true

# Dummy Class
class Foo
  def bar
    res = begin
      raisable_method
          rescue StandardError
            fall_back
    end
    res
  end
end

RuboCop version

$ [bundle exec] rubocop -V
0.64.0 (using Parser 2.6.0.0, running on ruby 2.4.4 x64-mingw32)
bug good first issue help wanted

All 16 comments

I'm having trouble duplicating this issue. Can you confirm it is still an issue on 0.66.0? If so, please specify any non-default configuration required to duplicate it. Thanks!

@mikegee It's still an issue at my end. rubocop-0.66.0 does not find any offense in the sample code posted above.
The test.rb is inside a directory with no other file / folders

Indeed. I mixed up your expected and actual behaviors in the initial report. It's very unusual to have the expected offenses so precise. It threw me off.

@mikegee My apologies.
Keeping the focus on Layout/EndAlignment cop, This cop does not raise an offense for the following sample either:

# test.rb
# frozen_string_literal: true

# Dummy Class
class Foo
  def bar
    res = begin
      raisable_method
          rescue StandardError
            fall_back
        end
    res
  end
end

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!

@mikegee @bbatsov can we re-open this? Came here after contributing to the original https://github.com/rubocop-hq/rubocop/issues/6254

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!

The second expected offense (for Layout/EndAlignment) is addressed by #7337.

EDIT: When enabling autocorrect for Layout/EndAlignment with the default EnforcedStyleAlignWith: keyword, this is the output with #7337 applied:

$ rubocop -x test.rb
Inspecting 1 file
W

Offenses:

test.rb:8:7: C: [Corrected] Layout/IndentationWidth: Use 2 (not -4) spaces for indentation.
      raisable_method
      ^^^^
test.rb:9:17: C: [Corrected] Layout/RescueEnsureAlignment: rescue at 9, 16 is not aligned with begin at 7, 10.
                rescue StandardError
                ^^^^^^
test.rb:10:11: C: [Corrected] Layout/IndentationWidth: Use 2 (not 8) spaces for indentation.
                  fall_back
          ^^^^^^^^
test.rb:11:5: W: [Corrected] Layout/EndAlignment: end at 11, 4 is not aligned with begin at 7, 10.
    end
    ^^^

1 file inspected, 4 offenses detected, 4 offenses corrected

On the other hand, for the other styles, there is no offense detected.

I assume we're discussing EnforcedStyleAlignWith: start_of_line, is this correct?

Let me add another case, similar to begin...rescue...end that is case...when...else...end, so basically another multi-clause statement:

            @expected_number = case count
                               when :once then 1
                               when :twice then 2
                               when :thrice then 3
                               else Integer(count)
                               end

Origin.
The cop suggests to align end with @, and that's an extremely odd suggestion.

@pirj This thread was originally about something RuboCop didn't do as expected instead of what it does / suggests..
However, I don't know if the original case was silently fixed by one of the recent releases.

Do you mind checking your case @ashmaroli ?

Do you mind checking your case

You could've checked it yourselves just as easily.
Anyways, my use case is still reproducible with RuboCop 0.79 and default configuration — No offenses detected in spite of end not aligned with begin and rescue

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!

I just tested the original case with RuboCop 0.89 (see this gist) and the default configuration, and it still detects no offenses. Setting either EnforcedStyleAlignWith: variable or start_of_line has no effect.

I think the begin..rescue..end case is now handled by the new cops Layout/BeginEndAlignment and Layout/RescueEnsureAlignment. Please note that the default setting for Layout/BeginEndAlignment is different.

Looks good! 0.92 with all new cops enabled now gives this for the gist above:

Offenses:

test.rb:7:5: C: Style/RedundantAssignment: Redundant assignment before returning detected.
    res = begin ...
    ^^^^^^^^^^^
test.rb:9:11: C: Layout/RescueEnsureAlignment: rescue at 9, 10 is not aligned with res = begin at 7, 4.
          rescue StandardError
          ^^^^^^

And autocorrect produces the much more readable

class Foo
  def bar
    raisable_method
  rescue StandardError
    fall_back
  end
end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirrmann picture kirrmann  路  3Comments

david942j picture david942j  路  3Comments

bbatsov picture bbatsov  路  3Comments

printercu picture printercu  路  3Comments

bquorning picture bquorning  路  3Comments