From fb2fdc52499298700b11b52f0b1c91e2b3d05029 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 18 Aug 2024 16:28:17 -0700 Subject: [PATCH] Minor type safety improvements --- Library/Homebrew/cask/cask_loader.rb | 27 ++++++++++++--------------- Library/Homebrew/cask/url.rb | 2 +- Library/Homebrew/cmd/home.rb | 2 +- Library/Homebrew/dev-cmd/tests.rb | 8 ++++---- Library/Homebrew/formula.rb | 2 +- Library/Homebrew/source_location.rb | 2 +- Library/Homebrew/version.rb | 6 ++---- 7 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 81646dc579..481118b555 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -56,24 +56,21 @@ module Cask .returns(T.nilable(T.attached_class)) } def self.try_new(ref, warn: false) - case ref - when Cask, URI::Generic - # do nothing - else - content = ref.to_str + return if ref.is_a?(Cask) - # Cache compiled regex - @regex ||= begin - token = /(?:"[^"]*"|'[^']*')/ - curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ - do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ - /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m - end + content = ref.to_str - return unless content.match?(@regex) - - new(content) + # Cache compiled regex + @regex ||= begin + token = /(?:"[^"]*"|'[^']*')/ + curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ + do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ + /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m end + + return unless content.match?(@regex) + + new(content) end sig { params(content: String, tap: Tap).void } diff --git a/Library/Homebrew/cask/url.rb b/Library/Homebrew/cask/url.rb index 64c9cdb40d..0ed2b2a880 100644 --- a/Library/Homebrew/cask/url.rb +++ b/Library/Homebrew/cask/url.rb @@ -155,7 +155,7 @@ module Cask # @api public def method_missing(method, *args, &block) if @dsl.respond_to?(method) - T.unsafe(@dsl).public_send(method, *args, &block) + @dsl.public_send(method, *args, &block) else super end diff --git a/Library/Homebrew/cmd/home.rb b/Library/Homebrew/cmd/home.rb index 8def8448c5..59ac2b6c5c 100644 --- a/Library/Homebrew/cmd/home.rb +++ b/Library/Homebrew/cmd/home.rb @@ -36,7 +36,7 @@ module Homebrew formula_or_cask.homepage end - exec_browser(*T.unsafe(homepages)) + exec_browser(*homepages) end private diff --git a/Library/Homebrew/dev-cmd/tests.rb b/Library/Homebrew/dev-cmd/tests.rb index 1ac6edfe2c..b45df9ac94 100644 --- a/Library/Homebrew/dev-cmd/tests.rb +++ b/Library/Homebrew/dev-cmd/tests.rb @@ -50,9 +50,9 @@ module Homebrew parallel = true - files = if args.only - # FIXME: This is safe once args are namespaced by command - test_name, line = T.unsafe(args.only).split(":", 2) + only = args.only + files = if only + test_name, line = only.split(":", 2) if line.nil? Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb") @@ -67,7 +67,7 @@ module Homebrew end if files.blank? - raise UsageError, "The `--only` argument requires a valid file or folder name!" if args.only + raise UsageError, "The `--only` argument requires a valid file or folder name!" if only if args.changed? opoo "No tests are directly associated with the changed files!" diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index c4132b8fbe..f0f5704ea1 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -3124,7 +3124,7 @@ class Formula removed = ENV.remove_cc_etc begin - T.unsafe(self).system("xcodebuild", *args) + self.system("xcodebuild", *args) ensure ENV.update(removed) end diff --git a/Library/Homebrew/source_location.rb b/Library/Homebrew/source_location.rb index b3671cb5a4..545d35ae0c 100644 --- a/Library/Homebrew/source_location.rb +++ b/Library/Homebrew/source_location.rb @@ -11,7 +11,7 @@ module Homebrew attr_reader :column sig { params(line: Integer, column: T.nilable(Integer)).void } - def initialize(line, column = T.unsafe(nil)) + def initialize(line, column = nil) @line = line @column = column end diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb index 4e6f14a267..beb92b2c5b 100644 --- a/Library/Homebrew/version.rb +++ b/Library/Homebrew/version.rb @@ -731,12 +731,10 @@ class Version def to_s = version.to_s sig { params(options: T.untyped).returns(String) } - def to_json(*options) - T.unsafe(version).to_json(*options) - end + def to_json(*options) = version.to_json(*options) sig { params(method: T.any(Symbol, String), include_all: T::Boolean).returns(T::Boolean) } - def respond_to?(method, include_all = T.unsafe(nil)) + def respond_to?(method, include_all = false) # rubocop:disable Style/OptionalBooleanParameter (`Object` override) return !null? if ["to_str", :to_str].include?(method) super