KEMBAR78
`Lint/EmptyInterpolation` false positive · Issue #14605 · rubocop/rubocop · GitHub
Skip to content

Lint/EmptyInterpolation false positive #14605

@darronschall

Description

@darronschall

Lint/EmptyInterpolation does not understand that being inside %W affects behavior; Auto-correct changes program semantics:

3.4.6 :001 > %W[#{''} test]
 => ["", "test"]
3.4.6 :002 > %W[ test]
 => ["test"]

Steps to reproduce the problem

# in example.rb

def foo
  parts = %W[#{""} one two]
  raise StandardError if parts.length < 3
  puts "done"
end

foo

Running ruby example.rb prints "done" to the console.

Expected behavior

Running rubocop --only Lint/EmptyInterpolation --autocorrect example.rb should not change the file.

Actual behavior

Inspecting 1 file
W

Offenses:

example.rb:2:14: W: [Corrected] Lint/EmptyInterpolation: Empty interpolation detected.
  parts = %W[#{""} one two]
             ^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

The file is incorrectly changed to:

def foo
  parts = %W[ one two]
  raise StandardError if parts.length < 3
  puts "done"
end

foo

Running ruby example.rb now errors:

example.rb:3:in 'Object#foo': StandardError (StandardError)
	from example.rb:7:in '<main>'

RuboCop version

rubocop -V
1.80.2 (using Parser 3.3.9.0, rubocop-ast 1.47.1, analyzing as Ruby 2.7, running on ruby 3.4.6) [arm64-darwin24]

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