You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rubocop多行数组缩进/布局规则触发无限循环错误求助

Rubocop多行数组自动格式化触发无限循环错误

使用Rubocop对多行数组执行自动格式化时,在1.31.2和1.59.0版本中均触发无限循环错误。尝试调整rubocop.yml中的布局规则(启用/禁用相关属性),问题仍未解决。但相同场景下的哈希自动格式化可正常完成。


我的Rubocop配置(rubocop.yml)

Layout/FirstArrayElementIndentation:
  Enabled: true
Layout/FirstArrayElementLineBreak:
  Enabled: true
Layout/ArrayAlignment:
  EnforcedStyle: with_fixed_indentation
Layout/MultilineArrayBraceLayout:
  EnforcedStyle: new_line

AllCops:
  NewCops: enable

测试数组代码(robo.rb)

# frozen_string_literal: true

# test an issue with robocop rules
class RoboTest
  def initialize
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
  end
end

未启用自动修复的Rubocop检查结果

bash-4.2$ rubocop robo.rb
Inspecting 1 file
C

违规项:

robo.rb:6:121: C: [可自动修复] Layout/LineLength: 行过长。[135/120]
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
                                                                                                                        ^^^^^^^^^^^^^^^
robo.rb:6:133: C: [可自动修复] Layout/SpaceInsideArrayLiteralBrackets: 数组方括号内不要留空格。
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
                                                                                                                                    ^

检查1个文件,发现2项违规,2项可自动修复

启用自动修复时触发错误

bash-4.2$ rubocop -a robo.rb
Inspecting 1 file
C

违规项:

robo.rb:7:7: C: [已修复] Layout/FirstArrayElementIndentation: 数组内缩进使用2个空格,相对于左括号后的第一个位置。
      'aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs',
      ^^^^^^^^^^^^^
robo.rb:7:12: C: [已修复] Layout/ArrayAlignment: 多行数组中,第一行之后的元素使用一级缩进。
           'aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs',
           ^^^^^^^^^^^^^

0个文件被检查,发现2项违规,2项已修复
在/home/me/mrt-cron/coll-health-obj-analysis/robo.rb中检测到无限循环,由Layout/FirstArrayElementIndentation -> Layout/ArrayAlignment导致
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:296:in `check_for_infinite_loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:279:in `block in iterate_until_no_changes'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:278:in `loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:278:in `iterate_until_no_changes'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:247:in `do_inspection_loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:130:in `block in file_offenses'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:155:in `file_offense_cache'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:129:in `file_offenses'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:120:in `process_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `each'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `reduce'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `each_inspected_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:86:in `inspect_files'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:47:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command.rb:11:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/environment.rb:18:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:71:in `run_command'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:78:in `execute_runners'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:47:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/exe/rubocop:19:in `block in <top (required)>'
/usr/share/ruby/benchmark.rb:308:in `realtime'
/home/me/.gem/ruby/gems/rubocop-1.31.2/exe/rubocop:19:in `<top (required)>'
/home/me/bin/rubocop:23:in `load'
/home/me/bin/rubocop:23:in `<main>'

哈希格式化无此问题

在rubocop.yml中添加以下哈希规则:

Layout/FirstHashElementIndentation:
  Enabled: true
  EnforcedStyle: consistent
Layout/MultilineHashKeyLineBreaks:
  Enabled: true
Layout/MultilineHashBraceLayout:
  EnforcedStyle: new_line

测试哈希代码(robohash.rb)

# frozen_string_literal: true

# test an issue with robocop rules
class RoboHashTest
  def initialize
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
  end
end

哈希自动修复结果

bash-4.2$ rubocop -a robohash.rb
Inspecting 1 file
C

违规项:

robohash.rb:6:10: C: [已修复] Layout/SpaceInsideHashLiteralBraces: 左大括号内缺少空格。
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
         ^
robohash.rb:6:99: C: [已修复] Layout/TrailingWhitespace: 检测到尾随空格。
    puts({ cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', 
                                                                                                  ^
robohash.rb:6:121: C: [已修复] Layout/LineLength: 行过长。[158/120]
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
                                                                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
robohash.rb:7:1: C: [已修复] Layout/HashAlignment: 若哈希字面量跨多行,对齐所有键。
count: 'one two three four five six seven eight nine ten' })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

检查1个文件,发现4项违规,4项已修复

内容的提问来源于stack exchange,提问作者terrywb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 02:35:01