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

Ensure all setter methods in the standard library return the value being set #10083

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions samples/havlak.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ class SimpleLoop
def parent=(parent : SimpleLoop)
@parent = parent
parent.add_child_loop(self)
parent
end

def header=(bb : BasicBlock)
@basic_blocks.add(bb)
@header = bb
bb
end

def nesting_level=(level)
@nesting_level = level
@root = true if level == 0
level
end
end

Expand Down
1 change: 1 addition & 0 deletions samples/sdl/sdl/surface.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SDL::Surface

def []=(offset, color)
(@surface.value.pixels.as(UInt32*))[offset] = color.to_u32
color
end

def []=(x, y, color)
Expand Down
1 change: 1 addition & 0 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ class Array(T)
# :nodoc:
protected def size=(size : Int)
@size = size.to_i
size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is wrong. The size argument and the Array#size getter may have different types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for them to be the same. Setters ought to return the given value, not the one actually returned by the corresponding getter.

end

# Optimized version of `Enumerable#map`.
Expand Down
1 change: 1 addition & 0 deletions src/big/big_float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct BigFloat < Float

def self.default_precision=(prec : Int)
LibGMP.mpf_set_default_prec(prec.to_u64)
prec
end

def <=>(other : BigFloat)
Expand Down
1 change: 1 addition & 0 deletions src/bit_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct BitArray
else
@bits[bit_index] &= ~(1 << sub_index)
end
value
end

# Returns all elements that are within the given range.
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Crystal

def warning=(warning)
@warning = !!warning
warning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto Array#size=

end

abstract def to_s_with_source(io : IO, source)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/progress_tracker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ module Crystal

def stage_progress=(@stage_progress)
print_progress
stage_progress
end

def stage_progress_total=(@stage_progress_total)
print_progress
stage_progress_total
end
end
end
1 change: 1 addition & 0 deletions src/compiler/crystal/semantic/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ module Crystal
end
end
end
value
end

# Returns the minimum and maximum number of arguments that must
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ class Crystal::Call
typed_def.raises = value
end
end
value
end

def super?
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/crystal/semantic/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ module Crystal
def color=(color)
@color = !!color
inner.try &.color=(color)
color
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto Array#size= and others in this file

end

def error_trace=(error_trace)
@error_trace = !!error_trace
inner.try &.error_trace=(error_trace)
error_trace
end

def warning=(warning)
super
inner.try &.warning=(warning)
warning
end

def self.for_node(node, message, inner = nil)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module Crystal
# having doc comments, so by default this does nothing and some
# subclasses implement this.
def doc=(doc)
doc
end

def name_location
Expand All @@ -64,6 +65,7 @@ module Crystal
end

def visibility=(visibility : Visibility)
visibility
end

def visibility
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/syntax/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Crystal

def color=(color)
@color = !!color
color
end

def has_location?
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module Crystal
def wants_doc=(wants_doc)
@wants_doc = !!wants_doc
@doc_enabled = !!wants_doc
wants_doc
end

def parse
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ module Crystal
end

def private=(set_private)
set_private
end

# Returns true if *name* if an unbound type variable in this (generic) type.
Expand Down Expand Up @@ -1217,6 +1218,7 @@ module Crystal
def superclass=(@superclass)
@depth = superclass ? (superclass.depth + 1) : 0
parents.push superclass if superclass
superclass
end

def add_subclass(subclass)
Expand Down
1 change: 1 addition & 0 deletions src/compress/zip/checksum_writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Compress::Zip
def io=(@io)
@count = 0_u32
@crc32 = ::Digest::CRC32.initial
io
end
end
end
1 change: 1 addition & 0 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Crystal::System::FileDescriptor
new_flags |= LibC::O_NONBLOCK
end
fcntl(LibC::F_SETFL, new_flags) unless new_flags == current_flags
value
end

private def system_close_on_exec?
Expand Down
2 changes: 2 additions & 0 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module Crystal::System::FileDescriptor

private def system_blocking=(blocking)
raise NotImplementedError.new("Crystal::System::FileDescriptor#system_blocking=") unless blocking
blocking
end

private def system_close_on_exec?
Expand All @@ -45,6 +46,7 @@ module Crystal::System::FileDescriptor

private def system_close_on_exec=(close_on_exec)
raise NotImplementedError.new("Crystal::System::FileDescriptor#system_close_on_exec=") if close_on_exec
close_on_exec
end

private def system_closed?
Expand Down
8 changes: 8 additions & 0 deletions src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class HTTP::Client
# ```
def read_timeout=(read_timeout : Number)
@read_timeout = read_timeout.to_f
read_timeout
end

# Sets the read timeout with a `Time::Span`, to wait when reading before raising an `IO::TimeoutError`.
Expand All @@ -298,18 +299,21 @@ class HTTP::Client
# ```
def read_timeout=(read_timeout : Time::Span)
self.read_timeout = read_timeout.total_seconds
read_timeout
end

# Sets the write timeout - if any chunk of request is not written
# within the number of seconds provided, `IO::TimeoutError` exception is raised.
def write_timeout=(write_timeout : Number)
@write_timeout = write_timeout.to_f
write_timeout
end

# Sets the write timeout - if any chunk of request is not written
# within the provided `Time::Span`, `IO::TimeoutError` exception is raised.
def write_timeout=(write_timeout : Time::Span)
self.write_timeout = write_timeout.total_seconds
write_timeout
end

# Sets the number of seconds to wait when connecting, before raising an `IO::TimeoutError`.
Expand All @@ -327,6 +331,7 @@ class HTTP::Client
# ```
def connect_timeout=(connect_timeout : Number)
@connect_timeout = connect_timeout.to_f
connect_timeout
end

# Sets the open timeout with a `Time::Span` to wait when connecting, before raising an `IO::TimeoutError`.
Expand All @@ -344,6 +349,7 @@ class HTTP::Client
# ```
def connect_timeout=(connect_timeout : Time::Span)
self.connect_timeout = connect_timeout.total_seconds
connect_timeout
end

# **This method has no effect right now**
Expand All @@ -363,6 +369,7 @@ class HTTP::Client
# ```
def dns_timeout=(dns_timeout : Number)
@dns_timeout = dns_timeout.to_f
dns_timeout
end

# **This method has no effect right now**
Expand All @@ -382,6 +389,7 @@ class HTTP::Client
# ```
def dns_timeout=(dns_timeout : Time::Span)
self.dns_timeout = dns_timeout.total_seconds
dns_timeout
end

# Adds a callback to execute before each request. This is usually
Expand Down
1 change: 1 addition & 0 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ module HTTP
# ```
def []=(key, value : String)
self[key] = Cookie.new(key, value)
value
end

# Sets a new cookie in the collection to the given `HTTP::Cookie`
Expand Down
1 change: 1 addition & 0 deletions src/http/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ module HTTP
in String then [value]
in Array(String) then value
end
value
end

# Returns all values for specified param *name*.
Expand Down
4 changes: 4 additions & 0 deletions src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class HTTP::Request

def content_length=(length : Int)
headers["Content-Length"] = length.to_s
length
end

def content_length
Expand All @@ -81,18 +82,21 @@ class HTTP::Request
def body=(body : String)
@body = IO::Memory.new(body)
self.content_length = body.bytesize
body
end

def body=(body : Bytes)
@body = IO::Memory.new(body)
self.content_length = body.size
body
end

def body=(@body : IO)
end

def body=(@body : Nil)
@headers["Content-Length"] = "0" if @method == "POST" || @method == "PUT"
body
end

def to_io(io)
Expand Down
1 change: 1 addition & 0 deletions src/http/server/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class HTTP::Server
# Convenience method to set the `Content-Length` header.
def content_length=(content_length : Int)
headers["Content-Length"] = content_length.to_s
content_length
end

# Convenience method to retrieve the HTTP status code.
Expand Down
3 changes: 3 additions & 0 deletions src/io/buffered.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ module IO::Buffered
def sync=(sync)
flush if sync && !@sync
@sync = !!sync
sync
end

# Determines if this `IO` does write buffering. If `true`, no buffering is done.
Expand All @@ -195,6 +196,7 @@ module IO::Buffered
# Turns on/off `IO` **read** buffering.
def read_buffering=(read_buffering)
@read_buffering = !!read_buffering
read_buffering
end

# Determines whether this `IO` buffers reads.
Expand All @@ -205,6 +207,7 @@ module IO::Buffered
# Turns on/off flushing the underlying `IO` when a newline is written.
def flush_on_newline=(flush_on_newline)
@flush_on_newline = !!flush_on_newline
flush_on_newline
end

# Determines if this `IO` flushes automatically when a newline is written.
Expand Down
1 change: 1 addition & 0 deletions src/io/memory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class IO::Memory < IO
raise ArgumentError.new("Negative pos") if value < 0

@pos = value.to_i
value
end

# Yields an `IO::Memory` to read a section of this `IO`'s buffer.
Expand Down
2 changes: 2 additions & 0 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class JSON::Builder
else
@indent = string
end
string
end

# Sets the indent *level* (number of spaces).
Expand All @@ -281,6 +282,7 @@ class JSON::Builder
else
@indent = " " * level
end
level
end

# Returns `true` if the next thing that must pushed into this
Expand Down
2 changes: 2 additions & 0 deletions src/llvm/function.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct LLVM::Function

def call_convention=(cc)
LibLLVM.set_function_call_convention(self, cc)
cc
end

def add_attribute(attribute : Attribute, index = AttributeIndex::FunctionIndex)
Expand Down Expand Up @@ -78,6 +79,7 @@ struct LLVM::Function

def personality_function=(fn)
LibLLVM.set_personality_fn(self, fn)
fn
end

def delete
Expand Down
3 changes: 3 additions & 0 deletions src/llvm/module.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class LLVM::Module

def name=(name : String)
LibLLVM.set_module_identifier(self, name, name.bytesize)
name
end
{% end %}

def target=(target)
LibLLVM.set_target(self, target)
target
end

def data_layout=(data : TargetData)
Expand All @@ -41,6 +43,7 @@ class LLVM::Module
# LLVM >= 3.9
LibLLVM.set_module_data_layout(self, data)
{% end %}
data
end

def dump
Expand Down
Loading