You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When both the ConstantRegexp and RegexpMatch cops have violations on the same line, the ConstantRegexp cop may make multiple corrections on the same line, breaking existing code.
# rubocop test.rb
Inspecting 1 file
C
Offenses:
test.rb:3:4: C: [Correctable] Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
if content =~ /#{CONSTANT}.*\n/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:3:15: C: [Correctable] Performance/ConstantRegexp: Extract this regexp into a constant, memoize it, or append an /o option to its options.
if content =~ /#{CONSTANT}.*\n/
^^^^^^^^^^^^^^^^^
1 file inspected, 2 offenses detected, 2 offenses autocorrectable
This is all as expected.
Running this with the --autocorrect/-a flag, however, generates a different report.
# rubocop -a test.rb
Inspecting 1 file
C
Offenses:
test.rb:3:4: C: [Corrected] Performance/ConstantRegexp: Extract this regexp into a constant, memoize it, or append an /o option to its options.
if /#{CONSTANT}.*\n/.match?(contento)
^^^^^^^^^^^^^^^^^
test.rb:3:4: C: [Corrected] Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
if content =~ /#{CONSTANT}.*\n/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:3:15: C: [Corrected] Performance/ConstantRegexp: Extract this regexp into a constant, memoize it, or append an /o option to its options.
if content =~ /#{CONSTANT}.*\n/
^^^^^^^^^^^^^^^^^
1 file inspected, 3 offenses detected, 3 offenses corrected
Note that the bottom-most entry reports an issue pre-RegexpMatch, and the top-most entry reports post-RegexpMatch. Also note that the top-most entry is matching against contento (not content). The resulting file is as follows:
When both the ConstantRegexp and RegexpMatch cops have violations on the same line, the ConstantRegexp cop may make multiple corrections on the same line, breaking existing code.
Steps to reproduce the problem
Given this configuration:
And given this source file:
Rubocop describes the following offenses:
This is all as expected.
Running this with the
--autocorrect/-a
flag, however, generates a different report.Note that the bottom-most entry reports an issue pre-
RegexpMatch
, and the top-most entry reports post-RegexpMatch
. Also note that the top-most entry is matching againstcontento
(notcontent
). The resulting file is as follows:Since the
content
variable has been changed tocontento
, the autocorrected file has semantically changed.RuboCop version
The text was updated successfully, but these errors were encountered: