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

Lint: Use Object#in? instead of multiple comparisons #12675

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
29 changes: 17 additions & 12 deletions samples/sdl/raytracer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,24 @@ SDL.hide_cursor
surface = SDL.set_video_mode WIDTH, HEIGHT, 32, LibSDL::DOUBLEBUF | LibSDL::HWSURFACE | LibSDL::ASYNCBLIT

first = true
while true
SDL.poll_events do |event|
if event.type == LibSDL::QUIT || event.type == LibSDL::KEYDOWN
SDL.quit
exit

begin
while true
SDL.poll_events do |event|
if event.type.in?(LibSDL::QUIT, LibSDL::KEYDOWN)
SDL.quit
exit
end
end
end

if first
start = SDL.ticks
render scene, surface
ms = SDL.ticks - start
puts "Rendered in #{ms} ms"
first = false
if first
start = SDL.ticks
render scene, surface
ms = SDL.ticks - start
puts "Rendered in #{ms} ms"
first = false
end
end
ensure
SDL.quit
end
2 changes: 1 addition & 1 deletion samples/sdl/tv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ puts "Rects: #{rects.size}"
begin
while true
SDL.poll_events do |event|
if event.type == LibSDL::QUIT || event.type == LibSDL::KEYDOWN
if event.type.in?(LibSDL::QUIT, LibSDL::KEYDOWN)
ms = SDL.ticks - start
puts "#{frames} frames in #{ms} ms"
puts "Average FPS: #{frames / (ms * 0.001)}"
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/codegen/union_type_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe "Code gen: union type" do
a = 1 || 1.5
foo(a)
)).to_string
(str == "(Int32 | Float64)" || str == "(Float64 | Int32)").should be_true
str.in?("(Int32 | Float64)", "(Float64 | Int32)").should be_true
end

it "provides T as a tuple literal" do
Expand Down
6 changes: 3 additions & 3 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ describe "String" do
it { "bcdaaa".strip('a').should eq("bcd") }
it { "aaabcd".strip('a').should eq("bcd") }

it { "ababcdaba".strip { |c| c == 'a' || c == 'b' }.should eq("cd") }
it { "ababcdaba".strip(&.in?('a', 'b')).should eq("cd") }
end

describe "rstrip" do
Expand All @@ -845,7 +845,7 @@ describe "String" do
it { "foobarrrr".rstrip('r').should eq("fooba") }
it { "foobar".rstrip('x').should eq("foobar") }

it { "foobar".rstrip { |c| c == 'a' || c == 'r' }.should eq("foob") }
it { "foobar".rstrip(&.in?('a', 'r')).should eq("foob") }

it "does not touch invalid code units in an otherwise ascii string" do
" \xA0 ".rstrip.should eq(" \xA0")
Expand All @@ -869,7 +869,7 @@ describe "String" do
it { "bbbbarfoo".lstrip('b').should eq("arfoo") }
it { "barfoo".lstrip('x').should eq("barfoo") }

it { "barfoo".lstrip { |c| c == 'a' || c == 'b' }.should eq("rfoo") }
it { "barfoo".lstrip(&.in?('a', 'b')).should eq("rfoo") }

it "does not touch invalid code units in an otherwise ascii string" do
" \xA0 ".lstrip.should eq("\xA0 ")
Expand Down
2 changes: 1 addition & 1 deletion src/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ module Base64
bytes_begin = bytes

# Get the position of the last valid base64 character (rstrip '\n', '\r' and '=')
while (size > 0) && (sym = bytes[size - 1]) && (sym == NL || sym == NR || sym == PAD)
while (size > 0) && (sym = bytes[size - 1]) && sym.in?(NL, NR, PAD)
size -= 1
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Crystal::Command
when command == "eval"
options.shift
eval
when command == "i" || command == "interactive"
when command.in?("i", "interactive")
options.shift
{% if flag?(:without_interpreter) %}
STDERR.puts "Crystal was compiled without interpreter support"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ class Crystal::Call
args["self"] = MetaVar.new("self", self_type)
end

strict_check = body.is_a?(Primitive) && (body.name == "proc_call" || body.name == "pointer_set")
strict_check = body.is_a?(Primitive) && body.name.in?("proc_call", "pointer_set")

arg_types.each_index do |index|
arg = typed_def.args[index]
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Crystal::Call
# Another special case: `new` and `initialize` are only looked up one level,
# so we must find the first one defined.
new_owner = owner
while defs.empty? && (def_name == "initialize" || def_name == "new")
while defs.empty? && def_name.in?("initialize", "new")
new_owner = new_owner.superclass
if new_owner
defs = new_owner.lookup_defs(def_name)
Expand Down Expand Up @@ -106,7 +106,7 @@ class Crystal::Call
end

# If it's on an initialize method and there's a similar method name, it's probably a typo
if (def_name == "initialize" || def_name == "new") && (similar_def = owner.instance_type.lookup_similar_def("initialize", self.args.size, block))
if def_name.in?("initialize", "new") && (similar_def = owner.instance_type.lookup_similar_def("initialize", self.args.size, block))
inner_msg = colorize("do you maybe have a typo in this '#{similar_def.name}' method?").yellow.bold.to_s
inner_exception = TypeException.for_node(similar_def, inner_msg)
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/type_guess_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ module Crystal
# If it's Pointer(T).malloc or Pointer(T).null, guess it to Pointer(T)
if obj.is_a?(Generic) &&
(name = obj.name).is_a?(Path) && name.single?("Pointer") &&
(node.name == "malloc" || node.name == "null")
node.name.in?("malloc", "null")
type = lookup_type?(obj)
return type if type.is_a?(PointerInstanceType)
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ module Crystal
end

def has_sign?
@value[0] == '+' || @value[0] == '-'
@value[0].in?('+', '-')
end

def integer_value
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ module Crystal
while ident_part?(next_char)
# Nothing to do
end
if current_char == '?' || ((current_char == '!' || current_char == '=') && peek_next_char != '=')
if current_char == '?' || (current_char.in?('!', '=') && peek_next_char != '=')
next_char
end
@token.type = :SYMBOL
Expand Down Expand Up @@ -1450,7 +1450,7 @@ module Crystal
while ident_part?(current_char)
next_char
end
if (current_char == '?' || current_char == '!') && peek_next_char != '='
if current_char.in?('?', '!') && peek_next_char != '='
next_char
end
@token.type = :IDENT
Expand Down Expand Up @@ -1896,7 +1896,7 @@ module Crystal
end

if reached_end &&
(current_char == '\n' || current_char == '\0' ||
(current_char.in?('\n', '\0') ||
(current_char == '\r' && peek_next_char == '\n' && next_char))
@token.type = :DELIMITER_END
@token.delimiter_state = delimiter_state.with_heredoc_indent(indent)
Expand Down Expand Up @@ -2251,7 +2251,7 @@ module Crystal
whitespace = false
beginning_of_line = true
else
whitespace = char.ascii_whitespace? || char == ';' || char == '(' || char == '[' || char == '{'
whitespace = char.ascii_whitespace? || char.in?(';', '(', '[', '{')
if beginning_of_line && !whitespace
beginning_of_line = false
end
Expand Down Expand Up @@ -2907,7 +2907,7 @@ module Crystal
private delegate ident_start?, ident_part?, to: Lexer

def ident_part_or_end?(char)
ident_part?(char) || char == '?' || char == '!'
ident_part?(char) || char.in?('?', '!')
end

def peek_not_ident_part_or_end_next_char
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ module Crystal
!exp.has_parentheses? && (
(exp.args.empty? && !exp.named_args) ||
Lexer.setter?(exp.name) ||
exp.name == "[]" || exp.name == "[]="
exp.name.in?("[]", "[]=")
)
else
false
Expand Down Expand Up @@ -2218,7 +2218,7 @@ module Crystal
this_piece_is_in_new_line = line_number != previous_line_number
next_piece_is_in_new_line = i == pieces.size - 1 || pieces[i + 1].line_number != line_number
if value.is_a?(String)
if value == "\n" || value == "\r\n"
if value.in?("\n", "\r\n")
current_line << value
if this_piece_is_in_new_line || next_piece_is_in_new_line
line = current_line.to_s
Expand Down Expand Up @@ -4298,7 +4298,7 @@ module Crystal
while current_char.ascii_whitespace?
next_char_no_column_increment
end
comes_plus_or_minus = current_char == '+' || current_char == '-'
comes_plus_or_minus = current_char.in?('+', '-')
self.current_pos = pos
comes_plus_or_minus
end
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ module Crystal
# If there's no '.' nor 'e', for example in `1_f64`,
# we need to include it (#3315)
node.value.each_char do |char|
if char == '.' || char == 'e'
return false
end
return false if char.in?('.', 'e')
end

true
Expand Down Expand Up @@ -347,7 +345,7 @@ module Crystal
node_obj = nil
end

if node_obj && (node.name == "[]" || node.name == "[]?") && !block
if node_obj && node.name.in?("[]", "[]?") && !block
in_parenthesis(need_parens, node_obj)

@str << "["
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,7 @@ module Crystal
base_indent = @indent

# Special case: $1, $2, ...
if @token.type.global_match_data_index? && (node.name == "[]" || node.name == "[]?") && obj.is_a?(Global)
if @token.type.global_match_data_index? && node.name.in?("[]", "[]?") && obj.is_a?(Global)
write "$"
write @token.value
next_token
Expand Down Expand Up @@ -2663,7 +2663,7 @@ module Crystal
end

# This is for foo &.[bar] and &.[bar]?, or foo.[bar] and foo.[bar]?
if (node.name == "[]" || node.name == "[]?") && @token.type.op_lsquare?
if node.name.in?("[]", "[]?") && @token.type.op_lsquare?
write "["
next_token_skip_space_or_newline
format_call_args(node, false, base_indent)
Expand Down Expand Up @@ -4741,7 +4741,7 @@ module Crystal
@current_doc_comment = nil
else
# Normalize crystal language tag
if language == "cr" || language == "crystal"
if language.in?("cr", "crystal")
value = value.rchop(language)
language = ""
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module Crystal
when !name[0].ascii_letter? then raise Error.new("NAME must start with a letter")
when name.index("--") then raise Error.new("NAME must not have consecutive dashes")
when name.index("__") then raise Error.new("NAME must not have consecutive underscores")
when !name.each_char.all? { |c| c.alphanumeric? || c == '-' || c == '_' }
when !name.each_char.all? { |c| c.alphanumeric? || c.in?('-', '_') }
raise Error.new("NAME must only contain alphanumerical characters, underscores or dashes")
else
# name is valid
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/playground/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module Crystal::Playground
File.read(@filename)
end

if extname == ".md" || extname == ".cr"
if extname.in?(".md", ".cr")
content = Markd.to_html(content)
end
content
Expand Down Expand Up @@ -534,7 +534,7 @@ module Crystal::Playground
private def accept_request?(origin)
case @host
when nil, "localhost", "127.0.0.1"
origin == "http://127.0.0.1:#{@port}" || origin == "http://localhost:#{@port}"
origin.in?("http://localhost:#{@port}", "http://127.0.0.1:#{@port}")
when "0.0.0.0"
true
else
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/mach_o.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module Crystal

private def read_magic
magic = @io.read_bytes(UInt32)
unless magic == MAGIC_64 || magic == CIGAM_64 || magic == MAGIC || magic == CIGAM
unless magic.in?(MAGIC_64, CIGAM_64, MAGIC, CIGAM)
raise Error.new("Invalid magic number")
end
magic
Expand All @@ -123,7 +123,7 @@ module Crystal
end

def endianness
if @magic == MAGIC_64 || @magic == MAGIC
if @magic.in?(MAGIC_64, MAGIC)
IO::ByteFormat::SystemEndian
elsif IO::ByteFormat::SystemEndian == IO::ByteFormat::LittleEndian
IO::ByteFormat::BigEndian
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/getrandom.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module Crystal::System::Random
read_bytes = Syscall.getrandom(buf.to_unsafe, LibC::SizeT.new(buf.size), Syscall::GRND_NONBLOCK)
if read_bytes < 0
err = Errno.new(-read_bytes.to_i)
if err == Errno::EINTR || err == Errno::EAGAIN
if err.in?(Errno::EINTR, Errno::EAGAIN)
::Fiber.yield
else
raise RuntimeError.from_os_error("getrandom", err)
Expand Down
2 changes: 1 addition & 1 deletion src/csv/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ abstract class CSV::Lexer
private def next_char
@column_number += 1
char = next_char_no_column_increment
if char == '\n' || char == '\r'
if char.in?('\n', '\r')
@column_number = 0
@line_number += 1
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 @@ -147,7 +147,7 @@ class Dir

private def self.constant_entry?(file)
file.each_char do |char|
return false if char == '*' || char == '?'
return false if char.in?('*', '?')
end

true
Expand Down Expand Up @@ -325,7 +325,7 @@ class Dir
private def self.each_child(path)
Dir.open(path || Dir.current) do |dir|
while entry = read_entry(dir)
next if entry.name == "." || entry.name == ".."
next if entry.name.in?(".", "..")
yield entry
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module Enumerable(T)

def same_as?(key) : Bool
return false unless @initialized
return false if key == Alone || key == Drop
return false if key.in?(Alone, Drop)
@key == key
end

Expand Down
2 changes: 1 addition & 1 deletion src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class HTTP::Request
end

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

def to_io(io)
Expand Down
2 changes: 1 addition & 1 deletion src/json/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ abstract class JSON::Lexer
char = next_char
end

if char == 'e' || char == 'E'
if char.in?('e', 'E')
consume_exponent
else
@token.kind = :float
Expand Down
4 changes: 2 additions & 2 deletions src/llvm/abi/x86_64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class LLVM::ABI::X86_64 < LLVM::ABI
return false if cls.empty?

cl = cls.first
cl == RegClass::Memory || cl == RegClass::X87 || cl == RegClass::ComplexX87
cl.in?(RegClass::Memory, RegClass::X87, RegClass::ComplexX87)
end

def sret?(cls) : Bool
Expand Down Expand Up @@ -161,7 +161,7 @@ class LLVM::ABI::X86_64 < LLVM::ABI
i = 0
ty_kind = ty.kind
e = cls.size
if e > 2 && (ty_kind == Type::Kind::Struct || ty_kind == Type::Kind::Array)
if e > 2 && ty_kind.in?(Type::Kind::Struct, Type::Kind::Array)
if cls[i].sse?
i += 1
while i < e
Expand Down
2 changes: 1 addition & 1 deletion src/mime/multipart/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module MIME::Multipart

0.upto(transport_padding_crlf.bytesize - 3) do |i| # 3 constant to ignore "\r\n" at end
byte = transport_padding_crlf.to_unsafe[i]
fail("padding contained non-whitespace character") unless byte == ' '.ord || byte == '\t'.ord
fail("padding contained non-whitespace character") unless byte.in?(' '.ord, '\t'.ord)
end
end

Expand Down
Loading