Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autocorrecting Style/RedundantStringEscape changes program behavior if there are two redundant escapes #11309

Closed
be-ragheb opened this issue Dec 20, 2022 · 1 comment · Fixed by #11310
Labels

Comments

@be-ragheb
Copy link

When Style/RedundantStringEscape sees a string like this

"\#\{foo}"

it identifies both \# and \{ as redundant string escapes, and if asked to autocorrect, will unescape both, changing the meaning of the program.


Expected behavior

Because Style/RedundantStringEscape promises safe autocorrection, I would expect that it would change the offending string to either"#\{foo}" (removing first slash) or "\#{foo}" (removing second slash).

Actual behavior

It actually removes both, resulting in #{foo}. This causes the program to interpolate the value of foo.

Steps to reproduce the problem

Assuming RuboCop is installed, this script will demonstrate the problem:

#!/bin/bash -x

echo 'puts "\#\{foo}"' > demo.rb
ruby demo.rb
rubocop --debug --autocorrect --only Style/RedundantStringEscape demo.rb
ruby demo.rb

Here is the output:

+ echo 'puts "\#\{foo}"'
+ ruby demo.rb
#{foo}
+ rubocop --debug --autocorrect --only Style/RedundantStringEscape demo.rb
For /Users/benragheb/Projects/rubocop-bug: Default configuration from /Users/benragheb/.rvm/gems/ruby-2.6.10/gems/rubocop-1.41.0/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/benragheb/Projects/rubocop-bug/demo.rb
Loading cache from /Users/benragheb/.cache/rubocop_cache/5dc345701efc12c82aae8fbc7c2d51bbefd152fa/05097446ed3d3d6bb5a46aa88fe4d53f18d298af/5c20c30dd0b384e0122a1414df3f4ffec395ac2c
C

Offenses:

demo.rb:1:7: C: [Corrected] Style/RedundantStringEscape: Redundant escape of # inside string literal.
puts "\#\{foo}"
      ^^
demo.rb:1:9: C: [Corrected] Style/RedundantStringEscape: Redundant escape of { inside string literal.
puts "\#\{foo}"
        ^^

1 file inspected, 2 offenses detected, 2 offenses corrected
Finished in 0.1450700000859797 seconds
+ ruby demo.rb
demo.rb:1:in `<main>': undefined local variable or method `foo' for main:Object (NameError)

RuboCop version

1.41.0 (using Parser 3.1.3.0, rubocop-ast 1.24.0, running on ruby 2.6.10) [x86_64-darwin21]
@koic koic added the bug label Dec 21, 2022
koic added a commit to koic/rubocop that referenced this issue Dec 21, 2022
…cape`

Fixes rubocop#11309.

This PR fixes a false positive for `Style/RedundantStringEscape`
when using a redundant escaped string interpolation `\#\{foo}`.
bbatsov pushed a commit that referenced this issue Dec 21, 2022
Fixes #11309.

This PR fixes a false positive for `Style/RedundantStringEscape`
when using a redundant escaped string interpolation `\#\{foo}`.
@be-ragheb
Copy link
Author

@koic Thank you for addressing this so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants