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
Autocorrection of Lint/LiteralInInterpolation changes code behaviour in regexes #6822
Comments
Does anyone have thoughts on the best way to fix this? Ignore the offenses if they are an argument to |
I think that there is no need to treat The problem is that auto-correcting results in wrong value replacing; like I think that RuboCop::Cop::Util#escape_string will fit to fix that... --- a/lib/rubocop/cop/lint/literal_in_interpolation.rb
+++ b/lib/rubocop/cop/lint/literal_in_interpolation.rb
@@ -55,7 +55,7 @@ module RuboCop
when :float
node.children.last.to_f.to_s
when :str
- node.children.last
+ escape_string(node.children.last)
when :sym
autocorrected_value_for_symbol(node)
else I will investigate later. Escaping is difficult... |
Sorry, I found that it breaks other behaviors. 😭 Forget that... memo:"foo#{"\n"}bar" now latest rubocop (v0.67.2) auto-corrects to that "foo
bar" but using "foo\nbar" |
But I believe that the current behavior below is simply a bug. # a.rb
"foo#{'\n'}bar" rubocop (v0.67.2) corrects
results in: # a.rb
"foo\nbar" I want to fix this problem. |
… single quotes ## Current behavior: Doing `$ rubocop --only Lint/LiteralInInterpolation`; source: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F") "foo#{'\n'}bar" "this is #{'"'} silly" ``` corrected to: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D\2F") "foo\nbar" "this is " silly" ``` ## New behavior: corrected to: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D\\2F") "foo\\nbar" "this is \" silly" ```
… quotes (#6979) ## Current behavior: Doing `$ rubocop --only Lint/LiteralInInterpolation`; source: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F") "foo#{'\n'}bar" "this is #{'"'} silly" ``` corrected to: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D\2F") "foo\nbar" "this is " silly" ``` ## New behavior: corrected to: ```ruby x = "ABC".gsub(/(A)(B)(C)/, "D\\2F") "foo\\nbar" "this is \" silly" ```
@hoshinotsuyoshi you are a true pillar of the community. Thank you for fixing this! |
Rubocop generally auto-replaces
"#{'a'}"
with"a"
as a response to the "Lint/LiteralInInterpolation: Literal interpolation detected." offence. However, when it comes to regex capture group literals, this changes the behaviour of the code, making this auto-correction unsafeExpected behavior
When running
rubocop -a
on this line of code:The resulting code should look like:
In both cases, the value of
x
is"DBF"
Actual behavior
When running
rubocop -a
on this line of code:The resulting code actually looks like:
The value of
x
is now"D\u0002F"
Steps to reproduce the problem
Put this in a file:
run
rubocop -a /path/to/file.rb
RuboCop version
The text was updated successfully, but these errors were encountered: