Skip to content

Commit 7f11a27

Browse files
committed
[GR-45620] Use custom RuboCop policies
PullRequest: truffleruby/3754
2 parents fe68026 + cd6ad87 commit 7f11a27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+463
-183
lines changed

.rubocop.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
require:
2+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_nil.rb
3+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_class.rb
4+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_equal.rb
5+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_kind_of.rb
6+
17
AllCops:
28
TargetRubyVersion: 3.1
39
DisabledByDefault: true
@@ -389,4 +395,28 @@ Layout/SpaceInsideBlockBraces:
389395
# Supports --auto-correct
390396
Layout/SpaceAroundEqualsInParameterDefault:
391397
Description: Checks that the equals signs in parameter default assignments have or don't have surrounding space depending on configuration.
392-
Enabled: true
398+
Enabled: true
399+
400+
# Supports --auto-correct
401+
TruffleRuby/ReplaceWithPrimitiveNil:
402+
Enabled: true
403+
Include: # inspect *only* this directory
404+
- src/main/ruby/**/*.rb
405+
406+
# Supports --auto-correct
407+
TruffleRuby/ReplaceWithPrimitiveObjectClass:
408+
Enabled: true
409+
Include: # inspect *only* this directory
410+
- src/main/ruby/**/*.rb
411+
412+
# Supports --auto-correct
413+
TruffleRuby/ReplaceWithPrimitiveObjectEqual:
414+
Enabled: true
415+
Include: # inspect *only* this directory
416+
- src/main/ruby/**/*.rb
417+
418+
# Supports --auto-correct
419+
TruffleRuby/ReplaceWithPrimitiveObjectKindOf:
420+
Enabled: true
421+
Include: # inspect *only* this directory
422+
- src/main/ruby/**/*.rb

src/main/ruby/truffleruby/core/argf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ARGFClass
5959
# @see #advance!
6060
#
6161
def initialize(argv = ARGV, *others)
62-
@argv = argv.equal?(ARGV) ? ARGV : [argv, *others]
62+
@argv = Primitive.object_equal(argv, ARGV) ? ARGV : [argv, *others]
6363
@lineno = 0
6464
# Setting $. sets ARGF.last_lineno, but does not set
6565
# ARGF.lineno, Almost every operation that sets lineno also sets

src/main/ruby/truffleruby/core/array.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def -(other)
8484

8585
def <=>(other)
8686
other = Truffle::Type.rb_check_convert_type other, Array, :to_ary
87-
return 0 if equal? other
87+
return 0 if Primitive.object_equal(self, other)
8888
return nil if Primitive.nil? other
8989

9090
total = other.size
@@ -125,7 +125,7 @@ def ==(other)
125125
return result
126126
end
127127

128-
return true if equal?(other)
128+
return true if Primitive.object_equal(self, other)
129129
unless Primitive.object_kind_of?(other, Array)
130130
return false unless other.respond_to? :to_ary
131131
return other == self
@@ -323,7 +323,7 @@ def cycle(n = nil)
323323

324324
return nil if empty?
325325

326-
if nil.equal? n
326+
if Primitive.nil?(n)
327327
until empty?
328328
each { |x| yield x }
329329
end
@@ -380,7 +380,7 @@ def eql?(other)
380380
return result
381381
end
382382

383-
return true if equal? other
383+
return true if Primitive.object_equal(self, other)
384384
return false unless Primitive.object_kind_of?(other, Array)
385385
return false if size != other.size
386386

@@ -507,7 +507,7 @@ def flatten!(level = -1)
507507
level = Primitive.rb_num2int level
508508
return nil if level == 0
509509

510-
out = self.class.allocate # new_reserved size
510+
out = Primitive.object_class(self).allocate # new_reserved size
511511
if Primitive.array_flatten_helper(self, out, level)
512512
Primitive.steal_array_storage(self, out)
513513
return self
@@ -1285,7 +1285,7 @@ def to_h
12851285
each_with_index do |elem, i|
12861286
elem = yield(elem) if block_given?
12871287
unless elem.respond_to?(:to_ary)
1288-
raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
1288+
raise TypeError, "wrong element type #{Primitive.object_class(elem)} at #{i} (expected array)"
12891289
end
12901290

12911291
ary = elem.to_ary
@@ -1350,7 +1350,7 @@ def values_at(*args)
13501350

13511351
# Synchronize with Enumerator#zip and Enumerable#zip
13521352
def zip(*others)
1353-
if !block_given? and others.size == 1 and Array === others[0]
1353+
if !block_given? and others.size == 1 and Primitive.object_kind_of?(others[0], Array)
13541354
return Primitive.array_zip self, others[0]
13551355
end
13561356

@@ -1364,7 +1364,7 @@ def zip(*others)
13641364
elsif Primitive.object_respond_to?(other, :each, false)
13651365
other.to_enum(:each)
13661366
else
1367-
raise TypeError, "wrong argument type #{other.class} (must respond to :each)"
1367+
raise TypeError, "wrong argument type #{Primitive.object_class(other)} (must respond to :each)"
13681368
end
13691369
end
13701370

src/main/ruby/truffleruby/core/comparable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
module Comparable
3030
def ==(other)
31-
return true if equal?(other)
31+
return true if Primitive.object_equal(self, other)
3232

3333
return false if Truffle::ThreadOperations.detect_pair_recursion(self, other) do
3434
unless comp = (self <=> other)
@@ -82,7 +82,7 @@ def between?(min, max)
8282
def clamp(min, max = undefined)
8383
if Primitive.undefined?(max)
8484
range = min
85-
raise TypeError, "wrong argument type #{range.class} (expected Range)" unless Primitive.object_kind_of?(range, Range)
85+
raise TypeError, "wrong argument type #{Primitive.object_class(range)} (expected Range)" unless Primitive.object_kind_of?(range, Range)
8686
raise ArgumentError, 'cannot clamp with an exclusive range' if range.exclude_end?
8787
min, max = range.begin, range.end
8888
end

src/main/ruby/truffleruby/core/complex.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ def ==(other)
199199

200200
def eql?(other)
201201
Primitive.object_kind_of?(other, Complex) and
202-
imag.class == other.imag.class and
203-
real.class == other.real.class and
202+
Primitive.object_class(imag) == Primitive.object_class(other.imag) and
203+
Primitive.object_class(real) == Primitive.object_class(other.real) and
204204
self == other
205205
end
206206

@@ -210,7 +210,7 @@ def coerce(other)
210210
elsif Primitive.object_kind_of?(other, Complex)
211211
[other, self]
212212
else
213-
raise TypeError, "#{other.class} can't be coerced into Complex"
213+
raise TypeError, "#{Primitive.object_class(other)} can't be coerced into Complex"
214214
end
215215
end
216216

@@ -268,7 +268,7 @@ def rationalize(eps = nil)
268268
def to_s
269269
result = real.to_s
270270

271-
if imag < 0 || imag.equal?(-0.0)
271+
if imag < 0 || Primitive.object_equal(imag, -0.0)
272272
result << '-'
273273
else
274274
result << '+'
@@ -305,7 +305,7 @@ def inspect
305305
end
306306

307307
def fdiv(other)
308-
raise TypeError, "#{other.class} can't be coerced into Complex" unless Primitive.object_kind_of?(other, Numeric)
308+
raise TypeError, "#{Primitive.object_class(other)} can't be coerced into Complex" unless Primitive.object_kind_of?(other, Numeric)
309309

310310
# FIXME
311311
self / other

src/main/ruby/truffleruby/core/dir.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def each_child
141141
end
142142

143143
def inspect
144-
"#<#{self.class}:#{@path}>"
144+
"#<#{Primitive.object_class(self)}:#{@path}>"
145145
end
146146

147147
class << self

src/main/ruby/truffleruby/core/dir_glob.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def self.single_compile(glob, flags = 0)
458458
end
459459

460460
def self.run(node, all_matches, glob_base_dir, flags = 0)
461-
if ConstantEntry === node
461+
if Primitive.object_kind_of?(node, ConstantEntry)
462462
node.process_directory all_matches, nil, nil, glob_base_dir
463463
else
464464
matches = []

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def to_h(*arg)
319319
each_with_index(*arg) do |elem, i|
320320
elem = yield(elem) if block_given?
321321
unless elem.respond_to?(:to_ary)
322-
raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
322+
raise TypeError, "wrong element type #{Primitive.object_class(elem)} at #{i} (expected array)"
323323
end
324324

325325
ary = elem.to_ary
@@ -342,7 +342,7 @@ def zip(*enums)
342342
elsif Primitive.object_respond_to?(enum, :each, false)
343343
enum.to_enum(:each)
344344
else
345-
raise TypeError, "wrong argument type #{enum.class} (must respond to :each)"
345+
raise TypeError, "wrong argument type #{Primitive.object_class(enum)} (must respond to :each)"
346346
end
347347
end
348348

@@ -376,7 +376,7 @@ def zip(*enums)
376376
def each_with_index(*args, &block)
377377
return to_enum(:each_with_index, *args) { enumerator_size } unless block_given?
378378

379-
if Array === self
379+
if Primitive.object_kind_of?(self, Array)
380380
Primitive.array_each_with_index(self, block)
381381
else
382382
idx = 0
@@ -798,7 +798,7 @@ def min_max(relative)
798798
else
799799
comp = block_given? ? yield(o, chosen) : o <=> chosen
800800
unless comp
801-
raise ArgumentError, "comparison of #{o.class} with #{chosen} failed"
801+
raise ArgumentError, "comparison of #{Primitive.object_class(o)} with #{chosen} failed"
802802
end
803803

804804
if (Comparable.compare_int(comp) <=> 0) == relative

src/main/ruby/truffleruby/core/enumerator.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ class Enumerator
7070
end
7171

7272
def inspect
73-
return "#<#{self.class}: uninitialized>" if @object.nil? && @iter.nil?
73+
return "#<#{Primitive.object_class(self)}: uninitialized>" if Primitive.nil?(@object) && Primitive.nil?(@iter)
7474

7575
args = @kwargs.empty? ? @args : [*@args, @kwargs]
7676
args = args.empty? ? '' : "(#{args.map(&:inspect).join(', ')})"
77-
"#<#{self.class}: #{@object.inspect}:#{@iter}#{args}>"
77+
"#<#{Primitive.object_class(self)}: #{@object.inspect}:#{@iter}#{args}>"
7878
end
7979

8080
def each(*args, **kwargs, &block)
@@ -222,7 +222,7 @@ def self.produce(initial = nil)
222222
# Taken from https://github.com/zverok/enumerator_generate
223223
raise ArgumentError, 'No block given' unless block_given?
224224
Enumerator.new(Float::INFINITY) do |y|
225-
val = initial == nil ? yield() : initial
225+
val = Primitive.nil?(initial) ? yield() : initial
226226

227227
loop do
228228
y << val
@@ -538,7 +538,7 @@ def zip(*lists)
538538
elsif Primitive.object_respond_to?(list, :each, false)
539539
list.to_enum :each
540540
else
541-
raise TypeError, "wrong argument type #{list.class} (must respond to :each)"
541+
raise TypeError, "wrong argument type #{Primitive.object_class(list)} (must respond to :each)"
542542
end
543543
end
544544

@@ -795,8 +795,8 @@ def rewind
795795
end
796796

797797
def inspect
798-
return "#<#{self.class.name}: ...>" if Truffle::ThreadOperations.detect_recursion(self) do
799-
return "#<#{self.class.name}: #{@enums || "uninitialized"}>"
798+
return "#<#{Primitive.object_class(self).name}: ...>" if Truffle::ThreadOperations.detect_recursion(self) do
799+
return "#<#{Primitive.object_class(self).name}: #{@enums || "uninitialized"}>"
800800
end
801801
end
802802
end

src/main/ruby/truffleruby/core/env.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def rehash
243243
end
244244

245245
def replace(other)
246-
return self if equal?(other)
246+
return self if Primitive.object_equal(self, other)
247247
other = Truffle::Type.rb_convert_type(other, Hash, :to_hash)
248248
keys_to_delete = keys
249249
other.each do |k, v|
@@ -281,7 +281,7 @@ def to_h
281281
each_pair do |*elem|
282282
elem = yield(elem)
283283
unless elem.respond_to?(:to_ary)
284-
raise TypeError, "wrong element type #{elem.class} (expected array)"
284+
raise TypeError, "wrong element type #{Primitive.object_class(elem)} (expected array)"
285285
end
286286

287287
ary = elem.to_ary
@@ -295,7 +295,7 @@ def to_h
295295
end
296296

297297
def update(other)
298-
return self if equal?(other)
298+
return self if Primitive.object_equal(self, other)
299299
other = Truffle::Type.rb_convert_type(other, Hash, :to_hash)
300300
if block_given?
301301
other.each do |k, v|

0 commit comments

Comments
 (0)