From 84b2276fd866342cd84c6ada8ffc13c5c209c3cf Mon Sep 17 00:00:00 2001 From: Markus Reiter <me@reitermark.us> Date: Mon, 24 Oct 2016 17:07:57 +0200 Subject: [PATCH] Use guard clauses. --- Library/Homebrew/cask/lib/hbc/cli/search.rb | 15 +++---- Library/Homebrew/cask/lib/hbc/installer.rb | 25 ++++++------ Library/Homebrew/cask/lib/hbc/utils.rb | 14 +++---- Library/Homebrew/cleanup.rb | 7 ++-- Library/Homebrew/descriptions.rb | 45 ++++++++++----------- Library/Homebrew/extend/ENV/std.rb | 5 +-- Library/Homebrew/sandbox.rb | 7 ++-- 7 files changed, 57 insertions(+), 61 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 8e8f8fd750..3f73fcd2e1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -41,14 +41,15 @@ module Hbc ohai "Exact match" puts exact_match end - unless partial_matches.empty? - if extract_regexp search_term - ohai "Regexp matches" - else - ohai "Partial matches" - end - puts Formatter.columns(partial_matches) + + return if partial_matches.empty? + + if extract_regexp search_term + ohai "Regexp matches" + else + ohai "Partial matches" end + puts Formatter.columns(partial_matches) end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index 183d1f14ba..57efe97e95 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -28,22 +28,21 @@ module Hbc def self.print_caveats(cask) odebug "Printing caveats" - unless cask.caveats.empty? - output = capture_output do - cask.caveats.each do |caveat| - if caveat.respond_to?(:eval_and_print) - caveat.eval_and_print(cask) - else - puts caveat - end + return if cask.caveats.empty? + + output = capture_output do + cask.caveats.each do |caveat| + if caveat.respond_to?(:eval_and_print) + caveat.eval_and_print(cask) + else + puts caveat end end - - unless output.empty? - ohai "Caveats" - puts output - end end + + return if output.empty? + ohai "Caveats" + puts output end def self.capture_output(&block) diff --git a/Library/Homebrew/cask/lib/hbc/utils.rb b/Library/Homebrew/cask/lib/hbc/utils.rb index b442efd2f2..88b8a88c4b 100644 --- a/Library/Homebrew/cask/lib/hbc/utils.rb +++ b/Library/Homebrew/cask/lib/hbc/utils.rb @@ -137,13 +137,13 @@ module Hbc def self.nowstamp_metadata_path(container_path) @timenow ||= Time.now.gmtime - if container_path.respond_to?(:join) - precision = 3 - timestamp = @timenow.strftime("%Y%m%d%H%M%S") - fraction = format("%.#{precision}f", @timenow.to_f - @timenow.to_i)[1..-1] - timestamp.concat(fraction) - container_path.join(timestamp) - end + return unless container_path.respond_to?(:join) + + precision = 3 + timestamp = @timenow.strftime("%Y%m%d%H%M%S") + fraction = format("%.#{precision}f", @timenow.to_f - @timenow.to_i)[1..-1] + timestamp.concat(fraction) + container_path.join(timestamp) end def self.size_in_bytes(files) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index f7db1c11f7..615a7ce9e7 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -10,10 +10,9 @@ module Homebrew cleanup_cellar cleanup_cache cleanup_logs - unless ARGV.dry_run? - cleanup_lockfiles - rm_ds_store - end + return if ARGV.dry_run? + cleanup_lockfiles + rm_ds_store end def self.update_disk_cleanup_size(path_size) diff --git a/Library/Homebrew/descriptions.rb b/Library/Homebrew/descriptions.rb index cc690c050e..08860f7cf5 100644 --- a/Library/Homebrew/descriptions.rb +++ b/Library/Homebrew/descriptions.rb @@ -57,42 +57,41 @@ class Descriptions # If it does exist, but the Report is empty, just touch the cache file. # Otherwise, use the report to update the cache. def self.update_cache(report) - if CACHE_FILE.exist? - if report.empty? - FileUtils.touch CACHE_FILE - else - renamings = report.select_formula(:R) - alterations = report.select_formula(:A) + report.select_formula(:M) + - renamings.map(&:last) - cache_formulae(alterations, save: false) - uncache_formulae(report.select_formula(:D) + - renamings.map(&:first)) - end + return unless CACHE_FILE.exist? + + if report.empty? + FileUtils.touch CACHE_FILE + else + renamings = report.select_formula(:R) + alterations = report.select_formula(:A) + report.select_formula(:M) + + renamings.map(&:last) + cache_formulae(alterations, save: false) + uncache_formulae(report.select_formula(:D) + + renamings.map(&:first)) end end # Given an array of formula names, add them and their descriptions to the # cache. Save the updated cache to disk, unless explicitly told not to. def self.cache_formulae(formula_names, options = { save: true }) - if cache - formula_names.each do |name| - begin - desc = Formulary.factory(name).desc - rescue FormulaUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS - end - @cache[name] = desc + return unless cache + + formula_names.each do |name| + begin + desc = Formulary.factory(name).desc + rescue FormulaUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS end - save_cache if options[:save] + @cache[name] = desc end + save_cache if options[:save] end # Given an array of formula names, remove them and their descriptions from # the cache. Save the updated cache to disk, unless explicitly told not to. def self.uncache_formulae(formula_names, options = { save: true }) - if cache - formula_names.each { |name| @cache.delete(name) } - save_cache if options[:save] - end + return unless cache + formula_names.each { |name| @cache.delete(name) } + save_cache if options[:save] end # Given a regex, find all formulae whose specified fields contain a match. diff --git a/Library/Homebrew/extend/ENV/std.rb b/Library/Homebrew/extend/ENV/std.rb index 27dc95d297..14f9b81b8e 100644 --- a/Library/Homebrew/extend/ENV/std.rb +++ b/Library/Homebrew/extend/ENV/std.rb @@ -11,9 +11,8 @@ module Stdenv DEFAULT_FLAGS = "-march=core2 -msse4".freeze def self.extended(base) - unless ORIGINAL_PATHS.include? HOMEBREW_PREFIX/"bin" - base.prepend_path "PATH", "#{HOMEBREW_PREFIX}/bin" - end + return if ORIGINAL_PATHS.include? HOMEBREW_PREFIX/"bin" + base.prepend_path "PATH", "#{HOMEBREW_PREFIX}/bin" end # @private diff --git a/Library/Homebrew/sandbox.rb b/Library/Homebrew/sandbox.rb index 4d0709cb4b..9597dafa81 100644 --- a/Library/Homebrew/sandbox.rb +++ b/Library/Homebrew/sandbox.rb @@ -27,10 +27,9 @@ class Sandbox end def self.print_sandbox_message - unless @printed_sandbox_message - ohai "Using the sandbox" - @printed_sandbox_message = true - end + return if @printed_sandbox_message + ohai "Using the sandbox" + @printed_sandbox_message = true end def initialize -- GitLab