Skip to content

Invalid autocorrection of Style/ItBlockParameter #14336

@nevans

Description

@nevans

rubocop incorrectly identifies and incorrectly autocorrects the following code:

# frozen_string_literal: true

##############################################################################
# Good examples: rubocop v1.77.0 gets these right:

foo { transform1 _1 }         # rubocop correctly updates this block
foo { transform2 _1, _2 }     # rubocop correctly ignores this block

foo { transform2 _1, _2 }     # rubocop correctly ignores this block
  .bar { transform2 _1, _2 }  # rubocop correctly ignores this block

foo { transform1 _1 }         # rubocop correctly updates this block
  .bar { transform2 _1, _2 }  # rubocop correctly ignores this block

foo { transform2 _1, _2 }     # rubocop correctly ignores this block
  .intervening
  .methods(calls) { etc }
  .bar { transform2 _1, _2 }  # rubocop correctly ignores this block

##############################################################################
# Bad examples: rubocop v1.77.0 gets these wrong:

foo { transform2 _1, _2 }     # rubocop INCORRECTLY updates this block <-- BUG
  .bar { transform1 _1 }      # rubocop correctly   updates this block

foo { transform1 _1 }         # rubocop correctly   updates this block
  .baz { transform2 _1, _2 }  # rubocop INCORRECTLY updates this block <-- BUG
  .qux { transform1 _1 }      # rubocop correctly   updates this block

foo { transform2 _1, _2 }     # rubocop INCORRECTLY updates this block <-- BUG
  .intervening
  .methods(calls) { etc }
  .bar { transform1 _1 }      # rubocop correctly   updates this block

Use the following config to reproduce:

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.4

Expected behavior

In the example above, the lines commented <-- BUG should not be identified as offences nor autocorrected.

Actual behavior

The example lines will be identified as errors and autocorrected. This creates syntax errors on the lines which should not have been changed.

For example:

foo { transform2 it, _2 }  # <-- SyntaxError
  .bar { transform1 it }

Steps to reproduce the problem

Use the .rubocop.yml and example.rb files, provided above. Run rubocop example.rb and rubocop -a example.rb

This happens with both RUBYOPT=--parser=parse.y and RUBYOPT=--parser=prism. (I don't know if that makes any difference elsewhere, but it also doesn't make any difference here.)

RuboCop version

$ rubocop -V
1.77.0 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.4, running on ruby 3.4.4) [x86_64-linux
]

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