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/SelectByRegexp] undefined method lvar_type? for nil:NilClass #10180

Closed
beauraF opened this issue Oct 11, 2021 · 3 comments · Fixed by #10181
Closed

[Style/SelectByRegexp] undefined method lvar_type? for nil:NilClass #10180

beauraF opened this issue Oct 11, 2021 · 3 comments · Fixed by #10181
Labels

Comments

@beauraF
Copy link

beauraF commented Oct 11, 2021

Expected behavior

Rubocop should not fail on following code:

# frozen_string_literal: true

require 'ci/test_files/filters'

module CI
  module TestFiles
    module Filters
      module BaseFilter
        def initialize(_spec); end

        def match?(_test_file)
          raise(NotImplementedError, 'Filter does not implement required method `#match?`')
        end

        def filter(test_files)
          test_files.select { |test_file| match?(test_file) }
        end
      end
    end
  end
end

Actual behavior

Rubocop Style/SelectByRegexp cop is failling.

An error occurred while Style/SelectByRegexp cop was inspecting lib/ci/test_files/filters/base_filter.rb:16:10.
undefined method `lvar_type?' for nil:NilClass

rubocop-1.22.1/lib/rubocop/cop/style/select_by_regexp.rb:124:in `find_regexp'
rubocop-1.22.1/lib/rubocop/cop/style/select_by_regexp.rb:87:in `on_send'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:136:in `public_send'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:134:in `each'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:154:in `on_block'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:154:in `on_def'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_def'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `each'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `each'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:20:in `walk'
rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
rubocop-1.22.1/lib/rubocop/cop/team.rb:155:in `investigate_partial'
rubocop-1.22.1/lib/rubocop/cop/team.rb:83:in `investigate'
rubocop-1.22.1/lib/rubocop/runner.rb:309:in `inspect_file'
rubocop-1.22.1/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
rubocop-1.22.1/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
rubocop-1.22.1/lib/rubocop/runner.rb:280:in `loop'
rubocop-1.22.1/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
rubocop-1.22.1/lib/rubocop/runner.rb:249:in `do_inspection_loop'
rubocop-1.22.1/lib/rubocop/runner.rb:130:in `block in file_offenses'
rubocop-1.22.1/lib/rubocop/runner.rb:155:in `file_offense_cache'
rubocop-1.22.1/lib/rubocop/runner.rb:129:in `file_offenses'
rubocop-1.22.1/lib/rubocop/runner.rb:120:in `process_file'
rubocop-1.22.1/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
rubocop-1.22.1/lib/rubocop/runner.rb:100:in `each'
rubocop-1.22.1/lib/rubocop/runner.rb:100:in `reduce'
rubocop-1.22.1/lib/rubocop/runner.rb:100:in `each_inspected_file'
rubocop-1.22.1/lib/rubocop/runner.rb:86:in `inspect_files'
rubocop-1.22.1/lib/rubocop/runner.rb:47:in `run'
rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
rubocop-1.22.1/lib/rubocop/cli/command.rb:11:in `run'
rubocop-1.22.1/lib/rubocop/cli/environment.rb:18:in `run'
rubocop-1.22.1/lib/rubocop/cli.rb:71:in `run_command'
rubocop-1.22.1/lib/rubocop/cli.rb:78:in `execute_runners'
rubocop-1.22.1/lib/rubocop/cli.rb:47:in `run'
rubocop-1.22.1/exe/rubocop:12:in `block in <top (required)>'
lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
rubocop-1.22.1/exe/rubocop:12:in `<top (required)>'
bin/rubocop:23:in `load'
bin/rubocop:23:in `<top (required)>'
lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `load'
lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `kernel_load'
lib/ruby/3.0.0/bundler/cli/exec.rb:28:in `run'
lib/ruby/3.0.0/bundler/cli.rb:474:in `exec'
lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
lib/ruby/3.0.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
lib/ruby/3.0.0/bundler/cli.rb:30:in `dispatch'
lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
lib/ruby/3.0.0/bundler/cli.rb:24:in `start'
bundler-2.2.22/libexec/bundle:49:in `block in <top (required)>'
lib/ruby/3.0.0/bundler/friendly_errors.rb:128:in `with_friendly_errors'
bundler-2.2.22/libexec/bundle:37:in `<top (required)>'
bin/bundle:23:in `load'
bin/bundle:23:in `<main>'

Steps to reproduce the problem

You can use the following code:

# frozen_string_literal: true

require 'ci/test_files/filters'

module CI
  module TestFiles
    module Filters
      module BaseFilter
        def initialize(_spec); end

        def match?(_test_file)
          raise(NotImplementedError, 'Filter does not implement required method `#match?`')
        end

        def filter(test_files)
          test_files.select { |test_file| match?(test_file) }
        end
      end
    end
  end
end
rubocop base_filter.rb

RuboCop version

➜  bundle exec rubocop -V
1.22.1 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 3.0.2 x86_64-darwin20)
  - rubocop-doctolib 0.1.0
  - rubocop-minitest 0.15.1
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.3
  - rubocop-rake 0.6.0
@koic koic added the bug label Oct 11, 2021
koic added a commit to koic/rubocop that referenced this issue Oct 11, 2021
Fixes rubocop#10180.

This PR fixes an error for `Style/SelectByRegexp` when using `match?` without a receiver.
@beauraF
Copy link
Author

beauraF commented Oct 11, 2021

So quick @koic, thank you so much. 🥰

Small question, are you testing the new versions on large codebase, or are you looking to do so?
If so, maybe we can help each other. I have the impression that our codebase is able to catch most of the problems. If we could find a clever way to leverage this, we could all save time. I would be happy to discuss it. :)

@koic
Copy link
Member

koic commented Oct 12, 2021

Occasionally I check with large OSS and proprietary code, but these rules are different, so the detection (or not detection) is different. If you have any other problems you find, please open issues 😃

koic added a commit that referenced this issue Oct 12, 2021
…regexp

[Fix #10180] Fix an error for `Style/SelectByRegexp`
@beauraF
Copy link
Author

beauraF commented Oct 12, 2021

Okay, thanks a lot again @koic ✌️

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