Rubocop: Lint/UnifiedInteger autocorrection breaks class checks for ruby < 2.4

Created on 15 Apr 2018  路  8Comments  路  Source: rubocop-hq/rubocop

Fixnum and Bignum have been deprecated in ruby 2.4, and starting with ruby 2.4 it's safe to replace them with Integer. Older ruby versions however make a difference, e.g.

1.class == Fixnum
# => true
1.class == Integer
# => false
1_000_000_000_000_000_000_000_000.is_a?(Bignum)
# => true
1_000_000_000_000_000_000_000_000.is_a?(Integer)
# => false

Expected behavior

For TargetRubyVersion < 2.4:
Don't break the class check. One possibility might be to replace the class check with kind_of?(Integer)

Actual behavior

Autocorrection breaks class checks, e.g.

1.class == Fixnum

becomes

1.class == Integer

Steps to reproduce the problem

  • Use a ruby version < 2.4
  • create this test file:
# test.rb
test = 1
if test.class == Fixnum
  # do something
end
  • run rubocop -a test.rb

RuboCop version

$ rubocop -V
0.54.0 (using Parser 2.5.1.0, running on ruby 2.3.4 x86_64-linux)
bug

All 8 comments

The cop is actually missing a minimum_ruby_target_version, which is causing the problem. It was supposed to be a no op on older Rubies.

1_000_000_000_000_000_000_000_000.is_a?(Bignum)
# => true
1_000_000_000_000_000_000_000_000.is_a?(Integer)
# => false

Since Bignum is a subclass of Integer, this behavior is odd 馃

1_000_000_000_000_000_000_000_000.is_a?(Integer)
# => false

Can't reproduce this on any Ruby version between 2.1 and 2.3:

RUBY_VERSION
# => "2.1.0"
1_000_000_000_000_000_000_000_000.is_a?(Integer)
# => true

One correction for:

1.class == Fixnum
# => true

Could be:

1.class < Integer
# => true

I'm ready for PR (23f2652) . However, this PR is not open. Why?

I think that Fixnum and Bignum should not be used also even before Ruby 2.3.

I opened PRs to many Gems supporting Ruby 2.3 or lower when Unified Integer was introduced. Because they can be replaced by superclass Integer.

So it is better to fix the user program using Integer without setting minimum_target_ruby_version to Lint/UnifiedInteger cop. I think this Issue is not a bug but should be closed.

馃摑 I remembered the presentation of a Ruby committer heard at RubyKaigi 2016.
http://rubykaigi.org/2016/presentations/tanaka_akr.html

If there is no objection to the comment above, I'd like to close this issue.

Yeah, let's close it.

Thanks!

Forgive me if I'm missing something, but why was this closed? Shouldn't the autocorrect functionality be removed if the minimum_ruby_target_version isn't met? If I run rubocop --auto-correct on my ruby 2.2 project, this cop breaks my code despite TargetRubyVersion: 2.2 being set.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benoittgt picture benoittgt  路  3Comments

NobodysNightmare picture NobodysNightmare  路  3Comments

bbatsov picture bbatsov  路  3Comments

mikegee picture mikegee  路  3Comments

Ana06 picture Ana06  路  3Comments