From 71d6e9b63fa06b3ace5207fc0ea1f1d20a2c6089 Mon Sep 17 00:00:00 2001 From: Caspian Baska Date: Tue, 22 Jun 2021 03:16:25 +1000 Subject: [PATCH] Use type instead of `is_a?` in filters (#10815) * Use type instead of `is_a?` in filters * Revert union types in filters --- src/compiler/crystal/codegen/cond.cr | 2 +- src/compiler/crystal/codegen/llvm_id.cr | 2 +- src/compiler/crystal/codegen/types.cr | 2 +- src/compiler/crystal/semantic/cleanup_transformer.cr | 2 +- src/compiler/crystal/semantic/exhaustiveness_checker.cr | 2 +- src/compiler/crystal/semantic/new.cr | 2 +- src/compiler/crystal/semantic/type_lookup.cr | 2 +- src/compiler/crystal/tools/doc/type.cr | 2 +- src/compiler/crystal/tools/print_hierarchy.cr | 4 ++-- src/indexable.cr | 4 ++-- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/compiler/crystal/codegen/cond.cr b/src/compiler/crystal/codegen/cond.cr index 0572556136fa..900aa7636b8a 100644 --- a/src/compiler/crystal/codegen/cond.cr +++ b/src/compiler/crystal/codegen/cond.cr @@ -31,7 +31,7 @@ class Crystal::CodeGenVisitor has_nil = union_types.any? &.nil_type? has_bool = union_types.any? &.bool_type? - has_pointer = union_types.any? &.is_a?(PointerInstanceType) + has_pointer = union_types.any?(PointerInstanceType) cond = llvm_true diff --git a/src/compiler/crystal/codegen/llvm_id.cr b/src/compiler/crystal/codegen/llvm_id.cr index 7cce56582b31..a37469efdc66 100644 --- a/src/compiler/crystal/codegen/llvm_id.cr +++ b/src/compiler/crystal/codegen/llvm_id.cr @@ -164,7 +164,7 @@ module Crystal end private def subclasses_of(type) - type.subclasses.reject(&.is_a?(GenericInstanceType)) + type.subclasses.reject(GenericInstanceType) end end end diff --git a/src/compiler/crystal/codegen/types.cr b/src/compiler/crystal/codegen/types.cr index b1ab01709d56..95b7223f3c52 100644 --- a/src/compiler/crystal/codegen/types.cr +++ b/src/compiler/crystal/codegen/types.cr @@ -150,7 +150,7 @@ module Crystal class UnionType def expand_union_types - if union_types.any?(&.is_a?(NonGenericModuleType)) + if union_types.any?(NonGenericModuleType) types = [] of Type union_types.each &.append_to_expand_union_types(types) types diff --git a/src/compiler/crystal/semantic/cleanup_transformer.cr b/src/compiler/crystal/semantic/cleanup_transformer.cr index bec5189dab34..a7b48cca4fa7 100644 --- a/src/compiler/crystal/semantic/cleanup_transformer.cr +++ b/src/compiler/crystal/semantic/cleanup_transformer.cr @@ -211,7 +211,7 @@ module Crystal def has_enum_type?(type) if type.is_a?(UnionType) - type.union_types.any? &.is_a?(EnumType) + type.union_types.any?(EnumType) else type.is_a?(EnumType) end diff --git a/src/compiler/crystal/semantic/exhaustiveness_checker.cr b/src/compiler/crystal/semantic/exhaustiveness_checker.cr index ee8fe37d453c..15bd7297a5a3 100644 --- a/src/compiler/crystal/semantic/exhaustiveness_checker.cr +++ b/src/compiler/crystal/semantic/exhaustiveness_checker.cr @@ -51,7 +51,7 @@ struct Crystal::ExhaustivenessChecker # If we covered all types, we are done. return if targets.empty? - if targets.all?(&.is_a?(TypeTarget)) && all_patterns_are_types + if targets.all?(TypeTarget) && all_patterns_are_types node.raise <<-MSG case is not exhaustive. diff --git a/src/compiler/crystal/semantic/new.cr b/src/compiler/crystal/semantic/new.cr index b3e10f02719b..65949c705d36 100644 --- a/src/compiler/crystal/semantic/new.cr +++ b/src/compiler/crystal/semantic/new.cr @@ -71,7 +71,7 @@ module Crystal has_self_initialize_methods = !self_initialize_methods.empty? if !has_self_initialize_methods is_generic = type.is_a?(GenericClassType) - inherits_from_generic = type.ancestors.any?(&.is_a?(GenericClassInstanceType)) + inherits_from_generic = type.ancestors.any?(GenericClassInstanceType) if is_generic || inherits_from_generic has_default_self_new = self_new_methods.any? do |a_def| a_def.args.empty? && !a_def.yields diff --git a/src/compiler/crystal/semantic/type_lookup.cr b/src/compiler/crystal/semantic/type_lookup.cr index a3020ff397e8..de715c934d4c 100644 --- a/src/compiler/crystal/semantic/type_lookup.cr +++ b/src/compiler/crystal/semantic/type_lookup.cr @@ -280,7 +280,7 @@ class Crystal::Type end begin - if instance_type.is_a?(GenericUnionType) && type_vars.any? &.is_a?(TypeSplat) + if instance_type.is_a?(GenericUnionType) && type_vars.any?(TypeSplat) # In the case of `Union(*T)`, we don't need to instantiate the union right # now because it will just return `*T`, but what we want to expand the # union types only when the type is instantiated. diff --git a/src/compiler/crystal/tools/doc/type.cr b/src/compiler/crystal/tools/doc/type.cr index fe0801093b0e..fb09c58c1ef8 100644 --- a/src/compiler/crystal/tools/doc/type.cr +++ b/src/compiler/crystal/tools/doc/type.cr @@ -577,7 +577,7 @@ class Crystal::Doc::Type end def type_to_html(type : Crystal::UnionType, io, text = nil, html : HTMLOption = :all) - has_type_splat = type.union_types.any? &.is_a?(TypeSplat) + has_type_splat = type.union_types.any?(TypeSplat) if !has_type_splat && type.union_types.size == 2 if type.union_types[0].nil_type? diff --git a/src/compiler/crystal/tools/print_hierarchy.cr b/src/compiler/crystal/tools/print_hierarchy.cr index 931cd2112733..513a53c2574e 100644 --- a/src/compiler/crystal/tools/print_hierarchy.cr +++ b/src/compiler/crystal/tools/print_hierarchy.cr @@ -58,7 +58,7 @@ module Crystal compute_targets type.types, exp, false - subtypes = type.subclasses.select { |sub| !sub.is_a?(GenericClassInstanceType) } + subtypes = type.subclasses.reject(GenericClassInstanceType) must_include |= compute_targets subtypes, exp, must_include if must_include @targets << type @@ -75,7 +75,7 @@ module Crystal compute_targets type.types, exp, false compute_targets type.instantiated_types, exp, must_include - subtypes = type.subclasses.select { |sub| !sub.is_a?(GenericClassInstanceType) } + subtypes = type.subclasses.reject(GenericClassInstanceType) must_include |= compute_targets subtypes, exp, must_include if must_include @targets << type diff --git a/src/indexable.cr b/src/indexable.cr index 5835762f7fef..d121ec478027 100644 --- a/src/indexable.cr +++ b/src/indexable.cr @@ -333,7 +333,7 @@ module Indexable(T) {% if T == String %} join_strings(separator) {% elsif String < T %} - if all?(&.is_a?(String)) + if all?(String) join_strings(separator) else super(separator) @@ -358,7 +358,7 @@ module Indexable(T) each_with_index do |elem, i| # elem is guaranteed to be a String, but the compiler doesn't know this - # if we enter via the all?(&.is_a?(String)) branch. + # if we enter via the all?(String) branch. elem = elem.to_s # Copy separator to buffer