Skip to content

Commit ed9942f

Browse files
authored
Merge pull request #2769 from MikeMcQuaid/rubocop-no-perl-backrefs
rubocop: don’t allow Perl regex backrefs.
2 parents 22c431d + 7a0aff1 commit ed9942f

File tree

21 files changed

+83
-87
lines changed

21 files changed

+83
-87
lines changed

Library/.rubocop.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ Style/PercentLiteralDelimiters:
172172
'%W': '[]'
173173
'%x': '()'
174174

175-
# we prefer Perl-style regex back references
176-
Style/PerlBackrefs:
177-
Enabled: false
178-
179175
Style/RaiseArgs:
180176
EnforcedStyle: exploded
181177

Library/Homebrew/cmd/info.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def print_json
7979

8080
def github_remote_path(remote, path)
8181
if remote =~ %r{^(?:https?://|git(?:@|://))github\.com[:/](.+)/(.+?)(?:\.git)?$}
82-
"https://github.com/#{$1}/#{$2}/blob/master/#{path}"
82+
"https://github.com/#{Regexp.last_match(1)}/#{Regexp.last_match(2)}/blob/master/#{path}"
8383
else
8484
"#{remote}/#{path}"
8585
end

Library/Homebrew/cmd/install.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def install
8080
if name !~ HOMEBREW_TAP_FORMULA_REGEX && name !~ HOMEBREW_CASK_TAP_CASK_REGEX
8181
next
8282
end
83-
tap = Tap.fetch($1, $2)
83+
tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2))
8484
tap.install unless tap.installed?
8585
end
8686
end

Library/Homebrew/cmd/search.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def search
9393

9494
def query_regexp(query)
9595
case query
96-
when %r{^/(.*)/$} then Regexp.new($1)
96+
when %r{^/(.*)/$} then Regexp.new(Regexp.last_match(1))
9797
else /.*#{Regexp.escape(query)}.*/i
9898
end
9999
rescue RegexpError

Library/Homebrew/dev-cmd/audit.rb

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def audit_options
578578

579579
next unless o.name =~ /^with(out)?-(?:checks?|tests)$/
580580
unless formula.deps.any? { |d| d.name == "check" && (d.optional? || d.recommended?) }
581-
problem "Use '--with#{$1}-test' instead of '--#{o.name}'. Migrate '--#{o.name}' with `deprecated_option`."
581+
problem "Use '--with#{Regexp.last_match(1)}-test' instead of '--#{o.name}'. Migrate '--#{o.name}' with `deprecated_option`."
582582
end
583583
end
584584

@@ -722,7 +722,7 @@ def audit_specs
722722
stable = formula.stable
723723
case stable && stable.url
724724
when /[\d\._-](alpha|beta|rc\d)/
725-
matched = $1
725+
matched = Regexp.last_match(1)
726726
version_prefix = stable.version.to_s.sub(/\d+$/, "")
727727
return if unstable_whitelist.include?([formula.name, version_prefix])
728728
problem "Stable version URLs should not contain #{matched}"
@@ -837,7 +837,7 @@ def patch_problems(patch)
837837
when %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
838838
problem <<-EOS.undent
839839
use GitHub pull request URLs:
840-
https://github.com/#{$1}/#{$2}/pull/#{$3}.patch
840+
https://github.com/#{Regexp.last_match(1)}/#{Regexp.last_match(2)}/pull/#{Regexp.last_match(3)}.patch
841841
Rather than patch-diff:
842842
#{patch.url}
843843
EOS
@@ -875,7 +875,7 @@ def audit_lines
875875

876876
def line_problems(line, _lineno)
877877
if line =~ /<(Formula|AmazonWebServicesFormula|ScriptFileFormula|GithubGistFormula)/
878-
problem "Use a space in class inheritance: class Foo < #{$1}"
878+
problem "Use a space in class inheritance: class Foo < #{Regexp.last_match(1)}"
879879
end
880880

881881
# Commented-out cmake support from default template
@@ -899,52 +899,52 @@ def line_problems(line, _lineno)
899899
# FileUtils is included in Formula
900900
# encfs modifies a file with this name, so check for some leading characters
901901
if line =~ %r{[^'"/]FileUtils\.(\w+)}
902-
problem "Don't need 'FileUtils.' before #{$1}."
902+
problem "Don't need 'FileUtils.' before #{Regexp.last_match(1)}."
903903
end
904904

905905
# Check for long inreplace block vars
906906
if line =~ /inreplace .* do \|(.{2,})\|/
907-
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{$1}|\"."
907+
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{Regexp.last_match(1)}|\"."
908908
end
909909

910910
# Check for string interpolation of single values.
911911
if line =~ /(system|inreplace|gsub!|change_make_var!).*[ ,]"#\{([\w.]+)\}"/
912-
problem "Don't need to interpolate \"#{$2}\" with #{$1}"
912+
problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}"
913913
end
914914

915915
# Check for string concatenation; prefer interpolation
916916
if line =~ /(#\{\w+\s*\+\s*['"][^}]+\})/
917-
problem "Try not to concatenate paths in string interpolation:\n #{$1}"
917+
problem "Try not to concatenate paths in string interpolation:\n #{Regexp.last_match(1)}"
918918
end
919919

920920
# Prefer formula path shortcuts in Pathname+
921921
if line =~ %r{\(\s*(prefix\s*\+\s*(['"])(bin|include|libexec|lib|sbin|share|Frameworks)[/'"])}
922-
problem "\"(#{$1}...#{$2})\" should be \"(#{$3.downcase}+...)\""
922+
problem "\"(#{Regexp.last_match(1)}...#{Regexp.last_match(2)})\" should be \"(#{Regexp.last_match(3).downcase}+...)\""
923923
end
924924

925925
if line =~ /((man)\s*\+\s*(['"])(man[1-8])(['"]))/
926-
problem "\"#{$1}\" should be \"#{$4}\""
926+
problem "\"#{Regexp.last_match(1)}\" should be \"#{Regexp.last_match(4)}\""
927927
end
928928

929929
# Prefer formula path shortcuts in strings
930930
if line =~ %r[(\#\{prefix\}/(bin|include|libexec|lib|sbin|share|Frameworks))]
931-
problem "\"#{$1}\" should be \"\#{#{$2.downcase}}\""
931+
problem "\"#{Regexp.last_match(1)}\" should be \"\#{#{Regexp.last_match(2).downcase}}\""
932932
end
933933

934934
if line =~ %r[((\#\{prefix\}/share/man/|\#\{man\}/)(man[1-8]))]
935-
problem "\"#{$1}\" should be \"\#{#{$3}}\""
935+
problem "\"#{Regexp.last_match(1)}\" should be \"\#{#{Regexp.last_match(3)}}\""
936936
end
937937

938938
if line =~ %r[((\#\{share\}/(man)))[/'"]]
939-
problem "\"#{$1}\" should be \"\#{#{$3}}\""
939+
problem "\"#{Regexp.last_match(1)}\" should be \"\#{#{Regexp.last_match(3)}}\""
940940
end
941941

942942
if line =~ %r[(\#\{prefix\}/share/(info|man))]
943-
problem "\"#{$1}\" should be \"\#{#{$2}}\""
943+
problem "\"#{Regexp.last_match(1)}\" should be \"\#{#{Regexp.last_match(2)}}\""
944944
end
945945

946946
if line =~ /depends_on :(automake|autoconf|libtool)/
947-
problem ":#{$1} is deprecated. Usage should be \"#{$1}\""
947+
problem ":#{Regexp.last_match(1)} is deprecated. Usage should be \"#{Regexp.last_match(1)}\""
948948
end
949949

950950
if line =~ /depends_on :apr/
@@ -954,23 +954,23 @@ def line_problems(line, _lineno)
954954
problem ":tex is deprecated" if line =~ /depends_on :tex/
955955

956956
if line =~ /depends_on\s+['"](.+)['"]\s+=>\s+:(lua|perl|python|ruby)(\d*)/
957-
problem "#{$2} modules should be vendored rather than use deprecated `depends_on \"#{$1}\" => :#{$2}#{$3}`"
957+
problem "#{Regexp.last_match(2)} modules should be vendored rather than use deprecated `depends_on \"#{Regexp.last_match(1)}\" => :#{Regexp.last_match(2)}#{Regexp.last_match(3)}`"
958958
end
959959

960960
if line =~ /depends_on\s+['"](.+)['"]\s+=>\s+(.*)/
961-
dep = $1
962-
$2.split(" ").map do |o|
961+
dep = Regexp.last_match(1)
962+
Regexp.last_match(2).split(" ").map do |o|
963963
break if ["if", "unless"].include?(o)
964964
next unless o =~ /^\[?['"](.*)['"]/
965-
problem "Dependency #{dep} should not use option #{$1}"
965+
problem "Dependency #{dep} should not use option #{Regexp.last_match(1)}"
966966
end
967967
end
968968

969969
# Commented-out depends_on
970-
problem "Commented-out dep #{$1}" if line =~ /#\s*depends_on\s+(.+)\s*$/
970+
problem "Commented-out dep #{Regexp.last_match(1)}" if line =~ /#\s*depends_on\s+(.+)\s*$/
971971

972972
if line =~ /if\s+ARGV\.include\?\s+'--(HEAD|devel)'/
973-
problem "Use \"if build.#{$1.downcase}?\" instead"
973+
problem "Use \"if build.#{Regexp.last_match(1).downcase}?\" instead"
974974
end
975975

976976
problem "Use separate make calls" if line.include?("make && make")
@@ -983,15 +983,15 @@ def line_problems(line, _lineno)
983983

984984
# Avoid hard-coding compilers
985985
if line =~ %r{(system|ENV\[.+\]\s?=)\s?['"](/usr/bin/)?(gcc|llvm-gcc|clang)['" ]}
986-
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{$3}\""
986+
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{Regexp.last_match(3)}\""
987987
end
988988

989989
if line =~ %r{(system|ENV\[.+\]\s?=)\s?['"](/usr/bin/)?((g|llvm-g|clang)\+\+)['" ]}
990-
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{$3}\""
990+
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{Regexp.last_match(3)}\""
991991
end
992992

993993
if line =~ /system\s+['"](env|export)(\s+|['"])/
994-
problem "Use ENV instead of invoking '#{$1}' to modify the environment"
994+
problem "Use ENV instead of invoking '#{Regexp.last_match(1)}' to modify the environment"
995995
end
996996

997997
if formula.name != "wine" && line =~ /ENV\.universal_binary/
@@ -1007,27 +1007,27 @@ def line_problems(line, _lineno)
10071007
end
10081008

10091009
if line =~ /build\.include\?[\s\(]+['"]\-\-(.*)['"]/
1010-
problem "Reference '#{$1}' without dashes"
1010+
problem "Reference '#{Regexp.last_match(1)}' without dashes"
10111011
end
10121012

10131013
if line =~ /build\.include\?[\s\(]+['"]with(out)?-(.*)['"]/
1014-
problem "Use build.with#{$1}? \"#{$2}\" instead of build.include? 'with#{$1}-#{$2}'"
1014+
problem "Use build.with#{Regexp.last_match(1)}? \"#{Regexp.last_match(2)}\" instead of build.include? 'with#{Regexp.last_match(1)}-#{Regexp.last_match(2)}'"
10151015
end
10161016

10171017
if line =~ /build\.with\?[\s\(]+['"]-?-?with-(.*)['"]/
1018-
problem "Don't duplicate 'with': Use `build.with? \"#{$1}\"` to check for \"--with-#{$1}\""
1018+
problem "Don't duplicate 'with': Use `build.with? \"#{Regexp.last_match(1)}\"` to check for \"--with-#{Regexp.last_match(1)}\""
10191019
end
10201020

10211021
if line =~ /build\.without\?[\s\(]+['"]-?-?without-(.*)['"]/
1022-
problem "Don't duplicate 'without': Use `build.without? \"#{$1}\"` to check for \"--without-#{$1}\""
1022+
problem "Don't duplicate 'without': Use `build.without? \"#{Regexp.last_match(1)}\"` to check for \"--without-#{Regexp.last_match(1)}\""
10231023
end
10241024

10251025
if line =~ /unless build\.with\?(.*)/
1026-
problem "Use if build.without?#{$1} instead of unless build.with?#{$1}"
1026+
problem "Use if build.without?#{Regexp.last_match(1)} instead of unless build.with?#{Regexp.last_match(1)}"
10271027
end
10281028

10291029
if line =~ /unless build\.without\?(.*)/
1030-
problem "Use if build.with?#{$1} instead of unless build.without?#{$1}"
1030+
problem "Use if build.with?#{Regexp.last_match(1)} instead of unless build.without?#{Regexp.last_match(1)}"
10311031
end
10321032

10331033
if line =~ /(not\s|!)\s*build\.with?\?/
@@ -1071,7 +1071,7 @@ def line_problems(line, _lineno)
10711071
end
10721072

10731073
if line =~ /^def (\w+).*$/
1074-
problem "Define method #{$1.inspect} in the class body, not at the top-level"
1074+
problem "Define method #{Regexp.last_match(1).inspect} in the class body, not at the top-level"
10751075
end
10761076

10771077
if line.include?("ENV.fortran") && !formula.requirements.map(&:class).include?(FortranRequirement)
@@ -1083,20 +1083,20 @@ def line_problems(line, _lineno)
10831083
end
10841084

10851085
if line =~ /depends_on :(.+) (if.+|unless.+)$/
1086-
conditional_dep_problems($1.to_sym, $2, $&)
1086+
conditional_dep_problems(Regexp.last_match(1).to_sym, Regexp.last_match(2), $&)
10871087
end
10881088

10891089
if line =~ /depends_on ['"](.+)['"] (if.+|unless.+)$/
1090-
conditional_dep_problems($1, $2, $&)
1090+
conditional_dep_problems(Regexp.last_match(1), Regexp.last_match(2), $&)
10911091
end
10921092

10931093
if line =~ /(Dir\[("[^\*{},]+")\])/
1094-
problem "#{$1} is unnecessary; just use #{$2}"
1094+
problem "#{Regexp.last_match(1)} is unnecessary; just use #{Regexp.last_match(2)}"
10951095
end
10961096

10971097
if line =~ /system (["'](#{FILEUTILS_METHODS})["' ])/o
1098-
system = $1
1099-
method = $2
1098+
system = Regexp.last_match(1)
1099+
method = Regexp.last_match(2)
11001100
problem "Use the `#{method}` Ruby method instead of `system #{system}`"
11011101
end
11021102

@@ -1114,7 +1114,7 @@ def line_problems(line, _lineno)
11141114
end
11151115

11161116
if line =~ /system\s+['"](otool|install_name_tool|lipo)/ && formula.name != "cctools"
1117-
problem "Use ruby-macho instead of calling #{$1}"
1117+
problem "Use ruby-macho instead of calling #{Regexp.last_match(1)}"
11181118
end
11191119

11201120
if formula.tap.to_s == "homebrew/core"
@@ -1125,29 +1125,29 @@ def line_problems(line, _lineno)
11251125
end
11261126

11271127
if line =~ /((revision|version_scheme)\s+0)/
1128-
problem "'#{$1}' should be removed"
1128+
problem "'#{Regexp.last_match(1)}' should be removed"
11291129
end
11301130

11311131
return unless @strict
11321132

1133-
problem "`#{$1}` in formulae is deprecated" if line =~ /(env :(std|userpaths))/
1133+
problem "`#{Regexp.last_match(1)}` in formulae is deprecated" if line =~ /(env :(std|userpaths))/
11341134

11351135
if line =~ /system ((["'])[^"' ]*(?:\s[^"' ]*)+\2)/
1136-
bad_system = $1
1136+
bad_system = Regexp.last_match(1)
11371137
unless %w[| < > & ; *].any? { |c| bad_system.include? c }
11381138
good_system = bad_system.gsub(" ", "\", \"")
11391139
problem "Use `system #{good_system}` instead of `system #{bad_system}` "
11401140
end
11411141
end
11421142

1143-
problem "`#{$1}` is now unnecessary" if line =~ /(require ["']formula["'])/
1143+
problem "`#{Regexp.last_match(1)}` is now unnecessary" if line =~ /(require ["']formula["'])/
11441144

11451145
if line =~ %r{#\{share\}/#{Regexp.escape(formula.name)}[/'"]}
11461146
problem "Use \#{pkgshare} instead of \#{share}/#{formula.name}"
11471147
end
11481148

11491149
return unless line =~ %r{share(\s*[/+]\s*)(['"])#{Regexp.escape(formula.name)}(?:\2|/)}
1150-
problem "Use pkgshare instead of (share#{$1}\"#{formula.name}\")"
1150+
problem "Use pkgshare instead of (share#{Regexp.last_match(1)}\"#{formula.name}\")"
11511151
end
11521152

11531153
def audit_reverse_migration
@@ -1297,7 +1297,7 @@ def audit_checksum
12971297

12981298
def audit_download_strategy
12991299
if url =~ %r{^(cvs|bzr|hg|fossil)://} || url =~ %r{^(svn)\+http://}
1300-
problem "Use of the #{$&} scheme is deprecated, pass `:using => :#{$1}` instead"
1300+
problem "Use of the #{$&} scheme is deprecated, pass `:using => :#{Regexp.last_match(1)}` instead"
13011301
end
13021302

13031303
url_strategy = DownloadStrategyDetector.detect(url)
@@ -1341,7 +1341,7 @@ def audit_download_strategy
13411341
def audit_urls
13421342
# Check GNU urls; doesn't apply to mirrors
13431343
if url =~ %r{^(?:https?|ftp)://ftpmirror.gnu.org/(.*)}
1344-
problem "Please use \"https://ftp.gnu.org/gnu/#{$1}\" instead of #{url}."
1344+
problem "Please use \"https://ftp.gnu.org/gnu/#{Regexp.last_match(1)}\" instead of #{url}."
13451345
end
13461346

13471347
if mirrors.include?(url)
@@ -1375,11 +1375,11 @@ def audit_urls
13751375
%r{^http://(?:[^/]*\.)?mirrorservice\.org/}
13761376
problem "Please use https:// for #{p}"
13771377
when %r{^http://search\.mcpan\.org/CPAN/(.*)}i
1378-
problem "#{p} should be `https://cpan.metacpan.org/#{$1}`"
1378+
problem "#{p} should be `https://cpan.metacpan.org/#{Regexp.last_match(1)}`"
13791379
when %r{^(http|ftp)://ftp\.gnome\.org/pub/gnome/(.*)}i
1380-
problem "#{p} should be `https://download.gnome.org/#{$2}`"
1380+
problem "#{p} should be `https://download.gnome.org/#{Regexp.last_match(2)}`"
13811381
when %r{^git://anonscm\.debian\.org/users/(.*)}i
1382-
problem "#{p} should be `https://anonscm.debian.org/git/users/#{$1}`"
1382+
problem "#{p} should be `https://anonscm.debian.org/git/users/#{Regexp.last_match(1)}`"
13831383
end
13841384
end
13851385

@@ -1389,7 +1389,7 @@ def audit_urls
13891389
when %r{^ftp://ftp\.mirrorservice\.org}
13901390
problem "Please use https:// for #{p}"
13911391
when %r{^ftp://ftp\.cpan\.org/pub/CPAN(.*)}i
1392-
problem "#{p} should be `http://search.cpan.org/CPAN#{$1}`"
1392+
problem "#{p} should be `http://search.cpan.org/CPAN#{Regexp.last_match(1)}`"
13931393
end
13941394
end
13951395

@@ -1403,7 +1403,7 @@ def audit_urls
14031403
next unless p =~ %r{^https?://.*\b(sourceforge|sf)\.(com|net)}
14041404

14051405
if p =~ /(\?|&)use_mirror=/
1406-
problem "Don't use #{$1}use_mirror in SourceForge urls (url is #{p})."
1406+
problem "Don't use #{Regexp.last_match(1)}use_mirror in SourceForge urls (url is #{p})."
14071407
end
14081408

14091409
if p.end_with?("/download")
@@ -1433,7 +1433,7 @@ def audit_urls
14331433
problem <<-EOS.undent
14341434
Please use a secure mirror for Debian URLs.
14351435
We recommend:
1436-
https://mirrors.ocf.berkeley.edu/debian/#{$1}
1436+
https://mirrors.ocf.berkeley.edu/debian/#{Regexp.last_match(1)}
14371437
EOS
14381438
end
14391439

@@ -1486,7 +1486,7 @@ def audit_urls
14861486
next unless u =~ %r{https?://codeload\.github\.com/(.+)/(.+)/(?:tar\.gz|zip)/(.+)}
14871487
problem <<-EOS.undent
14881488
use GitHub archive URLs:
1489-
https://github.com/#{$1}/#{$2}/archive/#{$3}.tar.gz
1489+
https://github.com/#{Regexp.last_match(1)}/#{Regexp.last_match(2)}/archive/#{Regexp.last_match(3)}.tar.gz
14901490
Rather than codeload:
14911491
#{u}
14921492
EOS
@@ -1495,7 +1495,7 @@ def audit_urls
14951495
# Check for Maven Central urls, prefer HTTPS redirector over specific host
14961496
urls.each do |u|
14971497
next unless u =~ %r{https?://(?:central|repo\d+)\.maven\.org/maven2/(.+)$}
1498-
problem "#{u} should be `https://search.maven.org/remotecontent?filepath=#{$1}`"
1498+
problem "#{u} should be `https://search.maven.org/remotecontent?filepath=#{Regexp.last_match(1)}`"
14991499
end
15001500

15011501
if name == "curl" && !urls.find { |u| u.start_with?("http://") }
@@ -1506,7 +1506,7 @@ def audit_urls
15061506
if @strict
15071507
urls.each do |p|
15081508
next unless p =~ %r{^https?://pypi.python.org/(.*)}
1509-
problem "#{p} should be `https://files.pythonhosted.org/#{$1}`"
1509+
problem "#{p} should be `https://files.pythonhosted.org/#{Regexp.last_match(1)}`"
15101510
end
15111511
end
15121512

0 commit comments

Comments
 (0)