Search code examples
ruby-on-railsrubyrubocop

How can I warn about Ruby 3 deprecated code, while using Ruby 2.7 with Rubocop?


I'm working on a huge repo with many contributors, and many code changes happen daily. I'm tasked to update current Ruby 2.7.4 to Ruby 3+, and as a first step I have decided best to add some rules to the already implemented Rubocop linter to check for deprecated code on Ruby 3, so that people will stop adding new code that's deprecated (Or rather that will be deprecated once I upgrade to Ruby 3).

Does Rubocop offer any cops that can help me with this task? Or can someone point me in the right direction?


Solution

  • Rubocop has a few lints to detect deprecated Ruby features. For example, Lint/DeprecatedClassMethods. For most of these Rubocop doesn't care what version of Ruby you're using.

    To find more issues, set your TargetRubyVersion to 3.0 (or whatever your target is).

    AllCops:
      TargetRubyVersion: 3.0
    

    Some cops which might be useful to you are not enabled by default because they're not 100% safe. You may wish to enable them.

    Add this to your automated code review checks.


    You can add your own custom cops to detect more 3.0 deprecated features. Reading the code for the existing cops looking for deprecations will help.

    I don't know of a rubocop extension specifically dedicated to Ruby upgrade cops. You could even bundle this up into rubocop-deprecations.