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/RedundantFetchBlock causes undefined method error. #8227

Closed
richardkmichael opened this issue Jun 30, 2020 · 4 comments
Closed

Style/RedundantFetchBlock causes undefined method error. #8227

richardkmichael opened this issue Jun 30, 2020 · 4 comments

Comments

@richardkmichael
Copy link
Contributor

I am enabling the recent Style/RedundantFetchBlock and {}.fetch(:key) { } (here in Rails) causes an undefined method error.

Expected behavior

As with other suggestions to #fetch() with a block, Rubocop should suggest the implicit nil returned by the block:

{}.fetch(:key, nil)

Actual behavior

 $ rubocop --force-default-config -d --only 'Style/RedundantFetchBlock' empty_block.rb
Default configuration from /Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/config/default.yml
Inspecting 1 file
Scanning ./empty_block.rb
An error occurred while Style/RedundantFetchBlock cop was inspecting ./empty_block.rb:2:0.
undefined method `basic_literal?' for nil:NilClass
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/style/redundant_fetch_block.rb:76:in `basic_literal?'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/style/redundant_fetch_block.rb:53:in `redundant_fetch_block_candidate?'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/style/redundant_fetch_block.rb:48:in `on_block'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:56:in `block (2 levels) in trigger_responding_cops'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:115:in `with_cop_error_handling'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:55:in `block in trigger_responding_cops'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:54:in `each'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:54:in `trigger_responding_cops'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:32:in `block (2 levels) in <class:Commissioner>'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-ast-0.1.0/lib/rubocop/ast/traversal.rb:59:in `block in on_begin'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-ast-0.1.0/lib/rubocop/ast/traversal.rb:59:in `each'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-ast-0.1.0/lib/rubocop/ast/traversal.rb:59:in `on_begin'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:33:in `block (2 levels) in <class:Commissioner>'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-ast-0.1.0/lib/rubocop/ast/traversal.rb:14:in `walk'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/commissioner.rb:43:in `investigate'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/team.rb:149:in `investigate'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/team.rb:137:in `offenses'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cop/team.rb:75:in `inspect_file'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:299:in `inspect_file'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:247:in `block in do_inspection_loop'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:279:in `block in iterate_until_no_changes'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:272:in `loop'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:272:in `iterate_until_no_changes'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:243:in `do_inspection_loop'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:122:in `block in file_offenses'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:146:in `file_offense_cache'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:120:in `file_offenses'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:111:in `process_file'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:90:in `block in each_inspected_file'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:89:in `each'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:89:in `reduce'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:89:in `each_inspected_file'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:78:in `inspect_files'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/runner.rb:39:in `run'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli/command/execute_runner.rb:21:in `execute_runner'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli/command/execute_runner.rb:13:in `run'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli/command.rb:10:in `run'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli/environment.rb:17:in `run'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli.rb:65:in `run_command'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/lib/rubocop/cli.rb:41:in `run'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/exe/rubocop:13:in `block in <top (required)>'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
/Users/testuser/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/rubocop-0.86.0/exe/rubocop:12:in `<top (required)>'
/Users/testuser/.rbenv/versions/2.5.8/bin/rubocop:23:in `load'
/Users/testuser/.rbenv/versions/2.5.8/bin/rubocop:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Style/RedundantFetchBlock cop was inspecting ./empty_block.rb:2:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.86.0 (using Parser 2.7.1.4, rubocop-ast 0.1.0, running on ruby 2.5.8 x86_64-darwin18)
Finished in 0.13840800000252784 seconds

Steps to reproduce the problem

echo '{}.fetch(:key) { }' > empty_block.rb
rubocop --force-default-config -d --only 'Style/RedundantFetchBlock' empty_block.rb

RuboCop version

0.86.0 (using Parser 2.7.1.4, rubocop-ast 0.1.0, running on ruby 2.5.8 x86_64-darwin18)

Presumably related to the helpful cop added in #8147 by @fatkodima (thank you!).

@richardkmichael richardkmichael changed the title Style/RedundantFetchBlock causes Nil node error on Rails master Style/RedundantFetchBlock causes undefined method error. Jun 30, 2020
@fatkodima
Copy link
Contributor

fatkodima commented Jun 30, 2020

Thanks for reporting this. Will look at it tomorrow, if no one gets ahead of me 😃 .

@jonathanhefner
Copy link

I believe this was already fixed by #8197.

@richardkmichael
Copy link
Contributor Author

@fatkodima You're welcome. It was brought to my attention in rails/rails#39765, so you might consider bumping your own rails/rails#39527 when fixed so Rails could use it.

@richardkmichael
Copy link
Contributor Author

@jonathanhefner Thanks again. :-) It is fast today!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants