Merge pull request #14409 from carlocab/compiler-check

rubocops/lines: update hard-coded compiler check
This commit is contained in:
Mike McQuaid 2023-01-24 10:08:53 +00:00 committed by GitHub
commit 518c961f4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -617,19 +617,23 @@ module RuboCop
# Avoid hard-coding compilers # Avoid hard-coding compilers
find_every_method_call_by_name(body_node, :system).each do |method| find_every_method_call_by_name(body_node, :system).each do |method|
next if @formula_name == "bazel" # TODO: Remove shim bypass in bazel.
param = parameters(method).first param = parameters(method).first
if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)(\s|$)})) if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|clang|cc|c[89]9)(\s|$)}))
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\"" problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)(\s|$)})) elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|clang|c)\+\+)(\s|$)}))
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
end end
end end
find_instance_method_call(body_node, "ENV", :[]=) do |method| find_instance_method_call(body_node, "ENV", :[]=) do |method|
next if @formula_name == "bazel" # TODO: Remove shim bypass in bazel.
param = parameters(method)[1] param = parameters(method)[1]
if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)(\s|$)})) if (match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|clang|cc|c[89]9)(\s|$)}))
problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\"" problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)(\s|$)})) elsif (match = regex_match_group(param, %r{^(/usr/bin/)?((g|clang|c)\+\+)(\s|$)}))
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
end end
end end

View File

@ -255,14 +255,14 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
RUBY RUBY
end end
it "reports an offense when a hard-coded `llvm-g++` is set as COMPILER_PATH" do it "reports an offense when a hard-coded `c++` is set as COMPILER_PATH" do
expect_offense(<<~'RUBY') expect_offense(<<~'RUBY')
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'https://brew.sh/foo-1.0.tgz' url 'https://brew.sh/foo-1.0.tgz'
def install def install
ENV["COMPILER_PATH"] = "/usr/bin/llvm-g++" ENV["COMPILER_PATH"] = "/usr/bin/c++"
^^^^^^^^^^^^^^^^^^^ Use "#{ENV.cxx}" instead of hard-coding "llvm-g++" ^^^^^^^^^^^^^^ Use "#{ENV.cxx}" instead of hard-coding "c++"
end end
end end
RUBY RUBY