Skip to content

Autocorrect with Style/EmptyElse in nested if statements causes syntax error #4491

@andrewngo

Description

@andrewngo

Running autocorrect on nested if statements can cause a syntax error if there's an if block with an empty else. Seems to be deleting one extra end statement. I just fixed it manually in our code, but autocorrect shouldn't introduce syntax errors if possible.


Expected behavior

Deletes only the else and empty line.

def nested_if_empty_else(s)
  if s.is_a?(Hash)
    if s.empty?
      puts 'hello'
    end
  end
end

Actual behavior

An extra end is deleted causing a syntax error

def nested_if_empty_else(s)
  if s.is_a?(Hash)
    if s.empty?
      puts 'hello'
    end
end

unexpected token $end

Steps to reproduce the problem

Run the following on this test code
rubocop --only EmptyElse -a test_file.rb

def nested_if_empty_else(s)
  if s.is_a?(Hash)
    if s.empty?
      puts 'hello'
    else

    end
  end
end

RuboCop version

$ rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.3.4 x86_64-darwin16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions