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

Style/IfUnlessModifier crashes for given example.rb #9729

Closed
A5308Y opened this issue Apr 22, 2021 · 1 comment · Fixed by #9730
Closed

Style/IfUnlessModifier crashes for given example.rb #9729

A5308Y opened this issue Apr 22, 2021 · 1 comment · Fixed by #9730
Labels

Comments

@A5308Y
Copy link

A5308Y commented Apr 22, 2021

Thanks for for your work on RuboCop!

I see a crash when running rubocop on some files in our app, which prevents us from updating to 1.13.0.
I distilled down the problem to theexample.rb file below.

#9690 in the latest release is related to the cop that is mentioned in the error so I assume this might be related.

Could you have a look?

Thanks in advance,
Andy


Expected behavior

Given a file:

class Example
  def method
    @really_long_instance_variable_name = params[:really_really_long_parameter_name]
    variable = Class.find(@really_long_instance_variable_name) if params[:really_really_long_parameter_name]
  end
end

called example.rb and the following .rubocop.yml:

# We want Exclude directives from different
# config files to get merged, not overwritten
inherit_mode:
  merge:
    - Exclude

inherit_from: .rubocop_todo.yml

require:
  - rubocop-rails
  - rubocop-rspec

Style/Documentation:
  Enabled: false
Metrics/ModuleLength:
  Exclude:
    - "**/*_spec.rb"
Metrics/BlockLength:
  Exclude:
    - "**/*_spec.rb"
Style/ClassAndModuleChildren:
  EnforcedStyle: compact
Style/HashEachMethods:
  Enabled: true
Style/HashTransformKeys:
  Enabled: true
Style/HashTransformValues:
  Enabled: true
Layout/LineLength:
  Max: 100
Layout/SpaceInsideHashLiteralBraces:
  EnforcedStyle: no_space
Layout/ParameterAlignment:
  EnforcedStyle: with_fixed_indentation
Layout/ExtraSpacing:
  AllowForAlignment: false
Layout/MultilineOperationIndentation:
  EnforcedStyle: indented
Naming/VariableNumber:
  EnforcedStyle: snake_case
Layout/EndAlignment:
  EnforcedStyleAlignWith: variable
AllCops:
  TargetRubyVersion: 2.6
  NewCops: enable
  Exclude:
    - 'db/**/*'
    - 'config/**/*'
    - 'bin/**/*'
    - 'app/views/**/*'
Rails:
  Enabled: true

rubocop -d example.rb runs without errors.

Actual behavior

I get the following result when running bundle exec rubocop -d example.rb:


Inspecting 1 file
Scanning example.rb
An error occurred while Style/IfUnlessModifier cop was inspecting example.rb:4:4.
undefined method `last_argument' for #<RuboCop::AST::Node:0x00007fb3d02498d0>
Did you mean?  last_line
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/style/if_unless_modifier.rb:71:in `autocorrect'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/style/if_unless_modifier.rb:62:in `block in on_if'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/base.rb:342:in `correct'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/base.rb:127:in `add_offense'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/style/if_unless_modifier.rb:61:in `on_if'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:100:in `public_send'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:98:in `each'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:69:in `on_if'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:137:in `each'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:154:in `on_def'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:71:in `on_def'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:154:in `on_class'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:71:in `on_class'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.1/lib/rubocop/ast/traversal.rb:20:in `walk'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cop/team.rb:83:in `investigate'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:310:in `inspect_file'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:254:in `block in do_inspection_loop'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:288:in `block in iterate_until_no_changes'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:281:in `loop'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:250:in `do_inspection_loop'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:129:in `file_offenses'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:120:in `process_file'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:100:in `each'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:100:in `reduce'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:86:in `inspect_files'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/runner.rb:47:in `run'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/command.rb:11:in `run'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli/environment.rb:18:in `run'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli.rb:65:in `run_command'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli.rb:72:in `execute_runners'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/lib/rubocop/cli.rb:41:in `run'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/exe/rubocop:12:in `block in <top (required)>'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-1.13.0/exe/rubocop:12:in `<top (required)>'
.rbenv/versions/2.6.3/bin/rubocop:23:in `load'
.rbenv/versions/2.6.3/bin/rubocop:23:in `<top (required)>'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli/exec.rb:74:in `load'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli/exec.rb:74:in `kernel_load'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli/exec.rb:28:in `run'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli.rb:463:in `exec'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli.rb:27:in `dispatch'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/cli.rb:18:in `start'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bundler-1.17.2/exe/bundle:30:in `block in <top (required)>'
.rbenv/versions/2.6.3/lib/ruby/2.6.0/bundler/friendly_errors.rb:124:in `with_friendly_errors'
.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/bundler-1.17.2/exe/bundle:22:in `<top (required)>'
.rbenv/versions/2.6.3/bin/bundle:23:in `load'
.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
W

Offenses:

example.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Example
^
example.rb:4:5: W: Lint/UselessAssignment: Useless assignment to variable - variable.
    variable = Class.find(@really_long_instance_variable_name) if params[:really_really_long_parameter_name]
    ^^^^^^^^
example.rb:4:101: C: Layout/LineLength: Line is too long. [108/100]
    variable = Class.find(@really_long_instance_variable_name) if params[:really_really_long_parameter_name]
                                                                                                    ^^^^^^^^

1 file inspected, 3 offenses detected, 1 offense auto-correctable

1 error occurred:
An error occurred while Style/IfUnlessModifier cop was inspecting repositories/code/Active/webapp/example.rb:4:4.

Steps to reproduce the problem

Create an example.rb as given above and run rubocop -d example.rb.

RuboCop version

1.13.0 (using Parser 3.0.1.0, rubocop-ast 1.4.1, running on ruby 2.6.3 x86_64-darwin19)
  - rubocop-rails 2.9.1
  - rubocop-rspec 2.2.0
@A5308Y A5308Y changed the title Style/IfUnlessModifier crashes for given example.rb Style/IfUnlessModifier crashes for given example.rb Apr 22, 2021
@koic koic added the bug label Apr 22, 2021
@koic
Copy link
Member

koic commented Apr 22, 2021

Thank you for your feedback. I confirmed the issue and opened #9730.

koic added a commit to koic/rubocop that referenced this issue Apr 22, 2021
Fixes rubocop#9729.

This PR fixes an error for `Style/IfUnlessModifier` when
variable assignment is used in the branch body of if modifier.
koic added a commit that referenced this issue Apr 22, 2021
…odifier

[Fix #9729] Fix an error for `Style/IfUnlessModifier`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants