Skip to content

Recent release 1.84.1 causing issues with Style/IfUnlessModifier #14837

@cretz

Description

@cretz

I suspect this is caused by some change with #12754. I admit the following report was mostly generated by AI.

Steps to reproduce the problem

Create a file example.rb with this content:

# frozen_string_literal: true

class Example
  def initialize
    @info = {
      last_failure: if some_condition
                      some_value
                    end,
      last_result: if another_condition
                     another_value
                   end,
      parent: if yet_another
                some_object
              end,
      root: if final_condition
              final_value
            end
    }
  end
end

Run:

rubocop --only Style/IfUnlessModifier example.rb

Expected behavior

Process the code

Actual behavior

RuboCop crashes with a NoMethodError:

An error occurred while Style/IfUnlessModifier cop was inspecting example.rb:6:19.
To see the complete backtrace run rubocop -d.

With rubocop -d:

/home/cretz/.asdf/installs/ruby/4.0.0/lib/ruby/gems/4.0.0/gems/rubocop-1.84.1/lib/rubocop/cop/style/if_unless_modifier.rb:267:in 'RuboCop::Cop::Style::IfUnlessModifier#shares_line_with?': undefined method 'line' for nil (NoMethodError)

          inner.loc.line == node.loc.end.line || inner.loc.end.line == node.loc.line
                                                              ^^^^^

The cop attempts to call .line on a nil loc.end object when analyzing multi-line if statements used as hash values.

RuboCop version

$ bundle exec rubocop -V
1.84.1 (using Parser 3.3.10.1, Prism 1.9.0, rubocop-ast 1.49.0, analyzing as Ruby 3.3, running on ruby 4.0.0) [x86_64-linux]

To workaround, 1.84.0 can be pinned as the Rubocop version.

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