KEMBAR78
Fix false positives for `Style/RedundantInterpolation` by koic · Pull Request #14574 · rubocop/rubocop · GitHub
Skip to content

Conversation

@koic
Copy link
Member

@koic koic commented Oct 1, 2025

This PR fixes the following false positive for Style/RedundantInterpolation when using a one-line => pattern matching:

$ echo '"#{42 => var}"' | RUBOCOP_TARGET_RUBY_VERSION=3.0 bundle exec rubocop --stdin example.rb -A --only Style/RedundantInterpolation
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/RedundantInterpolation: Prefer to_s over string interpolation.
"#{42 => var}"
^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
(42 => var).to_s

This detection results in the following syntax error:

$ ruby -cve '(42 => var).to_s'
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
ruby: -e:1: syntax error found (SyntaxError)
> 1 | (42 => var).to_s
    |  ^~~~~~~~~ unexpected void value expression

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

This PR fixes the following false positive for `Style/RedundantInterpolation`
when using a one-line `=>` pattern matching:

```console
$ echo '"#{42 => var}"' | RUBOCOP_TARGET_RUBY_VERSION=3.0 bundle exec rubocop --stdin example.rb -A --only Style/RedundantInterpolation
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/RedundantInterpolation: Prefer to_s over string interpolation.
"#{42 => var}"
^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
(42 => var).to_s
```

This detection results in the following syntax error:

```console
$ ruby -cve '(42 => var).to_s'
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
ruby: -e:1: syntax error found (SyntaxError)
> 1 | (42 => var).to_s
    |  ^~~~~~~~~ unexpected void value expression
```
@koic koic merged commit 729dc4a into rubocop:master Oct 1, 2025
22 checks passed
@koic koic deleted the fix_false_positive_for_style_redundant_interpolation branch October 1, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant