various: eliminate the usage of any? (#638)

`any?` is not the opposite of `empty?`. Besides the case that
`[false, nil].any?` will return false, `any?`(O(n)) has much worse
performance than `empty?`(O(1)).
This commit is contained in:
Xu Cheng 2016-08-05 22:01:32 +08:00 committed by GitHub
parent 38209aadbf
commit a8566c9848
19 changed files with 48 additions and 43 deletions

View File

@ -628,7 +628,8 @@ class FormulaAuditor
fv = FormulaVersions.new(formula, :max_depth => 10) fv = FormulaVersions.new(formula, :max_depth => 10)
revision_map = fv.revision_map("origin/master") revision_map = fv.revision_map("origin/master")
if (revisions = revision_map[formula.version]).any? revisions = revision_map[formula.version]
if !revisions.empty?
problem "revision should not decrease" if formula.revision < revisions.max problem "revision should not decrease" if formula.revision < revisions.max
elsif formula.revision != 0 elsif formula.revision != 0
if formula.stable if formula.stable
@ -644,7 +645,7 @@ class FormulaAuditor
def audit_legacy_patches def audit_legacy_patches
return unless formula.respond_to?(:patches) return unless formula.respond_to?(:patches)
legacy_patches = Patch.normalize_legacy_patches(formula.patches).grep(LegacyPatch) legacy_patches = Patch.normalize_legacy_patches(formula.patches).grep(LegacyPatch)
if legacy_patches.any? unless legacy_patches.empty?
problem "Use the patch DSL instead of defining a 'patches' method" problem "Use the patch DSL instead of defining a 'patches' method"
legacy_patches.each { |p| audit_patch(p) } legacy_patches.each { |p| audit_patch(p) }
end end

View File

@ -58,10 +58,10 @@ module Homebrew
next if Metafiles::EXTENSIONS.include? file.extname next if Metafiles::EXTENSIONS.include? file.extname
linked_libraries = Keg.file_linked_libraries(file, string) linked_libraries = Keg.file_linked_libraries(file, string)
result ||= linked_libraries.any? result ||= !linked_libraries.empty?
if ARGV.verbose? if ARGV.verbose?
print_filename(string, file) if linked_libraries.any? print_filename(string, file) unless linked_libraries.empty?
linked_libraries.each do |lib| linked_libraries.each do |lib|
puts " #{Tty.gray}-->#{Tty.reset} links to #{lib}" puts " #{Tty.gray}-->#{Tty.reset} links to #{lib}"
end end
@ -83,7 +83,7 @@ module Homebrew
end end
end end
if ARGV.verbose? && text_matches.any? if ARGV.verbose? && !text_matches.empty?
print_filename string, file print_filename string, file
text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset| text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset|
puts " #{Tty.gray}-->#{Tty.reset} match '#{match}' at offset #{Tty.em}0x#{offset}#{Tty.reset}" puts " #{Tty.gray}-->#{Tty.reset} match '#{match}' at offset #{Tty.em}0x#{offset}#{Tty.reset}"
@ -157,7 +157,7 @@ module Homebrew
versions = FormulaVersions.new(f) versions = FormulaVersions.new(f)
bottle_revisions = versions.bottle_version_map("origin/master")[f.pkg_version] bottle_revisions = versions.bottle_version_map("origin/master")[f.pkg_version]
bottle_revisions.pop if bottle_revisions.last.to_i > 0 bottle_revisions.pop if bottle_revisions.last.to_i > 0
bottle_revision = bottle_revisions.any? ? bottle_revisions.max.to_i + 1 : 0 bottle_revision = bottle_revisions.empty? ? 0 : bottle_revisions.max.to_i + 1
end end
filename = Bottle::Filename.create(f, Utils::Bottles.tag, bottle_revision) filename = Bottle::Filename.create(f, Utils::Bottles.tag, bottle_revision)
@ -287,7 +287,7 @@ module Homebrew
old_spec.send(field) != bottle.send(field) old_spec.send(field) != bottle.send(field)
end end
bad_fields.delete(:cellar) if old_spec.cellar == :any && bottle.cellar == :any_skip_relocation bad_fields.delete(:cellar) if old_spec.cellar == :any && bottle.cellar == :any_skip_relocation
if bad_fields.any? unless bad_fields.empty?
bottle_path.unlink if bottle_path.exist? bottle_path.unlink if bottle_path.exist?
odie "--keep-old is passed but there are changes in: #{bad_fields.join ", "}" odie "--keep-old is passed but there are changes in: #{bad_fields.join ", "}"
end end
@ -387,7 +387,7 @@ module Homebrew
next if key == "cellar" && old_value == "any" && value == "any_skip_relocation" next if key == "cellar" && old_value == "any" && value == "any_skip_relocation"
mismatches << key if old_value.empty? || value != old_value mismatches << key if old_value.empty? || value != old_value
end end
if mismatches.any? unless mismatches.empty?
odie "--keep-old was passed but there were changes in #{mismatches.join(", ")}!" odie "--keep-old was passed but there were changes in #{mismatches.join(", ")}!"
end end
output = bottle_output bottle output = bottle_output bottle

View File

@ -118,7 +118,7 @@ module Homebrew
attrs << "pinned at #{f.pinned_version}" if f.pinned? attrs << "pinned at #{f.pinned_version}" if f.pinned?
attrs << "keg-only" if f.keg_only? attrs << "keg-only" if f.keg_only?
puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" if attrs.any?}" puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}"
puts f.desc if f.desc puts f.desc if f.desc
puts "#{Tty.em}#{f.homepage}#{Tty.reset}" if f.homepage puts "#{Tty.em}#{f.homepage}#{Tty.reset}" if f.homepage
@ -126,14 +126,14 @@ module Homebrew
puts "Conflicts with: #{conflicts*", "}" unless conflicts.empty? puts "Conflicts with: #{conflicts*", "}" unless conflicts.empty?
kegs = f.installed_kegs.sort_by(&:version) kegs = f.installed_kegs.sort_by(&:version)
if kegs.any? if kegs.empty?
puts "Not installed"
else
kegs.each do |keg| kegs.each do |keg|
puts "#{keg} (#{keg.abv})#{" *" if keg.linked?}" puts "#{keg} (#{keg.abv})#{" *" if keg.linked?}"
tab = Tab.for_keg(keg).to_s tab = Tab.for_keg(keg).to_s
puts " #{tab}" unless tab.empty? puts " #{tab}" unless tab.empty?
end end
else
puts "Not installed"
end end
puts "From: #{Tty.em}#{github_info(f)}#{Tty.reset}" puts "From: #{Tty.em}#{github_info(f)}#{Tty.reset}"

View File

@ -82,7 +82,7 @@ module Homebrew
begin begin
formulae = [] formulae = []
if ARGV.casks.any? unless ARGV.casks.empty?
args = [] args = []
args << "--force" if ARGV.force? args << "--force" if ARGV.force?
args << "--debug" if ARGV.debug? args << "--debug" if ARGV.debug?

View File

@ -28,7 +28,7 @@ module Homebrew
# Unbrewed uses the PREFIX, which will exist # Unbrewed uses the PREFIX, which will exist
# Things below use the CELLAR, which doesn't until the first formula is installed. # Things below use the CELLAR, which doesn't until the first formula is installed.
unless HOMEBREW_CELLAR.exist? unless HOMEBREW_CELLAR.exist?
raise NoSuchKegError.new(ARGV.named.first) if ARGV.named.any? raise NoSuchKegError.new(ARGV.named.first) unless ARGV.named.empty?
return return
end end

View File

@ -17,13 +17,17 @@ require "keg"
module Homebrew module Homebrew
def outdated def outdated
formulae = ARGV.resolved_formulae.any? ? ARGV.resolved_formulae : Formula.installed formulae = if ARGV.resolved_formulae.empty?
Formula.installed
else
ARGV.resolved_formulae
end
if ARGV.json == "v1" if ARGV.json == "v1"
outdated = print_outdated_json(formulae) outdated = print_outdated_json(formulae)
else else
outdated = print_outdated(formulae) outdated = print_outdated(formulae)
end end
Homebrew.failed = ARGV.resolved_formulae.any? && outdated.any? Homebrew.failed = !ARGV.resolved_formulae.empty? && !outdated.empty?
end end
def print_outdated(formulae) def print_outdated(formulae)

View File

@ -22,10 +22,10 @@ module Homebrew
end end
options = { :aliases => ARGV.include?("--aliases") } options = { :aliases => ARGV.include?("--aliases") }
taps = if ARGV.named.any? taps = if ARGV.named.empty?
[Tap.fetch(ARGV.named.first)]
else
Tap Tap
else
[Tap.fetch(ARGV.named.first)]
end end
taps.each do |tap| taps.each do |tap|
Homebrew.failed = true unless Readall.valid_tap?(tap, options) Homebrew.failed = true unless Readall.valid_tap?(tap, options)

View File

@ -89,7 +89,7 @@ module Homebrew
arg.include?(char) && !arg.start_with?("/") arg.include?(char) && !arg.start_with?("/")
end end
end end
if ARGV.any? && bad_regex if !ARGV.empty? && bad_regex
ohai "Did you mean to perform a regular expression search?" ohai "Did you mean to perform a regular expression search?"
ohai "Surround your query with /slashes/ to search by regex." ohai "Surround your query with /slashes/ to search by regex."
end end

View File

@ -21,16 +21,16 @@ module Homebrew
if ARGV.named.empty? if ARGV.named.empty?
outdated = Formula.installed.select(&:outdated?) outdated = Formula.installed.select(&:outdated?)
exit 0 if outdated.empty? exit 0 if outdated.empty?
elsif ARGV.named.any? else
outdated = ARGV.resolved_formulae.select(&:outdated?) outdated = ARGV.resolved_formulae.select(&:outdated?)
(ARGV.resolved_formulae - outdated).each do |f| (ARGV.resolved_formulae - outdated).each do |f|
versions = f.installed_kegs.map { |keg| keg.version } versions = f.installed_kegs.map { |keg| keg.version }
if versions.any? if versions.empty?
onoe "#{f.full_name} not installed"
else
version = versions.max version = versions.max
onoe "#{f.full_name} #{version} already installed" onoe "#{f.full_name} #{version} already installed"
else
onoe "#{f.full_name} not installed"
end end
end end
exit 1 if outdated.empty? exit 1 if outdated.empty?

View File

@ -25,7 +25,7 @@ module Homebrew
end end
contents.gsub!(old, new) contents.gsub!(old, new)
end end
if contents.errors.any? unless contents.errors.empty?
raise Utils::InreplaceError, path => contents.errors raise Utils::InreplaceError, path => contents.errors
end end
contents contents

View File

@ -342,7 +342,7 @@ module Homebrew
@name = "#{diff_start_sha1}-#{diff_end_sha1}" @name = "#{diff_start_sha1}-#{diff_end_sha1}"
end end
# Handle formulae arguments being passed on the command-line e.g. `brew test-bot wget fish`. # Handle formulae arguments being passed on the command-line e.g. `brew test-bot wget fish`.
elsif @formulae && @formulae.any? elsif @formulae && !@formulae.empty?
@name = "#{@formulae.first}-#{diff_end_sha1}" @name = "#{@formulae.first}-#{diff_end_sha1}"
diff_start_sha1 = diff_end_sha1 diff_start_sha1 = diff_end_sha1
# Handle a hash being passed on the command-line e.g. `brew test-bot 1a2b3c`. # Handle a hash being passed on the command-line e.g. `brew test-bot 1a2b3c`.
@ -602,7 +602,7 @@ module Homebrew
test "brew", "uninstall", "--force", formula_name test "brew", "uninstall", "--force", formula_name
FileUtils.ln bottle_filename, HOMEBREW_CACHE/bottle_filename, :force => true FileUtils.ln bottle_filename, HOMEBREW_CACHE/bottle_filename, :force => true
@formulae.delete(formula_name) @formulae.delete(formula_name)
if unchanged_build_dependencies.any? unless unchanged_build_dependencies.empty?
test "brew", "uninstall", "--force", *unchanged_build_dependencies test "brew", "uninstall", "--force", *unchanged_build_dependencies
unchanged_dependencies -= unchanged_build_dependencies unchanged_dependencies -= unchanged_build_dependencies
end end
@ -652,7 +652,7 @@ module Homebrew
test "brew", "uninstall", "--devel", "--force", formula_name test "brew", "uninstall", "--devel", "--force", formula_name
end end
end end
test "brew", "uninstall", "--force", *unchanged_dependencies if unchanged_dependencies.any? test "brew", "uninstall", "--force", *unchanged_dependencies unless unchanged_dependencies.empty?
end end
def homebrew def homebrew

View File

@ -350,7 +350,7 @@ class BuildError < RuntimeError
end end
end end
puts puts
if RUBY_VERSION >= "1.8.7" && issues && issues.any? if RUBY_VERSION >= "1.8.7" && issues && !issues.empty?
puts "These open issues may also help:" puts "These open issues may also help:"
puts issues.map { |i| "#{i["title"]} #{i["html_url"]}" }.join("\n") puts issues.map { |i| "#{i["title"]} #{i["html_url"]}" }.join("\n")
end end

View File

@ -998,7 +998,7 @@ class Formula
# @private # @private
def outdated? def outdated?
outdated_versions.any? !outdated_versions.empty?
rescue Migrator::MigrationNeededError rescue Migrator::MigrationNeededError
true true
end end
@ -1517,7 +1517,7 @@ class Formula
installed_kegs.select { |k| pkg_version > k.version } installed_kegs.select { |k| pkg_version > k.version }
end end
if eligible_kegs.any? unless eligible_kegs.empty?
eligible_kegs.each do |keg| eligible_kegs.each do |keg|
if keg.linked? if keg.linked?
opoo "Skipping (old) #{keg} due to it being linked" opoo "Skipping (old) #{keg} due to it being linked"
@ -1526,7 +1526,7 @@ class Formula
end end
end end
end end
elsif installed_prefixes.any? && !pinned? elsif !installed_prefixes.empty? && !pinned?
# If the cellar only has one version installed, don't complain # If the cellar only has one version installed, don't complain
# that we can't tell which one to keep. Don't complain at all if the # that we can't tell which one to keep. Don't complain at all if the
# only installed version is a pinned formula. # only installed version is a pinned formula.

View File

@ -31,7 +31,7 @@ class FormulaPin
end end
def pinnable? def pinnable?
@f.installed_prefixes.any? !@f.installed_prefixes.empty?
end end
def pinned_version def pinned_version

View File

@ -253,11 +253,11 @@ class Keg
when :zsh then path.join("share", "zsh", "site-functions") when :zsh then path.join("share", "zsh", "site-functions")
when :fish then path.join("share", "fish", "vendor_completions.d") when :fish then path.join("share", "fish", "vendor_completions.d")
end end
dir && dir.directory? && dir.children.any? dir && dir.directory? && !dir.children.empty?
end end
def plist_installed? def plist_installed?
Dir["#{path}/*.plist"].any? !Dir["#{path}/*.plist"].empty?
end end
def python_site_packages_installed? def python_site_packages_installed?
@ -265,7 +265,7 @@ class Keg
end end
def python_pth_files_installed? def python_pth_files_installed?
Dir["#{path}/lib/python2.7/site-packages/*.pth"].any? !Dir["#{path}/lib/python2.7/site-packages/*.pth"].empty?
end end
def apps def apps

View File

@ -131,8 +131,8 @@ module Language
dep_site_packages = Formula[d.name].opt_lib/"python#{xy}/site-packages" dep_site_packages = Formula[d.name].opt_lib/"python#{xy}/site-packages"
next unless dep_site_packages.exist? next unless dep_site_packages.exist?
"import site; site.addsitedir('#{dep_site_packages}')\n" "import site; site.addsitedir('#{dep_site_packages}')\n"
end end.compact
if pth_contents.any? unless pth_contents.empty?
(venv_root/"lib/python#{xy}/site-packages/homebrew_deps.pth").write pth_contents.join (venv_root/"lib/python#{xy}/site-packages/homebrew_deps.pth").write pth_contents.join
end end

View File

@ -77,7 +77,7 @@ class SoftwareSpec
end end
def bottle_defined? def bottle_defined?
bottle_specification.collector.keys.any? !bottle_specification.collector.keys.empty?
end end
def bottled? def bottled?

View File

@ -259,10 +259,10 @@ module GitHub
open_or_closed_prs = issues_matching(query, :type => "pr") open_or_closed_prs = issues_matching(query, :type => "pr")
open_prs = open_or_closed_prs.select { |i| i["state"] == "open" } open_prs = open_or_closed_prs.select { |i| i["state"] == "open" }
if open_prs.any? if !open_prs.empty?
puts "Open pull requests:" puts "Open pull requests:"
prs = open_prs prs = open_prs
elsif open_or_closed_prs.any? elsif !open_or_closed_prs.empty?
puts "Closed pull requests:" puts "Closed pull requests:"
prs = open_or_closed_prs prs = open_or_closed_prs
else else

View File

@ -28,12 +28,12 @@ module Utils
s.gsub!(before, after, audit_result) s.gsub!(before, after, audit_result)
end end
errors[path] = s.errors if s.errors.any? errors[path] = s.errors unless s.errors.empty?
Pathname(path).atomic_write(s) Pathname(path).atomic_write(s)
end end
raise InreplaceError.new(errors) if errors.any? raise InreplaceError.new(errors) unless errors.empty?
end end
module_function :inreplace module_function :inreplace
end end