Style/RedundantInterpolation
auto-correct should not be marked safe or should be implemented differently
#10656
Labels
The
Style/RedundantInterpolation
cop's auto-correct should not be marked safe because it cannot guarantee that subsequent code wasn't relying on the "redundant" interpolation essentially dup'ing the string.I've put together a brief demo of two scenarios where replacing the interpolation with
to_s
fails to produce equivalent code: https://github.com/jimryan/rubocop-redundant-interpolation-unsafe-autocorrect-demoAn alternative solution to not marking as safe would be to replace redundant string interpolations with
.to_s.dup
which is more precisely what the redundant interpolation was doing. E.g."#{foo}"
becomesfoo.to_s.dup
. I believe that would be safe.Expected behavior
Autocorrection should not be marked "safe" for this cop. I would not expect the potential for an error or change in behavior of any kind after running a safe auto-correction.
Alternative behavior could be to replace redundant string interpolations with
to_s.dup
.Actual behavior
It is marked safe, yet safe autocorrection can break/change code in some scenarios.
Steps to reproduce the problem
Steps are in the
README
of the demo repo, but in general:Style/RedundantInterpolation
will replace that interpolation with.to_s
on the original string, which just returns the same String object. Now the mutation is mutating the original string (the one that was interpolated), and not a copy of it. When a string is the subject of the redundant interpolation, the interpolation is equivalent todup
, notto_s
.RuboCop version
The text was updated successfully, but these errors were encountered: