Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return type restrictions to file-related methods and Log #10584

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compress/zip/file_info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ module Compress::Zip::FileInfo
end

# Returns `true` if this entry is a directory.
def dir?
def dir? : Bool
filename.ends_with?('/')
end

# Returns `true` if this entry is a file.
def file?
def file? : Bool
!dir?
end

Expand Down
4 changes: 2 additions & 2 deletions src/dir/glob.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class Dir
record DirectoriesOnly
record ConstantEntry, path : String
record EntryMatch, pattern : String do
def matches?(string)
def matches?(string) : Bool
File.match?(pattern, string)
end
end
record RecursiveDirectories
record ConstantDirectory, path : String
record RootDirectory
record DirectoryMatch, pattern : String do
def matches?(string)
def matches?(string) : Bool
File.match?(pattern, string)
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class File < IO::FileDescriptor
# * `\\` escapes the next character.
#
# NOTE: Only `/` is recognized as path separator in both *pattern* and *path*.
def self.match?(pattern : String, path : Path | String)
def self.match?(pattern : String, path : Path | String) : Bool
expanded_patterns = [] of String
File.expand_brace_pattern(pattern, expanded_patterns)

Expand Down Expand Up @@ -528,7 +528,7 @@ class File < IO::FileDescriptor
end

# :nodoc:
def self.expand_brace_pattern(pattern : String, expanded)
def self.expand_brace_pattern(pattern : String, expanded) : Array(String)?
reader = Char::Reader.new(pattern)

lbrace = nil
Expand Down Expand Up @@ -791,7 +791,7 @@ class File < IO::FileDescriptor
end

# Returns the size in bytes of the currently opened file.
def size
def size : Int64
info.size
end

Expand Down
4 changes: 2 additions & 2 deletions src/file/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class File::Error < IO::Error
end
end

protected def self.build_message(message, *, file : String)
protected def self.build_message(message, *, file : String) : String
"#{message}: '#{file.inspect_unquoted}'"
end

protected def self.build_message(message, *, file : String, other : String)
protected def self.build_message(message, *, file : String, other : String) : String
"#{message}: '#{file.inspect_unquoted}' -> '#{other.inspect_unquoted}'"
end

Expand Down
2 changes: 1 addition & 1 deletion src/file_utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module FileUtils
# ```
#
# NOTE: Alias of `File.same_content?`
def cmp(filename1 : Path | String, filename2 : Path | String)
def cmp(filename1 : Path | String, filename2 : Path | String) : Bool
File.same_content?(filename1, filename2)
end

Expand Down
2 changes: 1 addition & 1 deletion src/log/dispatch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Log
end

# :nodoc:
def self.for(mode : DispatchMode)
def self.for(mode : DispatchMode) : self
case mode
in .sync?
SyncDispatcher.new
Expand Down
2 changes: 1 addition & 1 deletion src/log/entry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum Log::Severity
# Used only for severity level filter.
None

def label
def label : String
case self
in Trace then "TRACE"
in Debug then "DEBUG"
Expand Down
2 changes: 1 addition & 1 deletion src/log/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Log
at_exit { @@builder.close }

# Returns the default `Log::Builder` used for `Log.for` calls.
def self.builder
def self.builder : Log::Builder
@@builder
end

Expand Down
8 changes: 4 additions & 4 deletions src/log/metadata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class Log::Metadata

# Returns a `Metadata` with the information of the argument.
# Used to handle `Log::Context#set` and `Log#Emitter.emit` overloads.
def self.build(value : NamedTuple | Hash)
def self.build(value : NamedTuple | Hash) : self
return @@empty if value.empty?
Metadata.new(nil, value)
end

# :ditto:
def self.build(value : Metadata)
def self.build(value : Metadata) : Metadata
value
end

Expand All @@ -74,7 +74,7 @@ class Log::Metadata
Metadata.new(self, other)
end

def empty?
def empty? : Bool
parent = @parent

@size == 0 && (parent.nil? || parent.empty?)
Expand Down Expand Up @@ -213,7 +213,7 @@ class Log::Metadata
end

# :nodoc:
def self.to_metadata_value(value)
def self.to_metadata_value(value) : Metadata::Value
value.is_a?(Value) ? value : Value.new(value)
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ struct Path
# Path["file.tar.gz"].stem # => "file.tar"
# Path["foo/file.cr"].stem # => "file"
# ```
def stem
def stem : String
basename(extension)
end

Expand Down Expand Up @@ -681,7 +681,7 @@ struct Path
# Converts this path to the given *kind*.
#
# See `#to_windows` and `#to_posix` for details.
def to_kind(kind)
def to_kind(kind) : Path
if kind.posix?
to_posix
else
Expand Down Expand Up @@ -1284,7 +1284,7 @@ struct Path
Path.separators(@kind)
end

def ends_with_separator?
def ends_with_separator? : Bool
ends_with_separator?(@name)
end

Expand Down
4 changes: 2 additions & 2 deletions src/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ class Process

# Whether the process is still registered in the system.
# Note that this returns `true` for processes in the zombie or similar state.
def exists?
def exists? : Bool
@process_info.exists?
end

# Whether this process is already terminated.
def terminated?
def terminated? : Bool
!exists?
end

Expand Down
2 changes: 1 addition & 1 deletion src/process/executable_path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Process
# will be expanded).
#
# Returns `nil` if the file can't be found.
def self.executable_path
def self.executable_path : String?
if executable = executable_path_impl
begin
File.real_path(executable)
Expand Down
8 changes: 4 additions & 4 deletions src/system/group.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ class System::Group
# Returns the group associated with the given name.
#
# Raises `NotFoundError` if no such group exists.
def self.find_by(*, name : String)
def self.find_by(*, name : String) : System::Group
find_by?(name: name) || raise NotFoundError.new("No such group: #{name}")
end

# Returns the group associated with the given name.
#
# Returns `nil` if no such group exists.
def self.find_by?(*, name : String)
def self.find_by?(*, name : String) : System::Group?
from_name?(name)
end

# Returns the group associated with the given ID.
#
# Raises `NotFoundError` if no such group exists.
def self.find_by(*, id : String)
def self.find_by(*, id : String) : System::Group
find_by?(id: id) || raise NotFoundError.new("No such group: #{id}")
end

# Returns the group associated with the given ID.
#
# Returns `nil` if no such group exists.
def self.find_by?(*, id : String)
def self.find_by?(*, id : String) : System::Group?
from_id?(id)
end

Expand Down
8 changes: 4 additions & 4 deletions src/system/user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ class System::User
# Returns the user associated with the given username.
#
# Raises `NotFoundError` if no such user exists.
def self.find_by(*, name : String)
def self.find_by(*, name : String) : System::User
find_by?(name: name) || raise NotFoundError.new("No such user: #{name}")
end

# Returns the user associated with the given username.
#
# Returns `nil` if no such user exists.
def self.find_by?(*, name : String)
def self.find_by?(*, name : String) : System::User?
from_username?(name)
end

# Returns the user associated with the given ID.
#
# Raises `NotFoundError` if no such user exists.
def self.find_by(*, id : String)
def self.find_by(*, id : String) : System::User
find_by?(id: id) || raise NotFoundError.new("No such user: #{id}")
end

# Returns the user associated with the given ID.
#
# Returns `nil` if no such user exists.
def self.find_by?(*, id : String)
def self.find_by?(*, id : String) : System::User?
from_id?(id)
end

Expand Down