Skip to content

Style/SafeNavigation crashes with 1.76.0 #14233

@dentarg

Description

@dentarg

Consider this code

$ cat safe_navigation-1.76.0.rb
foo = "foo"
bar = foo ? foo[0..1] : nil
$ cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"
gem "rubocop", "~> 1.76"

$ cat Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    ast (2.4.3)
    json (2.12.2)
    language_server-protocol (3.17.0.5)
    lint_roller (1.1.0)
    parallel (1.27.0)
    parser (3.3.8.0)
      ast (~> 2.4.1)
      racc
    prism (1.4.0)
    racc (1.8.1)
    rainbow (3.1.1)
    regexp_parser (2.10.0)
    rubocop (1.76.0)
      json (~> 2.3)
      language_server-protocol (~> 3.17.0.2)
      lint_roller (~> 1.1.0)
      parallel (~> 1.10)
      parser (>= 3.3.0.2)
      rainbow (>= 2.2.2, < 4.0)
      regexp_parser (>= 2.9.3, < 3.0)
      rubocop-ast (>= 1.45.0, < 2.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 2.4.0, < 4.0)
    rubocop-ast (1.45.0)
      parser (>= 3.3.7.2)
      prism (~> 1.4)
    ruby-progressbar (1.13.0)
    unicode-display_width (3.1.4)
      unicode-emoji (~> 4.0, >= 4.0.4)
    unicode-emoji (4.0.4)

PLATFORMS
  ruby

DEPENDENCIES
  rubocop (~> 1.76)

BUNDLED WITH
   2.6.8
$ bundle exec rubocop --debug --only Style/SafeNavigation safe_navigation-1.76.0.rb
For /Users/dentarg/code/rubocop-testing/SafeNavigation: Default configuration from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/dentarg/code/rubocop-testing/SafeNavigation/safe_navigation-1.76.0.rb
An error occurred while Style/SafeNavigation cop was inspecting /Users/dentarg/code/rubocop-testing/SafeNavigation/safe_navigation-1.76.0.rb:2:6.
/Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/corrector.rb:111:in 'RuboCop::Cop::Corrector#to_range': Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass (TypeError)
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/corrector.rb:120:in 'RuboCop::Cop::Corrector#check_range_validity'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:398:in 'Parser::Source::TreeRewriter#combine'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:207:in 'Parser::Source::TreeRewriter#wrap'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:231:in 'Parser::Source::TreeRewriter#insert_before'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/style/safe_navigation.rb:153:in 'block in RuboCop::Cop::Style::SafeNavigation#on_if'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/style/safe_navigation.rb:193:in 'block in RuboCop::Cop::Style::SafeNavigation#report_offense'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/base.rb:426:in 'RuboCop::Cop::Base#correct'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/base.rb:210:in 'RuboCop::Cop::Base#add_offense'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/style/safe_navigation.rb:185:in 'RuboCop::Cop::Style::SafeNavigation#report_offense'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/style/safe_navigation.rb:152:in 'RuboCop::Cop::Style::SafeNavigation#on_if'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:107:in 'Kernel#public_send'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:107:in 'block (2 levels) in RuboCop::Cop::Commissioner#trigger_responding_cops'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:171:in 'RuboCop::Cop::Commissioner#with_cop_error_handling'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:106:in 'block in RuboCop::Cop::Commissioner#trigger_responding_cops'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:105:in 'Array#each'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:105:in 'RuboCop::Cop::Commissioner#trigger_responding_cops'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:69:in 'RuboCop::Cop::Commissioner#on_if'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.0/lib/rubocop/ast/traversal.rb:144:in 'RuboCop::AST::Traversal#on_lvasgn'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_lvasgn'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.0/lib/rubocop/ast/traversal.rb:146:in 'block in RuboCop::AST::Traversal#on_dstr'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.0/lib/rubocop/ast/traversal.rb:146:in 'Array#each'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.0/lib/rubocop/ast/traversal.rb:146:in 'RuboCop::AST::Traversal#on_dstr'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_begin'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.0/lib/rubocop/ast/traversal.rb:20:in 'RuboCop::AST::Traversal#walk'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/commissioner.rb:87:in 'RuboCop::Cop::Commissioner#investigate'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/team.rb:174:in 'RuboCop::Cop::Team#investigate_partial'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cop/team.rb:108:in 'RuboCop::Cop::Team#investigate'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:348:in 'block in RuboCop::Runner#inspect_file'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:347:in 'Array#each'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:347:in 'Enumerable#flat_map'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:347:in 'RuboCop::Runner#inspect_file'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:290:in 'block in RuboCop::Runner#do_inspection_loop'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:324:in 'block in RuboCop::Runner#iterate_until_no_changes'
  from <internal:kernel>:168:in 'Kernel#loop'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:317:in 'RuboCop::Runner#iterate_until_no_changes'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:286:in 'RuboCop::Runner#do_inspection_loop'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:167:in 'block in RuboCop::Runner#file_offenses'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:192:in 'RuboCop::Runner#file_offense_cache'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:166:in 'RuboCop::Runner#file_offenses'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:154:in 'RuboCop::Runner#process_file'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:135:in 'block in RuboCop::Runner#each_inspected_file'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:134:in 'Array#each'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:134:in 'Enumerable#reduce'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:134:in 'RuboCop::Runner#each_inspected_file'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:120:in 'RuboCop::Runner#inspect_files'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/runner.rb:73:in 'RuboCop::Runner#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/command/execute_runner.rb:26:in 'block in RuboCop::CLI::Command::ExecuteRunner#execute_runner'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/command/execute_runner.rb:52:in 'RuboCop::CLI::Command::ExecuteRunner#with_redirect'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/command/execute_runner.rb:25:in 'RuboCop::CLI::Command::ExecuteRunner#execute_runner'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/command/execute_runner.rb:17:in 'RuboCop::CLI::Command::ExecuteRunner#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/command.rb:11:in 'RuboCop::CLI::Command.run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli/environment.rb:18:in 'RuboCop::CLI::Environment#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli.rb:122:in 'RuboCop::CLI#run_command'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli.rb:129:in 'RuboCop::CLI#execute_runners'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli.rb:51:in 'block in RuboCop::CLI#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli.rb:81:in 'RuboCop::CLI#profile_if_needed'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/lib/rubocop/cli.rb:43:in 'RuboCop::CLI#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/exe/rubocop:19:in '<top (required)>'
  from /Users/dentarg/.arm64_rubies/3.4.3/bin/rubocop:25:in 'Kernel#load'
  from /Users/dentarg/.arm64_rubies/3.4.3/bin/rubocop:25:in '<top (required)>'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli/exec.rb:59:in 'Kernel.load'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli/exec.rb:59:in 'Bundler::CLI::Exec#kernel_load'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli/exec.rb:23:in 'Bundler::CLI::Exec#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli.rb:452:in 'Bundler::CLI#exec'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/vendor/thor/lib/thor/command.rb:28:in 'Bundler::Thor::Command#run'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in 'Bundler::Thor::Invocation#invoke_command'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/vendor/thor/lib/thor.rb:538:in 'Bundler::Thor.dispatch'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli.rb:35:in 'Bundler::CLI.dispatch'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/vendor/thor/lib/thor/base.rb:584:in 'Bundler::Thor::Base::ClassMethods#start'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/cli.rb:29:in 'Bundler::CLI.start'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/bundler-2.6.8/exe/bundle:28:in 'block in <top (required)>'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/site_ruby/3.4.0/bundler/friendly_errors.rb:117:in 'Bundler.with_friendly_errors'
  from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/bundler-2.6.8/exe/bundle:20:in '<top (required)>'
  from /Users/dentarg/.arm64_rubies/3.4.3/bin/bundle:25:in 'Kernel#load'
  from /Users/dentarg/.arm64_rubies/3.4.3/bin/bundle:25:in '<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Style/SafeNavigation cop was inspecting /Users/dentarg/code/rubocop-testing/SafeNavigation/safe_navigation-1.76.0.rb:2:6.
configuration from /Users/dentarg/.arm64_rubies/3.4.3/lib/ruby/gems/3.4.0/gems/rubocop-1.76.0/config/default.yml
Errors are usually caused by RuboCop bugs.
Please, update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.76.0 (using Parser 3.3.8.0, Prism 1.4.0, default, rubocop-ast 1.45.0, analyzing as Ruby 3.4, running on ruby 3.4.3) [arm64-darwin23]
Finished in 0.07380400015972555 seconds

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions