Autocorrect should turn a %W literal with items including string interpolation into a [] literal when Style/WordArray.EnforcedStyle: brackets is set.
Rubocop reports an error while executing the Style/WordArray cop.
I have a minimum working example to reproduce the behaviour:
.rubocop.yml
Style/WordArray:
EnforcedStyle: brackets
example.rb
foo = 'foo'
%W[#{foo}bar]
The output of rubocop -da example.rb is:
For /Users/luka/code/rubocop_bug: configuration from /Users/luka/code/rubocop_bug/.rubocop.yml
Default configuration from /Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/config/default.yml
Inheriting configuration from /Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/config/enabled.yml
Inheriting configuration from /Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/config/disabled.yml
Inspecting 1 file
Scanning /Users/luka/code/rubocop_bug/example.rb
An error occurred while Style/WordArray cop was inspecting /Users/luka/code/rubocop_bug/example.rb:2:0.
undefined method `gsub' for "s(:begin,\n s(:lvar, :foo))":RuboCop::AST::Node
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/util.rb:104:in `to_string_literal'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/style/word_array.rb:84:in `block in correct_bracketed'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/style/word_array.rb:84:in `map'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/style/word_array.rb:84:in `correct_bracketed'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/style/word_array.rb:58:in `autocorrect'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/cop.rb:153:in `correct'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/cop.rb:131:in `add_offense'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/mixin/percent_array.rb:41:in `check_percent_array'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/style/word_array.rb:50:in `on_array'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:57:in `block (2 levels) in trigger_responding_cops'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:105:in `with_cop_error_handling'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:56:in `block in trigger_responding_cops'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:55:in `each'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:55:in `trigger_responding_cops'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:33:in `block (2 levels) in <class:Commissioner>'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/ast/traversal.rb:49:in `block in on_begin'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/ast/traversal.rb:49:in `each'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/ast/traversal.rb:49:in `on_begin'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:34:in `block (2 levels) in <class:Commissioner>'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/ast/traversal.rb:12:in `walk'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/commissioner.rb:45:in `investigate'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/team.rb:114:in `investigate'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/team.rb:94:in `offenses'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:265:in `inspect_file'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:212:in `block in do_inspection_loop'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:244:in `block in iterate_until_no_changes'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:237:in `loop'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:237:in `iterate_until_no_changes'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:208:in `do_inspection_loop'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:111:in `block in file_offenses'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:129:in `file_offense_cache'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:109:in `file_offenses'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:100:in `process_file'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:75:in `each'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:75:in `reduce'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:75:in `each_inspected_file'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:67:in `inspect_files'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/runner.rb:39:in `run'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cli.rb:157:in `execute_runner'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cli.rb:85:in `execute_runners'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/lib/rubocop/cli.rb:41:in `run'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/exe/rubocop:13:in `block in <top (required)>'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
/Users/luka/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.2/exe/rubocop:12:in `<top (required)>'
/Users/luka/.rbenv/versions/2.5.1/bin/rubocop:23:in `load'
/Users/luka/.rbenv/versions/2.5.1/bin/rubocop:23:in `<main>'
.
1 file inspected, no offenses detected
1 error occurred:
An error occurred while Style/WordArray cop was inspecting /Users/luka/code/rubocop_bug/example.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.58.2 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin16)
Finished in 0.3419160000048578 seconds
~/c/rubocop_bug $ which rubocop
/Users/luka/.rbenv/shims/rubocop
$ rubocop -V
0.58.2 (using Parser 2.5.1.2, running on ruby 2.6.0 x86_64-darwin16)
Thanks for the feedback. I confirmed this reproduction and I opened a PR #6241.
Most helpful comment
Thanks for the feedback. I confirmed this reproduction and I opened a PR #6241.