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

An error occurred while Style/SwapValues cop was inspecting bug.rb #10394

Closed
pvdb opened this issue Feb 2, 2022 · 0 comments · Fixed by #10395
Closed

An error occurred while Style/SwapValues cop was inspecting bug.rb #10394

pvdb opened this issue Feb 2, 2022 · 0 comments · Fixed by #10395
Labels

Comments

@pvdb
Copy link

pvdb commented Feb 2, 2022

The following error message is printed when processing an otherwise perfectly valid Ruby file:

1 error occurred:
An error occurred while Style/SwapValues cop was inspecting /private/tmp/rubocop/bug.rb:3:5.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

This appears to be caused - after a bit of digging - by the following exception:

An error occurred while Style/SwapValues cop was inspecting /private/tmp/rubocop/bug.rb:3:5.
undefined method `type' for :do_it:Symbol
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:61:in `simple_assignment?'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:53:in `swapping_values?'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:35:in `on_asgn'
...

Expected behavior

Consider the following Ruby script named bug.rb:

# frozen_string_literal: true

def (blegga = Object.new).do_it
  puts 42
end

blegga.do_it

It is completely valid (but possibly questionable) 😅 Ruby syntax:

> ruby -W2 -c bug.rb
Syntax OK
> _

It runs correctly:

> ruby bug.rb
42
> _

It lints correctly:

> rubocop --lint bug.rb
Inspecting 1 file
.

1 file inspected, no offenses detected
> _

Actual behavior

The Style/SwapValues cop chokes on it:

> rubocop --only Style/SwapValues bug.rb
An error occurred while Style/SwapValues cop was inspecting /private/tmp/rubocop/bug.rb:3:5.
To see the complete backtrace run rubocop -d.
Inspecting 1 file
An error occurred while Style/SwapValues cop was inspecting /private/tmp/rubocop/bug.rb:3:5.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

...

> _

Steps to reproduce the problem

  1. create a file named bug.rb with the above content
  2. run rubocop --only Style/SwapValues bug.rb

Alternatively, run the following one-liner (in a shell that supports so-called "here strings"):

rubocop --debug --stdin /dev/null --only Style/SwapValues <<<"def (_ = Object.new).do_it() puts 42; end"

Running this one-liner includes the stacktrace, and points at the root cause of the bug:

For /private/tmp/rubocop: Default configuration from /usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/config/default.yml
Inspecting 1 file
Scanning /dev/null
An error occurred while Style/SwapValues cop was inspecting /dev/null:1:5.
undefined method `type' for :do_it:Symbol
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:61:in `simple_assignment?'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:53:in `swapping_values?'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/style/swap_values.rb:35:in `on_asgn'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:98:in `each'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:69:in `on_lvasgn'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.15.1/lib/rubocop/ast/traversal.rb:155:in `on_defs'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:71:in `on_defs'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.15.1/lib/rubocop/ast/traversal.rb:20:in `walk'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cop/team.rb:83:in `investigate'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:309:in `inspect_file'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:280:in `loop'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:249:in `do_inspection_loop'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:129:in `file_offenses'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:120:in `process_file'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `each'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `reduce'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:86:in `inspect_files'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/runner.rb:47:in `run'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/command.rb:11:in `run'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli/environment.rb:18:in `run'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli.rb:71:in `run_command'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli.rb:78:in `execute_runners'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/lib/rubocop/cli.rb:47:in `run'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/exe/rubocop:12:in `block in <top (required)>'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/usr/local/var/rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/rubocop-1.25.0/exe/rubocop:12:in `<top (required)>'
/usr/local/var/rbenv/versions/2.6.9/bin/rubocop:23:in `load'
/usr/local/var/rbenv/versions/2.6.9/bin/rubocop:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Style/SwapValues cop was inspecting /dev/null:1:5.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.25.0 (using Parser 3.1.0.0, rubocop-ast 1.15.1, running on ruby 2.6.9 x86_64-darwin19)
Finished in 0.12142199999652803 seconds

RuboCop version

> rubocop -V
1.25.0 (using Parser 3.1.0.0, rubocop-ast 1.15.1, running on ruby 2.6.9 x86_64-darwin19)
> _
@koic koic added the bug label Feb 2, 2022
koic added a commit to koic/rubocop that referenced this issue Feb 2, 2022
Fixes rubocop#10394.

This PR fixes an error for `Style/SwapValues`
when assigning receiver object at `def`.
bbatsov pushed a commit that referenced this issue Feb 3, 2022
Fixes #10394.

This PR fixes an error for `Style/SwapValues`
when assigning receiver object at `def`.
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