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

Error when parsing nameless method in Style/MultilineMethodSignature cop #11765

Closed
pjg opened this issue Apr 5, 2023 · 2 comments · Fixed by #11768
Closed

Error when parsing nameless method in Style/MultilineMethodSignature cop #11765

pjg opened this issue Apr 5, 2023 · 2 comments · Fixed by #11768
Labels

Comments

@pjg
Copy link
Contributor

pjg commented Apr 5, 2023

Rubocop errors out while parsing an invalid Ruby file. I'm experiencing this issue (vim displaying rubocop's error) with ruby-lsp and vim every time I type "def" in a Ruby class.

Expected behavior

Not seeing this error.

Actual behavior

An error in Rubocop.

Debug info:

☺ rubocop --debug test.rb                                                                                                                                                        ◉ 20:06
For /Users/<user>/data/www/<app>: configuration from /Users/<user>/data/www/<app>/.rubocop.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-performance-1.16.0/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-performance-1.16.0/config/default.yml
Default configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-rails-2.18.0/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-rails-2.18.0/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-capybara-2.17.1/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-capybara-2.17.1/lib/../config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-rspec-2.19.0/config/default.yml
configuration from /Users/<user>/.gem/ruby/3.1.3/gems/rubocop-rspec-2.19.0/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/<user>/data/www/<app>/test.rb
An error occurred while Style/MultilineMethodSignature cop was inspecting /Users/<user>/data/www/<app>/test.rb:2:2.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/corrector.rb:111:in `to_range'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/corrector.rb:120:in `check_range_validity'
/Users/<user>/.gem/ruby/3.1.3/gems/parser-3.2.2.0/lib/parser/source/tree_rewriter.rb:398:in `combine'
/Users/<user>/.gem/ruby/3.1.3/gems/parser-3.2.2.0/lib/parser/source/tree_rewriter.rb:207:in `wrap'
/Users/<user>/.gem/ruby/3.1.3/gems/parser-3.2.2.0/lib/parser/source/tree_rewriter.rb:243:in `insert_after'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/style/multiline_method_signature.rb:50:in `autocorrect'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/style/multiline_method_signature.rb:32:in `block in on_def'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/base.rb:373:in `correct'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/base.rb:181:in `add_offense'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/style/multiline_method_signature.rb:32:in `on_def'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:107:in `public_send'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:107:in `block (2 levels) in trigger_responding_cops'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:106:in `block in trigger_responding_cops'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:105:in `each'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:105:in `trigger_responding_cops'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:69:in `on_def'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cop/team.rb:98:in `investigate'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:345:in `block in inspect_file'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:344:in `each'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:344:in `flat_map'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:344:in `inspect_file'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:314:in `loop'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:164:in `block in file_offenses'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:189:in `file_offense_cache'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:163:in `file_offenses'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:154:in `process_file'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:134:in `each'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:134:in `reduce'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:134:in `each_inspected_file'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:120:in `inspect_files'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/runner.rb:73:in `run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/command.rb:11:in `run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli/environment.rb:18:in `run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli.rb:118:in `run_command'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli.rb:51:in `block in run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/lib/rubocop/cli.rb:43:in `run'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/exe/rubocop:19:in `block in <top (required)>'
/Users/<user>/.rubies/ruby-3.1.3/lib/ruby/3.1.0/benchmark.rb:311:in `realtime'
/Users/<user>/.gem/ruby/3.1.3/gems/rubocop-1.49.0/exe/rubocop:19:in `<top (required)>'
/Users/<user>/.gem/ruby/3.1.3/bin/rubocop:25:in `load'
/Users/<user>/.gem/ruby/3.1.3/bin/rubocop:25:in `<main>'
C

Offenses:

test.rb:2:3: C: [Correctable] Style/EmptyMethod: Put empty method definitions on a single line.
  def ...
  ^^^
test.rb:3:1: C: [Correctable] Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body beginning.

1 file inspected, 2 offenses detected, 2 offenses autocorrectable

1 error occurred:
An error occurred while Style/MultilineMethodSignature cop was inspecting /Users/<user>/data/www/<app>/test.rb:2:2.
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.49.0 (using Parser 3.2.2.0, rubocop-ast 1.28.0, running on ruby 3.1.3) [arm64-darwin21]
Finished in 0.2925559999421239 seconds

Steps to reproduce the problem

Given test.rb file with the following content:

class Test
  def

  def test; end
end

run:

rubocop test.rb

RuboCop version

1.49.0 (using Parser 3.2.2.0, rubocop-ast 1.28.0, running on ruby 3.1.3) [arm64-darwin21]
@koic koic added the bug label Apr 6, 2023
koic added a commit to koic/rubocop that referenced this issue Apr 6, 2023
Fixes rubocop#11765.

This PR fixes an error for `Style/MultilineMethodSignature`
when line break after `def` keyword.

It seems preferable that the following cases can be detected by a new different cop:

```ruby
def
method_name arg; end
```

This is probably not a common line break, the new cop will be able to handle `class`, `module`,
class method definitions, and others as well.

And this `Style/MultilineMethodSignature` cop is disabled by default,
but I have a feeling that the new cop can be enabled by default.
@pjg
Copy link
Contributor Author

pjg commented Apr 6, 2023

@koic thank you for taking a look at this!

I've found another testcase for this bug, not sure if your fix also handles it:

class Test
  def initialize settings, user = nil, test =
    @user = user
  end

  def test; end
end

@koic
Copy link
Member

koic commented Apr 6, 2023

Yeah, #11768 also prevents that error case.

bbatsov pushed a commit that referenced this issue Apr 6, 2023
Fixes #11765.

This PR fixes an error for `Style/MultilineMethodSignature`
when line break after `def` keyword.

It seems preferable that the following cases can be detected by a new different cop:

```ruby
def
method_name arg; end
```

This is probably not a common line break, the new cop will be able to handle `class`, `module`,
class method definitions, and others as well.

And this `Style/MultilineMethodSignature` cop is disabled by default,
but I have a feeling that the new cop can be enabled by default.
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