diff --git a/docs/404.html b/docs/404.html index d45717a97..0617b707d 100644 --- a/docs/404.html +++ b/docs/404.html @@ -4,7 +4,7 @@ Page Not Found :: RuboCop Docs - + @@ -83,6 +83,9 @@
  • default
  • +
  • + 1.69 +
  • 1.68
  • @@ -447,7 +450,7 @@
    @@ -455,7 +458,7 @@

    Page Not Found

    -

    The page you’re looking for does not exist. It may have been moved. You can return to the start page, or follow one of the links in the navigation to the left.

    +

    The page you’re looking for does not exist. It may have been moved. You can return to the start page, or follow one of the links in the navigation to the left.

    If you arrived on this page by clicking on a link, please notify the owner of the site that the link is broken. diff --git a/docs/index.html b/docs/index.html index b2937a550..67ec18b99 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,9 +1,9 @@ - - - + + + Redirect Notice

    Redirect Notice

    -

    The page you requested has been relocated to https://docs.rubocop.org/rubocop/1.68/index.html.

    +

    The page you requested has been relocated to https://docs.rubocop.org/rubocop/1.69/index.html.

    diff --git a/docs/rubocop-ast/1.0/index.html b/docs/rubocop-ast/1.0/index.html index 495f901e3..1584844f3 100644 --- a/docs/rubocop-ast/1.0/index.html +++ b/docs/rubocop-ast/1.0/index.html @@ -6,7 +6,7 @@ RuboCop AST :: RuboCop Docs - + @@ -116,6 +116,9 @@

    RuboCop AST

  • default
  • +
  • + 1.69 +
  • 1.68
  • @@ -480,7 +483,7 @@

    RuboCop AST

    + +
    + + + + + + + + diff --git a/docs/rubocop/1.69/about/history.html b/docs/rubocop/1.69/about/history.html new file mode 100644 index 000000000..12387b20e --- /dev/null +++ b/docs/rubocop/1.69/about/history.html @@ -0,0 +1,730 @@ + + + + + + History :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    History

    +
    +
    +
    +
    +You don’t know where you’re going until you know where you’ve been. +
    +
    +
    +

    RuboCop was created by Bozhidar Batsov in the spring of 2012, as an attempt +to make it easy to apply consistently the guidelines from the community Ruby Style Guide.[1]

    +
    +
    +
    +
    +

    Notable Milestones

    +
    +
    +
      +
    • +

      12 Sep 2011 - Creation of the community Ruby Style Guide.

      +
    • +
    • +

      21 Apr 2012 - Initial commit by Bozhidar Batsov.

      +
    • +
    • +

      03 May 2012 - RuboCop 0.0.0 is released.

      +
    • +
    • +

      28 May 2013 - RuboCop 0.8 is released. It’s the first version fully powered by the powerful parser gem.

      +
    • +
    • +

      01 Jul 2013 - RuboCop 0.9 introduces autocorrection and output formatters.

      +
    • +
    • +

      31 May 2018 - At RubyKaigi 2018 Bozhidar announces the transition of the project to a RuboCop GitHub organization. He also outlines the plans for a 1.0 release that guide the next couple of years of development.

      +
    • +
    • +

      21 Oct 2020 - RuboCop 1.0 is released (exactly 7.5 years after the first commit).

      +
    • +
    • +

      06 Mar 2024 - RuboCop 1.62 adds support for Prism.

      +
    • +
    +
    +
    +
    +
    +

    Additional Resources

    + +
    +
    +
    +
    +1. This is the GitHub issue that started it all. +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/about/license.html b/docs/rubocop/1.69/about/license.html new file mode 100644 index 000000000..ad96e8111 --- /dev/null +++ b/docs/rubocop/1.69/about/license.html @@ -0,0 +1,686 @@ + + + + + + License & Copyright :: RuboCop Docs + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    License & Copyright

    +
    +

    Code License

    +
    +
    +

    Use of RuboCop is granted under the terms of the MIT license. Check +out the LICENSE.txt file in RuboCop’s code repository for more details.

    +
    +
    +
    +
    +

    Documentation License

    +
    +
    +

    All the original text is available under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0).

    +
    +
    +

    All the original Ruby code is available under the terms of the MIT license.

    +
    +
    +
    +
    + +
    +
    +

    Copyright (c) 2012-2024 Bozhidar Batsov and RuboCop contributors.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/about/logo.html b/docs/rubocop/1.69/about/logo.html new file mode 100644 index 000000000..5c2881586 --- /dev/null +++ b/docs/rubocop/1.69/about/logo.html @@ -0,0 +1,668 @@ + + + + + + Logo :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + + +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/about/team.html b/docs/rubocop/1.69/about/team.html new file mode 100644 index 000000000..c0305a768 --- /dev/null +++ b/docs/rubocop/1.69/about/team.html @@ -0,0 +1,745 @@ + + + + + + Team :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Team

    +
    +

    The Core Team

    +
    +
    +

    The direction of the project and its official extensions is being +stewarded by the RuboCop core team. This group of long-term +contributors manages releases, evaluates pull-requests, and does a lot +of the groundwork on major new features. Here are the current members +of the RuboCop core team, listed in the order of joining it:

    +
    +
    + +
    +
    +
    +
    +

    RuboCop Alumni

    +
    +
    +

    In addition, we’d like to extend a special thanks the following retired RuboCop +core team members. Lovingly known as The Alumni:

    +
    +
    + +
    +
    +
    +
    +

    Joining the Core Team

    +
    +
    +

    We’re always looking for more people to join our Core Team, as there’s plenty of work to go around. +There’s no formal procedure for applying to the team, but it’s fairly straightforward to get an invitation +to join it:

    +
    +
    +
      +
    • +

      contribute consistently over an extended period of time (e.g. 6-12 months)

      +
    • +
    • +

      demonstrate a passion for what we’re doing

      +
    • +
    • +

      eventually an invitation will be extended to you to become part of RuboCop’s team

      +
    • +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/auto_correct.html b/docs/rubocop/1.69/auto_correct.html new file mode 100644 index 000000000..9095a94e9 --- /dev/null +++ b/docs/rubocop/1.69/auto_correct.html @@ -0,0 +1,9 @@ + + + + + + +Redirect Notice +

    Redirect Notice

    +

    The page you requested has been relocated to https://docs.rubocop.org/rubocop/1.69/usage/autocorrect.html.

    diff --git a/docs/rubocop/1.69/automated_code_review.html b/docs/rubocop/1.69/automated_code_review.html new file mode 100644 index 000000000..8a851a68c --- /dev/null +++ b/docs/rubocop/1.69/automated_code_review.html @@ -0,0 +1,744 @@ + + + + + + Automated Code Review :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Automated Code Review

    +
    +
    +
    +

    The section describes SaaS solutions that provide automated code reviews for Ruby based on RuboCop.

    +
    +
    + + + + + +
    + + +The services are listed in alphabetical order. +
    +
    +
    +
    +
    +

    Awesome Code

    +
    +
    +

    Awesome Code improves your code readability by git push or sending pull requests, with one click or even fully automated. It’s an online rubocop -a service.

    +
    +
    +
    +
    +

    Codacy

    +
    +
    +

    Codacy checks your code from style to security, duplication, complexity, and also integrates with coverage. +Codacy is free for open source, and it provides RuboCop analysis out-of-the-box.

    +
    +
    +
    +
    +

    Code Climate

    +
    +
    +

    Code Climate provides automated code review for test coverage, complexity, duplication, security, style, and more, and merge with confidence.

    +
    +
    +
    +
    +

    CodeFactor

    +
    +
    +

    CodeFactor reports various code metrics like duplication, churn, and problems for code style, performance, complexity, and many others. CodeFactor is free for open source. It supports analysis and autocorrection for RuboCop.

    +
    +
    +
    +
    +

    Codety

    +
    +
    +

    Codety detects code issues for 30+ programming languages and IaC frameworks, Codety Scanner is open source and it embeds 6,000+ code analysis rules(including RuboCop rules).

    +
    +
    +
    +
    +

    Hound

    +
    +
    +

    Hound comments on style violations in GitHub pull requests, allowing you and your team to better review and maintain a clean codebase. +It is open source software.

    +
    +
    +
    +
    +

    Pronto

    +
    +
    +

    Pronto does quick automated code review of your changes. Created to be used on GitHub pull requests, but also works locally and integrates with GitLab and Bitbucket.

    +
    +
    +
    +
    +

    ReviewDog

    +
    +
    +

    ReviewDog is similar to Pronto but with better support for GitHub Actions.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/compatibility.html b/docs/rubocop/1.69/compatibility.html new file mode 100644 index 000000000..36935086d --- /dev/null +++ b/docs/rubocop/1.69/compatibility.html @@ -0,0 +1,825 @@ + + + + + + Compatibility :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Compatibility

    +
    +
    +
    +

    RuboCop targets Ruby 2.0+ code analysis.[1]

    +
    +
    +

    RuboCop officially runtime supports MRI (a.k.a. CRuby) and JRuby.

    +
    +
    +
      +
    • +

      MRI 2.7+

      +
    • +
    • +

      JRuby 9.4+

      +
    • +
    +
    +
    +

    The oldest supported JRuby version is derived from the oldest compatible MRI version.

    +
    +
    + + + + + +
    + + +RuboCop might be working with other Ruby implementations as well, but it’s tested only on MRI and JRuby. +
    +
    +
    +
    +
    +

    Support Matrix

    +
    +
    +

    RuboCop generally aims to follow MRI’s own support policy - meaning RuboCop would support all officially supported MRI releases.[2] To give people extra time for a smooth transition, we’ve customarily provided support for about one year after EOL of MRI version. [3]

    +
    +
    + + + + + +
    + + +There are major version incompatibilities between Ruby 2.7 and Ruby 3.0, and the latest stable version of Ruby on Rails, 7.1, still supports Ruby 2.7. Therefore, it might be early for RuboCop to drop support for Ruby 2.7 at this stage. As long as Rails 7.1 is in the "Bug Fix" phase of the Maintenance Policy for Ruby on Rails, RuboCop will continue supporting Ruby 2.7. +
    +
    +
    +

    The following table is the runtime support matrix.

    +
    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Supported runtime Ruby versionLast supported RuboCop version

    1.9

    0.41

    2.0

    0.50

    2.1

    0.57

    2.2

    0.68

    2.3

    0.81

    2.4

    1.12

    2.5

    1.28

    2.6

    1.50

    2.7

    -

    3.0

    -

    3.1

    -

    3.2

    -

    3.3

    -

    3.4 (experimental)

    -

    +
    +

    RuboCop targets Ruby 2.0+ code analysis with Parser gem as a parser since RuboCop 1.30. It restored code analysis support that had been removed earlier by mistake, together with dropping runtime support for unsupported Ruby versions.

    +
    +
    +

    Starting from RuboCop 1.62, support for Prism’s Prism::Translation::parser will enable analysis of Ruby 3.3+. For more details, please refer to the setting ParserEngine.

    +
    +
    + + + + + +
    + + +The compatibility setting TargetRubyVersion is about code analysis (what RuboCop can analyze), not runtime (is RuboCop capable of running on some Ruby or not). +
    +
    +
    +
    +
    +

    Forward Compatibility

    +
    +
    +

    Features may be provided for development version of Ruby. These are all considered experimental, and may change before the stable version of Ruby will be released.

    +
    +
    +
    +
    +
    +
    +1. As defined by its reference implementation MRI. +
    +
    +2. Typically the last 3 releases. +
    +
    +3. At the core team’s discretion this policy might be waived aside for MRI releases causing significant maintenance overhead. +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/configuration.html b/docs/rubocop/1.69/configuration.html new file mode 100644 index 000000000..507576a4a --- /dev/null +++ b/docs/rubocop/1.69/configuration.html @@ -0,0 +1,2020 @@ + + + + + + Configuration :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Configuration

    +
    +
    +
    +

    The behavior of RuboCop can be controlled via the +.rubocop.yml +configuration file. It makes it possible to enable/disable certain cops +(checks) and to alter their behavior if they accept any parameters. The file +can be placed in your home directory, XDG config directory, or in some project +directory.

    +
    +
    +

    The file has the following format:

    +
    +
    +
    +
    inherit_from: ../.rubocop.yml
    +
    +Style/Encoding:
    +  Enabled: false
    +
    +Layout/LineLength:
    +  Max: 99
    +
    +
    +
    + + + + + +
    + + +Qualifying cop name with its type, e.g., Style, is recommended, +but not necessary as long as the cop name is unique across all types. +
    +
    +
    +
    +
    +

    Config file locations

    +
    +
    +

    RuboCop will start looking for the configuration file in the directory +where the inspected file is and continue its way up to the root directory.

    +
    +
    +

    If it cannot be found until reaching the project’s root directory, then it will +be searched for in the .config directory of the project root +and the user’s global config locations. The user’s global config locations consist of a +dotfile or a config file inside the XDG Base Directory +specification.

    +
    +
    +
      +
    • +

      .config/.rubocop.yml or .config/rubocop/config.yml at the project root

      +
    • +
    • +

      ~/.rubocop.yml

      +
    • +
    • +

      $XDG_CONFIG_HOME/rubocop/config.yml (expands to ~/.config/rubocop/config.yml +if $XDG_CONFIG_HOME is not set)

      +
    • +
    +
    +
    +

    If both files exist, the dotfile will be selected.

    +
    +
    +

    As an example, if RuboCop is invoked from inside /path/to/project/lib/utils, +then RuboCop will use the config as specified inside the first of the following +files:

    +
    +
    +
      +
    • +

      /path/to/project/lib/utils/.rubocop.yml

      +
    • +
    • +

      /path/to/project/lib/.rubocop.yml

      +
    • +
    • +

      /path/to/project/.rubocop.yml

      +
    • +
    • +

      /path/to/project/.config/.rubocop.yml

      +
    • +
    • +

      /path/to/project/.config/rubocop/config.yml

      +
    • +
    • +

      /.rubocop.yml

      +
    • +
    • +

      ~/.rubocop.yml

      +
    • +
    • +

      ~/.config/rubocop/config.yml

      +
    • +
    • +

      RuboCop’s default configuration

      +
    • +
    +
    +
    + + + + + +
    + + +All the previous logic does not apply if a specific configuration file is passed +on the command line through the --config flag. In that case, the resolved +configuration file will be the one passed to the CLI. +
    +
    +
    +
    +
    +

    Inheritance

    +
    +
    +

    All configuration inherits from RuboCop’s default configuration (See +"Defaults").

    +
    +
    +

    RuboCop also supports inheritance in user’s configuration files. The most common +example would be the .rubocop_todo.yml file (See "Automatically Generated +Configuration" below).

    +
    +
    +

    Settings in the child file (that which inherits) override those in the parent +(that which is inherited), with the following caveats.

    +
    +
    +

    Inheritance of hashes vs. other types

    +
    +

    Configuration parameters that are hashes, for example PreferredMethods in +Style/CollectionMethods, are merged with the same parameter in the parent +configuration. This means that any key-value pairs given in child configuration +override the same keys in parent configuration. Giving ~, YAML’s +representation of nil, as a value cancels the setting of the corresponding +key in the parent configuration. For example:

    +
    +
    +
    +
    Style/CollectionMethods:
    +  Enabled: true
    +  PreferredMethods:
    +    # No preference for collect, keep all others from default config.
    +    collect: ~
    +
    +
    +
    +

    Other types, such as AllCops / Include (an array), are overridden by the +child setting.

    +
    +
    +

    Arrays override because if they were merged, there would be no way to +remove elements in child files.

    +
    +
    +

    However, advanced users can still merge arrays using the inherit_mode setting. +See "Merging arrays using inherit_mode" below.

    +
    +
    +
    +

    Inheriting from another configuration file in the project

    +
    +

    The optional inherit_from directive is used to include configuration +from one or more files. This makes it possible to have the common +project settings in the .rubocop.yml file at the project root, and +then only the deviations from those rules in the subdirectories. The +files can be given with absolute paths or paths relative to the file +where they are referenced. The settings after an inherit_from +directive override any settings in the file(s) inherited from. When +multiple files are included, the first file in the list has the lowest +precedence and the last one has the highest. The format for multiple +inheritance is:

    +
    +
    +
    +
    inherit_from:
    +  - ../.rubocop.yml
    +  - ../conf/.rubocop.yml
    +
    +
    +
    +

    inherit_from also accepts a glob, for example:

    +
    +
    +
    +
    inherit_from:
    +  - packages/*/.rubocop_todo.yml
    +
    +
    +
    +

    The example above is one potential use-case: allowing components within your repo to organize their own .rubocop_todo.yml files.

    +
    +
    +
    +
    +
    +

    Inheriting configuration from a remote URL

    +
    +
    +

    The optional inherit_from directive can contain a full url to a remote +file. This makes it possible to have common project settings stored on a http +server and shared between many projects.

    +
    +
    +

    The remote config file is cached locally and is only updated if:

    +
    +
    +
      +
    • +

      The file does not exist.

      +
    • +
    • +

      The file has not been updated in the last 24 hours.

      +
    • +
    • +

      The remote copy has a newer modification time than the local copy.

      +
    • +
    +
    +
    +

    You can inherit from both remote and local files in the same config and the +same inheritance rules apply to remote URLs and inheriting from local +files where the first file in the list has the lowest precedence and the +last one has the highest. The format for multiple inheritance using URLs is:

    +
    +
    +
    +
    inherit_from:
    +  - http://www.example.com/rubocop.yml
    +  - ../.rubocop.yml
    +
    +
    +
    +

    You can inherit from a repo with basic auth that is authorized to access the repo as follow:

    +
    +
    +
    +
    inherit_from:
    +  - http://<user_name>:<password>@raw.github.com/example/rubocop.yml
    +
    +
    +
    +

    A GitHub personal access token +can also be configured as follow:

    +
    +
    +
    +
    inherit_from:
    +  - http://<personal_access_token>@raw.github.com/example/rubocop.yml
    +
    +
    +
    +

    Inheriting configuration from a dependency gem

    +
    +

    The optional inherit_gem directive is used to include configuration from +one or more gems external to the current project. This makes it possible to +inherit a shared dependency’s RuboCop configuration that can be used from +multiple disparate projects.

    +
    +
    +

    Configurations inherited in this way will be essentially prepended to the +inherit_from directive, such that the inherit_gem configurations will be +loaded first, then the inherit_from relative file paths will be loaded +(overriding the configurations from the gems), and finally the remaining +directives in the configuration file will supersede any of the inherited +configurations. This means the configurations inherited from one or more gems +have the lowest precedence of inheritance.

    +
    +
    +

    The directive should be formatted as a YAML Hash using the gem name as the +key and the relative path within the gem as the value:

    +
    +
    +
    +
    inherit_gem:
    +  my-shared-gem: .rubocop.yml
    +  cucumber: conf/rubocop.yml
    +
    +
    +
    +

    An array can also be used as the value to include multiple configuration files +from a single gem:

    +
    +
    +
    +
    inherit_gem:
    +  my-shared-gem:
    +    - default.yml
    +    - strict.yml
    +
    +
    +
    + + + + + +
    + + +If the shared dependency is declared using a Bundler +Gemfile and the gem was installed using bundle install, it would be +necessary to also invoke RuboCop using Bundler in order to find the +dependency’s installation path at runtime: +
    +
    +
    +
    +
    $ bundle exec rubocop <options...>
    +
    +
    +
    +
    +

    Merging arrays using inherit_mode

    +
    +

    The optional directive inherit_mode specifies which configuration keys that +have array values should be merged together instead of overriding the inherited +value.

    +
    +
    +

    This applies to explicit inheritance using inherit_from as well as implicit +inheritance from the default configuration.

    +
    +
    +

    Given the following config:

    +
    +
    +
    +
    # .rubocop.yml
    +inherit_from:
    +  - shared.yml
    +
    +inherit_mode:
    +  merge:
    +    - Exclude
    +
    +AllCops:
    +  Exclude:
    +    - 'generated/**/*.rb'
    +
    +Style/For:
    +  Exclude:
    +    - bar.rb
    +
    +
    +
    +
    +
    # .shared.yml
    +Style/For:
    +  Exclude:
    +    - foo.rb
    +
    +
    +
    +

    The list of Excludes for the Style/For cop in this example will be +['foo.rb', 'bar.rb']. Similarly, the AllCops:Exclude list will contain all +the default patterns plus the generated/**/*.rb entry that was added locally.

    +
    +
    +

    The directive can also be used on individual cop configurations to override +the global setting.

    +
    +
    +
    +
    inherit_from:
    +  - shared.yml
    +
    +inherit_mode:
    +  merge:
    +    - Exclude
    +
    +Style/For:
    +  inherit_mode:
    +    override:
    +      - Exclude
    +  Exclude:
    +    - bar.rb
    +
    +
    +
    +

    In this example the Exclude would only include bar.rb.

    +
    +
    +
    +
    +
    +

    Pre-processing

    +
    +
    +

    Configuration files are pre-processed using the ERB templating mechanism. This +makes it possible to add dynamic content that will be evaluated when the +configuration file is read. For example, you could let RuboCop ignore all files +ignored by Git.

    +
    +
    +
    +
    AllCops:
    +  Exclude:
    +  <% `git status --ignored --porcelain`.lines.grep(/^!! /).each do |path| %>
    +    - <%= path.sub(/^!! /, '').sub(/\/$/, '/**/*') %>
    +  <% end %>
    +
    +
    +
    +
    +
    +

    Defaults

    +
    +
    +

    The file config/default.yml under the RuboCop home directory contains the +default settings that all configurations inherit from. Project and personal +.rubocop.yml files need only make settings that are different from the +default ones. If there is no .rubocop.yml file in the project, home or XDG +directories, config/default.yml will be used.

    +
    +
    +
    +
    +

    Including/Excluding files

    +
    +
    +

    RuboCop does a recursive file search starting from the directory it is +run in, or directories given as command line arguments. Files that +match any pattern listed under AllCops/Include and extensionless +files with a hash-bang (#!) declaration containing one of the known +ruby interpreters listed under AllCops/RubyInterpreters are +inspected, unless the file also matches a pattern in +AllCops/Exclude. Hidden directories (i.e., directories whose names +start with a dot) are not searched by default.

    +
    +
    +

    Here is an example that might be used for a Rails project:

    +
    +
    +
    +
    AllCops:
    +  Exclude:
    +    - 'db/**/*'
    +    - 'config/**/*'
    +    - 'script/**/*'
    +    - 'bin/{rails,rake}'
    +    - !ruby/regexp /old_and_unused\.rb$/
    +
    +# other configuration
    +# ...
    +
    +
    +
    + + + + + +
    + + +When inspecting a certain directory(or file) +given as RuboCop’s command line arguments, +patterns listed under AllCops / Exclude are also inspected. +If you want to apply AllCops / Exclude rules in this circumstance, +add --force-exclusion to the command line argument. +
    +
    +
    +

    Here is an example:

    +
    +
    +
    +
    # .rubocop.yml
    +AllCops:
    +  Exclude:
    +    - foo.rb
    +
    +
    +
    +

    If foo.rb specified as a RuboCop’s command line argument, the result is:

    +
    +
    +
    +
    # RuboCop inspects foo.rb.
    +$ bundle exec rubocop foo.rb
    +
    +# RuboCop does not inspect foo.rb.
    +$ bundle exec rubocop --force-exclusion foo.rb
    +
    +
    +
    +

    Path relativity

    +
    +

    In .rubocop.yml and any other configuration file beginning with .rubocop, +files, and directories are specified relative to the directory where the +configuration file is. In configuration files that don’t begin with .rubocop, +e.g. our_company_defaults.yml, paths are relative to the directory where +rubocop is run.

    +
    +
    +

    This affects cops that have customisable paths: if the default is db/migrate/*.rb, +and the cop is enabled in db/migrate/.rubocop.yml, the path will need to be +explicitly set as *.rb, as the default will look for db/migrate/db/migrate/*.rb. +This is unlikely to be what you wanted.

    +
    +
    +
    +

    Unusual files, that would not be included by default

    +
    +

    RuboCop comes with a comprehensive list of common ruby file names and +extensions. But, if you’d like RuboCop to check files that are not included by +default, you’ll need to pass them in on the command line, or to add entries for +them under AllCops/Include. Remember that your configuration files override +RuboCops’s defaults. In the following example, we want to include +foo.unusual_extension, but we also must copy any other patterns we need from +the overridden default.yml.

    +
    +
    +
    +
    AllCops:
    +  Include:
    +    - foo.unusual_extension
    +    - '**/*.rb'
    +    - '**/*.gemfile'
    +    - '**/*.gemspec'
    +    - '**/*.rake'
    +    - '**/*.ru'
    +    - '**/Gemfile'
    +    - '**/Rakefile'
    +
    +
    +
    +

    This behavior of Include (overriding default.yml) was introduced in +0.56.0 +via #5882. This change allows +people to include/exclude precisely what they need to, without the defaults +getting in the way.

    +
    +
    +

    Another example, using inherit_mode

    +
    +
    +
    inherit_mode:
    +  merge:
    +    - Include
    +
    +AllCops:
    +  Include:
    +    - foo.unusual_extension
    +
    +
    +
    +

    See "Merging arrays using inherit_mode" above.

    +
    +
    +
    +
    +

    Deprecated patterns

    +
    +

    Patterns that are just a file name, e.g. Rakefile, will match +that file name in any directory, but this pattern style is deprecated. The +correct way to match the file in any directory, including the current, is +**/Rakefile.

    +
    +
    +

    The pattern config/** will match any file recursively under +config, but this pattern style is deprecated and should be replaced by +config/**/*.

    +
    +
    +

    Include and Exclude are relative to their directory

    +
    +

    The Include and Exclude parameters are special. They are +valid for the directory tree starting where they are defined. They are not +shadowed by the setting of Include and Exclude in other .rubocop.yml +files in subdirectories. This is different from all other parameters, who +follow RuboCop’s general principle that configuration for an inspected file +is taken from the nearest .rubocop.yml, searching upwards.

    +
    +
    + + + + + +
    + + +This behavior +will be overridden if you specify the --ignore-parent-exclusion command line +argument. +
    +
    +
    +
    +
    +

    Cop-specific Include and Exclude

    +
    +

    Cops can be run only on specific sets of files when that’s needed (for +instance you might want to run some Rails model checks only on files whose +paths match app/models/*.rb). All cops support the +Include param.

    +
    +
    +
    +
    Rails/HasAndBelongsToMany:
    +  Include:
    +    - app/models/*.rb
    +
    +
    +
    +

    Cops can also exclude only specific sets of files when that’s needed (for +instance you might want to run some cop only on a specific file). All cops support the +Exclude param.

    +
    +
    +
    +
    Rails/HasAndBelongsToMany:
    +  Exclude:
    +    - app/models/problematic.rb
    +
    +
    +
    +
    +
    +
    +

    Generic configuration parameters

    +
    +
    +

    In addition to Include and Exclude, the following parameters are available +for every cop.

    +
    +
    +

    Enabled

    +
    +

    Specific cops can be disabled by setting Enabled to false for that specific cop.

    +
    +
    +
    +
    Layout/LineLength:
    +  Enabled: false
    +
    +
    +
    +

    Most cops are enabled by default. Cops, introduced or significantly updated +between major versions, are in a special pending status (read more in +"Versioning"). Some cops, configured the above Enabled: false +in config/default.yml, +are disabled by default.

    +
    +
    +

    The cop enabling process can be altered by setting DisabledByDefault or +EnabledByDefault (but not both) to true. These settings override the default for all +cops to disabled or enabled, except Lint/Syntax which is always enabled, +regardless of the cops' default values (whether enabled, disabled or pending).

    +
    +
    +
    +
    AllCops:
    +  DisabledByDefault: true
    +
    +
    +
    +

    All cops except Lint/Syntax are then disabled by default. Only cops appearing in user +configuration files with Enabled: true will be enabled; every other cop will +be disabled without having to explicitly disable them in configuration. It is +also possible to enable entire departments by adding for example

    +
    +
    +
    +
    Style:
    +  Enabled: true
    +
    +
    +
    +

    All cops in the Style department are then enabled. In this case, only the cops +in the Style department that are enabled by default will be enabled. +The cops in the Style department that are disabled by default will remain disabled.

    +
    +
    +

    If a department is disabled, cops in that department can still be individually +enabled, and that setting overrides the setting for its department in the same +configuration file and in any inherited file.

    +
    +
    +
    +
    inherit_from: config_that_disables_the_metrics_department.yml
    +
    +Metrics/MethodLength:
    +  Enabled: true
    +
    +Style:
    +  Enabled: false
    +
    +Style/Alias:
    +  Enabled: true
    +
    +
    +
    +
    +

    Severity

    +
    +

    Each cop has a default severity level based on which department it belongs +to. The level is normally warning for Lint and convention for all the +others, but this can be changed in user configuration. Cops can customize their +severity level. Allowed values are info, refactor, convention, warning, error +and fatal.

    +
    +
    +

    Cops with severity info will be reported but will not cause rubocop to return +a non-zero value.

    +
    +
    +

    There is one exception from the general rule above and that is Lint/Syntax, a +special cop that checks for syntax errors before the other cops are invoked. It +cannot be disabled and its severity (fatal) cannot be changed in +configuration.

    +
    +
    +
    +
    Lint:
    +  Severity: error
    +
    +Metrics/CyclomaticComplexity:
    +  Severity: warning
    +
    +
    +
    +
    +

    Details

    +
    +

    Individual cops can be embellished with extra details in offense messages:

    +
    +
    +
    +
    Layout/LineLength:
    +  Details: >-
    +    If lines are too short, text becomes hard to read because you must
    +    constantly jump from one line to the next while reading. If lines are too
    +    long, the line jumping becomes too hard because you "lose the line" while
    +    going back to the start of the next line. 80 characters is a good
    +    compromise.
    +
    +
    +
    +

    These details will only be seen when RuboCop is run with the --extra-details flag or if ExtraDetails is set to true in your global RuboCop configuration.

    +
    +
    +
    +

    AutoCorrect

    +
    +

    Cops that support the --autocorrect option offer flexible settings for autocorrection. +These settings can be specified in the configuration file as follows:

    +
    +
    +
      +
    • +

      always

      +
    • +
    • +

      contextual

      +
    • +
    • +

      disabled

      +
    • +
    +
    +
    +

    always (Default)

    +
    +

    This setting enables autocorrection always by default. For backward compatibility, true is treated the same as always.

    +
    +
    +
    +
    Style/PerlBackrefs:
    +  AutoCorrect: always # or true
    +
    +
    +
    +
    +

    contextual

    +
    +

    This setting enables autocorrection when launched from the rubocop command, but it is not available through LSP. +e.g., rubocop --lsp, rubocop --editor-mode, or a program where RuboCop::LSP.enable has been applied.

    +
    +
    +

    Inspections via the command line are treated as code that has been finalized.

    +
    +
    +
    +
    Style/PerlBackrefs:
    +  AutoCorrect: contextual
    +
    +
    +
    +

    This setting prevents autocorrection during editing in the editor. e.g, with textDocument/formatting LSP method. +However workspace/executeCommand LSP method, which is triggered by intentional user actions, respects the user’s intention for autocorrection.

    +
    +
    +

    Additionally, for cases like Metrics cops where the highlight range extends over the entire body of classes, modules, methods, or blocks +offending range will be confined to only name. This approach helps to avoid redundant and noisy offenses in editor display.

    +
    +
    +
    +

    disabled

    +
    +

    This setting disables autocorrection. For backward compatibility, false is treated the same as disabled.

    +
    +
    +
    +
    Style/PerlBackrefs:
    +  AutoCorrect: disabled # or false
    +
    +
    +
    +
    +
    +
    +
    +

    Common configuration parameters

    +
    +
    +

    There are some configuration parameters that are shared by many cops, with the same behavior.

    +
    +
    +

    IgnoredMethods

    +
    +

    Cops that evaluate methods can often be configured to ignore certain methods. Both strings and +regular expressions can be used. For example:

    +
    +
    +
    +
    Metrics/BlockLength:
    +  IgnoredMethods:
    +    - refine
    +    - !ruby/regexp /\b(class|instance)_methods\b/
    +
    +
    +
    +
    +
    +
    +

    Setting the target Ruby version

    +
    +
    +

    Some checks are dependent on the version of the Ruby interpreter which the +inspected code must run on. For example, enforcing using Ruby 2.6+ endless +ranges foo[n..] rather than foo[n..-1] can help make your code shorter and +more consistent…​ unless it must run on e.g. Ruby 2.5.

    +
    +
    +

    Users may let RuboCop know the oldest version of Ruby which your project +supports with:

    +
    +
    +
    +
    AllCops:
    +  TargetRubyVersion: 2.5
    +
    +
    +
    + + + + + +
    + + +When ParserEngine: parser_prism is specified, TargetRubyVersion must +be set to 3.3 or higher. +
    +
    +
    +

    If a TargetRubyVersion is not specified in your config, then RuboCop will +check your project for a series of other files where the Ruby version may be +specified already. The files that will be checked are (in this order): +*.gemspec, .ruby-version, .tool-versions, and Gemfile.lock.

    +
    +
    +

    If a target Ruby version cannot be found via any of the above sources, then a +default target Ruby version will be used.

    +
    +
    +

    Finding target Ruby in a *.gemspec file

    +
    +

    In order for RuboCop to parse a *.gemspec file’s required_ruby_version, the +Ruby version must be specified using one of these syntaxes:

    +
    +
    +
      +
    1. +

      a string range, e.g. '~> 3.2.0' or '>= 3.2.2'

      +
    2. +
    3. +

      an array of strings, e.g. ['>= 3.0.0', '< 3.4.0']

      +
    4. +
    5. +

      a Gem::Requirement, e.g. Gem::Requirement.new('>= 3.1.2')

      +
    6. +
    +
    +
    +

    If a *.gemspec file specifies a range of supported Ruby versions via any of +these means, then the greater of the following Ruby versions will be used:

    +
    +
    +
      +
    • +

      the lowest Ruby version that is compatible with your specified range

      +
    • +
    • +

      the lowest version of Ruby that is still supported by your version of RuboCop

      +
    • +
    +
    +
    +

    If a *.gemspec file defines its required_ruby_version dynamically (e.g. by +reading from a .ruby-version file, via an environment variable, referencing a +constant or local variable, etc), then RuboCop will not detect that Ruby +version, and will instead try to find a target Ruby version elsewhere.

    +
    +
    +
    +
    +
    +

    Setting the parser engine

    +
    +
    + + + + + +
    + + +The parser engine configuration was introduced in RuboCop 1.62. This +experimental feature has been under consideration for a while. +
    +
    +
    +

    RuboCop allows switching the backend parser by specifying either +parser_whitequark or parser_prism as the value for the ParserEngine.

    +
    +
    +

    Here are the parsers used as backends for each value:

    +
    +
    + +
    +
    +

    By default, parser_whitequark is implicitly used.

    +
    +
    +

    parser_whitequark can analyze source code from Ruby 2.0 and above:

    +
    +
    +
    +
    AllCops:
    +  ParserEngine: parser_whitequark
    +
    +
    +
    +

    parser_prism can analyze source code from Ruby 3.3 and above:

    +
    +
    +
    +
    AllCops:
    +  ParserEngine: parser_prism
    +  TargetRubyVersion: 3.3
    +
    +
    +
    +

    parser_prism tends to perform analysis faster than parser_whitequark.

    +
    +
    + + + + + +
    + + +Since the support for Prism is experimental, it is not included in +RuboCop’s runtime dependencies. If running RuboCop through Bundler, please add +gem 'prism' to your Gemfile: +
    +
    +
    +
    +
    gem 'prism'
    +
    +
    +
    +

    There are some incompatibilities with parser_whitequark in parser_prism, +which is the main reason why the support for it is considered experimental. +We’re working with Prism’s team to address those. You can track the outstanding +known issues in Prism that affect RuboCop +here.

    +
    +
    +
    +
    +

    Automatically Generated Configuration

    +
    +
    +

    If you have a code base with an overwhelming amount of offenses, it can +be a good idea to use rubocop --auto-gen-config, which creates +.rubocop_todo.yml and adds inherit_from: .rubocop_todo.yml in your +.rubocop.yml. The generated file .rubocop_todo.yml contains +configuration to disable cops that currently detect an offense in the +code by changing the configuration for the cop, excluding the offending +files, or disabling the cop altogether once a file count limit has been +reached.

    +
    +
    +

    By adding the option --exclude-limit COUNT, e.g., rubocop +--auto-gen-config --exclude-limit 5, you can change how many files are +excluded before the cop is entirely disabled. The default COUNT is 15. +If you don’t want the cop to be entirely disabled regardless of the +number of files, use the --no-exclude-limit option, e.g., +rubocop --auto-gen-config --no-exclude-limit.

    +
    +
    +

    The next step is to cut and paste configuration from .rubocop_todo.yml +into .rubocop.yml for everything that you think is in line with your +(organization’s) code style and not a good fit for a todo list. Pay +attention to the comments above each entry. They can reveal configuration +parameters such as EnforcedStyle, which can be used to modify the +behavior of a cop instead of disabling it completely.

    +
    +
    +

    Then you can start removing the entries in the generated +.rubocop_todo.yml file one by one as you work through all the offenses +in the code. You can also regenerate your .rubocop_todo.yml using +the same options by running rubocop --regenerate-todo.

    +
    +
    +

    Another way of silencing offense reports, aside from configuration, is +through source code comments. These can be added manually or +automatically. See "Disabling Cops within Source Code" below.

    +
    +
    +

    The cops in the Metrics department will by default get Max parameters +generated in .rubocop_todo.yml. The value of these will be just high enough +so that no offenses are reported the next time you run rubocop. If you +prefer to exclude files, like for other cops, add --auto-gen-only-exclude +when running with --auto-gen-config. It will still change the maximum if the +number of excluded files is higher than the exclude limit.

    +
    +
    +

    Some cops have a configurable option named EnforcedStyle. +By default, when generating the .rubocop_todo.yml, if one style is used +for all files, these cops will add the settings for the style being used. +If you want to excluded on a file-by-file basis, +add the --no-auto-gen-enforced-style option along with --auto-gen-config.

    +
    +
    +
    +
    +

    Updating the configuration file

    +
    +
    +

    When you update RuboCop version, sometimes you need to change .rubocop.yml. +If you use mry, you can update .rubocop.yml +to latest version automatically.

    +
    +
    +
    +
    $ gem install mry
    +# Update to latest version
    +$ mry .rubocop.yml
    +# Update to specified version
    +$ mry --target=0.48.0 .rubocop.yml
    +
    +
    +
    +

    See https://github.com/pocke/mry for more information.

    +
    +
    +
    +
    +

    Disabling Cops within Source Code

    +
    +
    +

    One or more individual cops can be disabled locally in a section of a +file by adding a comment such as

    +
    +
    +
    +
    # rubocop:disable Layout/LineLength, Style/StringLiterals
    +[...]
    +# rubocop:enable Layout/LineLength, Style/StringLiterals
    +
    +
    +
    +

    You can also disable entire departments by giving a department name in the comment.

    +
    +
    +
    +
    # rubocop:disable Metrics, Layout/LineLength
    +[...]
    +# rubocop:enable Metrics, Layout/LineLength
    +
    +
    +
    +

    You can also disable all cops with

    +
    +
    +
    +
    # rubocop:disable all
    +[...]
    +# rubocop:enable all
    +
    +
    +
    +

    In cases where you want to differentiate intentionally-disabled cops vs. cops +you’d like to revisit later, you can use rubocop:todo as an alias of +rubocop:disable.

    +
    +
    +
    +
    # rubocop:todo Layout/LineLength, Style/StringLiterals
    +[...]
    +# rubocop:enable Layout/LineLength, Style/StringLiterals
    +
    +
    +
    +

    One or more cops can be disabled on a single line with an end-of-line +comment.

    +
    +
    +
    +
    for x in (0..19) # rubocop:disable Style/For
    +
    +
    +
    +

    If you want to disable a cop that inspects comments, you can do so by +adding an "inner comment" on the comment line.

    +
    +
    +
    +
    # coding: utf-8 # rubocop:disable Style/Encoding
    +
    +
    +
    +

    Running rubocop --autocorrect --disable-uncorrectable will +create comments to disable all offenses that can’t be automatically +corrected.

    +
    +
    +

    Do not write anything other than cop name in the disabling comment. E.g.:

    +
    +
    +
    +
    # rubocop:disable Layout/LineLength --This is a bad comment that includes other than cop name.
    +
    +
    +
    +
    +
    +

    Temporarily enabling cops in source code

    +
    +
    +

    In a similar way to disabling cops within source code, you can also temporarily enable specific +cops if you want to enforce specific rules for part of the totality of a file.

    +
    +
    +

    Let’s use the cop Style/AsciiComments, which is by default Enabled: false. If you want a +specific file to have ASCII-only comments to be compatible with some specific post-processing.

    +
    +
    +
    +
    # rubocop:enable Style/AsciiComments
    +# If applicable, leave a comment to others explaining the rationale:
    +# We need the comments to remain ASCII only for compatibility with lib/post_processor.rb
    +
    +class Restaurant
    +  # This comment has to be ASCII-only because of the rubocop:enable directive
    +  def menu
    +    return dishes.map(&:humanize)
    +  end
    +end
    +
    +
    +
    +

    You can also enforce the same for part of a file by disabling the cop afterwards

    +
    +
    +
    +
    class Dish
    +  def humanize
    +    return [
    +      "Delicious #{self.name}"
    +      *ingredients
    +    ].join("\n")
    +  end
    +end
    +
    +# rubocop:enable Style/AsciiComments
    +# If applicable, leave a comment to others explaining the rationale:
    +# We need the comments to remain ASCII only for compatibility with lib/post_processor.rb
    +
    +class Restaurant
    +  # This comment has to be ASCII-only because of the rubocop:enable directive
    +  def menu
    +    return dishes.map(&:humanize)
    +  end
    +end
    +
    +# rubocop:disable Style/AsciiComments
    +
    +class Ingredient
    +  # Notice how the comment below is non-ASCII
    +  # Gets rid of odd characters like 😀, ͸
    +  def sanitize
    +    self.name.gsub(/[^a-z]/, '')
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Setting the style guide URL

    +
    +
    +

    You can specify the base URL of the style guide using StyleGuideBaseURL. +If specified under AllCops, all cops are targeted.

    +
    +
    +
    +
    AllCops:
    +  StyleGuideBaseURL: https://rubystyle.guide
    +
    +
    +
    +

    StyleGuideBaseURL is combined with StyleGuide specified to the cop.

    +
    +
    +
    +
    Lint/UselessAssignment:
    +  StyleGuide: '#underscore-unused-vars'
    +
    +
    + +
    +

    If specified under a specific department, it takes precedence over AllCops. +The following is an example of specifying Rails department.

    +
    +
    +
    +
    Rails:
    +  StyleGuideBaseURL: https://rails.rubystyle.guide
    +
    +
    +
    +
    +
    Rails/TimeZone:
    +  StyleGuide: '#time'
    +
    +
    +
    +

    The style guide URL is https://rails.rubystyle.guide#time.

    +
    +
    +
    +
    +

    Setting the documentation URL

    +
    +
    +

    You can specify the base URL of the documentation using DocumentationBaseURL. +If specified under AllCops, all cops are targeted.

    +
    +
    +
    +
    AllCops:
    +  DocumentationBaseURL: https://docs.rubocop.org/rubocop
    +
    +
    +
    +

    If specified under a specific department, it takes precedence over AllCops. +The following is an example of specifying Rails department.

    +
    +
    +
    +
    Rails:
    +  DocumentationBaseURL: https://docs.rubocop.org/rubocop-rails
    +
    +
    +
    +

    By default, documentation is expected to be served as HTML but if you prefer +to use something else like markdown you can set DocumentationExtension.

    +
    +
    +

    With markdown as the documentation format you are able to host it directly through +GitHub without having to own a domain or using GitHub Pages. The rubocop-sorbet +extension is an example of this, its docs are available +here.

    +
    +
    +
    +
    Sorbet:
    +  DocumentationBaseURL: https://github.com/Shopify/rubocop-sorbet/blob/main/manual
    +  DocumentationExtension: .md
    +
    +
    +
    +
    +
    +

    Enable checking Active Support extensions

    +
    +
    +

    Some cops for checking specified methods (e.g. Style/HashExcept) supports Active Support extensions. +This is off by default, but can be enabled by the ActiveSupportExtensionsEnabled option.

    +
    +
    +
    +
    AllCops:
    +  ActiveSupportExtensionsEnabled: true
    +
    +
    +
    +
    +
    +

    Opting into globally frozen string literals

    +
    +
    +

    Ruby continues to move into the direction of having all string literals frozen by default. +Ruby 3.4 for example will show a warning if a non-frozen string literal from a file without +the frozen string literal magic comment gets modified. By starting ruby with the environment +variable RUBYOPT set to --enable=frozen-string-literal you can opt into that behaviour today. +For RuboCop to provide accurate analysis you must also configure the StringLiteralsFrozenByDefault +option.

    +
    +
    +
    +
    AllCops:
    +  StringLiteralsFrozenByDefault: true
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/contributing.html b/docs/rubocop/1.69/contributing.html new file mode 100644 index 000000000..afebfb66c --- /dev/null +++ b/docs/rubocop/1.69/contributing.html @@ -0,0 +1,826 @@ + + + + + + Contributing :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Contributing

    +
    +

    Issues

    +
    +
    +

    Report issues and suggest features and improvements on the +GitHub issue tracker.

    +
    +
    +

    If you want to file an issue (bug report or feature request), please provide all +the necessary info listed in our issue reporting template (it’s loaded +automatically when you create a new GitHub issue).

    +
    +
    + + + + + +
    + + +Please, don’t ask support-like questions on the issue tracker (e.g. "How +can I configure RuboCop to do X?" - use the support channels +instead. +
    +
    +
    +
    +
    +

    Patches

    +
    +
    +

    Patches in any form are always welcome! GitHub pull requests are even better! :-)

    +
    +
    +

    Before submitting a patch or a pull request make sure all tests are +passing and that your patch is in line with the contribution +guidelines.

    +
    +
    +

    A handy way to test only the files that you have modified in the last commit +(with rspec and rubocop) is to use rake check_commit.

    +
    +
    +

    Also see the Development section.

    +
    +
    +
    +
    +

    Documentation

    +
    +
    +

    Good documentation is just as important as good code. +Please, help us improve RuboCop’s documentation.

    +
    +
    +

    You should also check out the +cop documentation section of the docs and consider +adding or improving Cop descriptions.

    +
    +
    +

    Working on the Docs

    +
    +

    The manual is generated from the AsciiDoc files in the docs folder of RuboCop’s GitHub repo and is published to https://docs.rubocop.org. +Antora is used to convert the manual into HTML. +The filesystem layout is described at https://docs.antora.org/antora/2.0/component-structure/.

    +
    +
    +

    To make changes to the manual you simply have to change the files under doc. +The manual will be regenerated manually periodically.

    +
    +
    +

    Installing Antora

    +
    + + + + + +
    + + +The instructions here assume you already have (the right version of) node.js installed. +
    +
    +
    +

    Installing the Antora is super simple:

    +
    +
    +
    +
    $ npm i -g @antora/cli@2.0 @antora/site-generator-default@2.0
    +
    +
    +
    +

    Check out the detailed installation instructions +if you run into any problems.

    +
    +
    +
    +

    Building the Site

    +
    +

    You can build the documentation locally from the docs.rubocop.org repo.

    +
    +
    +
    +
    $ cd docs.rubocop.org
    +$ antora --pull antora-playbook.yml
    +
    +
    +
    +

    After you’re done with the initial setup you can use the deploy script to push changes to the site:

    +
    +
    +
    +
    ./deploy
    +
    +
    +
    + + + + + +
    + + +You’ll need commit access to the repository for this to work. +
    +
    +
    +

    To check the generated site you can simply open docs/index.html in your favourite browser.

    +
    +
    +

    If you want to make changes to the manual’s page structure you’ll have to edit +nav.adoc.

    +
    +
    +
    +
    +
    +
    +

    Funding

    +
    +
    +

    While RuboCop is free software and will always be, the project would benefit immensely from some funding. +Raising a monthly budget of a couple of thousand dollars would make it possible to pay people to work on +certain complex features, fund other development related stuff (e.g. hardware, conference trips) and so on. +Raising a monthly budget of over $5000 would open the possibility of someone working full-time on the project +which would speed up the pace of development significantly.

    +
    +
    +

    We welcome both individual and corporate sponsors! We also offer a wide array of funding channels to account +for your preferences (although currently Open Collective is our preferred funding platform).

    +
    +
    +

    If you’re working in a company that’s making significant use of RuboCop we’d appreciate it if you suggest to your company +to become a RuboCop sponsor.

    +
    +
    +

    You can support the development of RuboCop via +GitHub Sponsors, +Patreon, +PayPal, +and Open Collective.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops.html b/docs/rubocop/1.69/cops.html new file mode 100644 index 000000000..5dca0e60c --- /dev/null +++ b/docs/rubocop/1.69/cops.html @@ -0,0 +1,2540 @@ + + + + + + Cops :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Cops

    +
    +
    +
    +

    In RuboCop lingo the various checks performed on the code are called cops. +Each cop is responsible for detecting one particular offense. There are several +cop departments, grouping the cops by class of offense. A short description of +the different departments is provided below.

    +
    +
    +

    Many of the Style and Layout cops have configuration options, allowing them to +enforce different coding conventions.

    +
    +
    +

    You can also load custom cops.

    +
    +
    +

    Cop-related failures are silenced by default but can be turned on using the +--raise-cop-errors option.

    +
    +
    +
    +
    +

    Style

    +
    +
    +

    Style cops check for stylistic consistency of your code. Many of the them are +based on the Ruby Style Guide.

    +
    +
    +
    +
    +

    Layout

    +
    +
    +

    Layout cops inspect your code for consistent use of indentation, alignment, +and white space.

    +
    +
    +
    +
    +

    Lint

    +
    +
    +

    Lint cops check for ambiguities and possible errors in your code.

    +
    +
    +

    RuboCop implements, in a portable way, all built-in MRI lint checks +(ruby -wc) and adds a lot of extra lint checks of its own.

    +
    +
    +

    You can run only the Lint cops like this:

    +
    +
    +
    +
    $ rubocop -l
    +
    +
    +
    +

    The -l/--lint option can be used together with --only to run all the +enabled Lint cops plus a selection of other cops.

    +
    +
    +

    Disabling Lint cops is generally a bad idea.

    +
    +
    +
    +
    +

    Metrics

    +
    +
    +

    Metrics cops deal with properties of the source code that can be measured, +such as class length, method length, etc. Generally speaking, they have a +configuration parameter called Max and when running +rubocop --auto-gen-config, this parameter will be set to the highest value +found for the inspected code.

    +
    +
    +
    +
    +

    Naming

    +
    +
    +

    Naming cops check for naming issue of your code, such as method name, constant +name, file name, etc.

    +
    +
    +
    +
    +

    Security

    +
    +
    +

    Security cops checks for method calls and constructs which are known to be +associated with potential security issues.

    +
    +
    +
    +
    +

    Bundler

    +
    +
    +

    Bundler cops check for style and bad practices in Bundler files, e.g. Gemfile.

    +
    +
    +
    +
    +

    Gemspec

    +
    +
    +

    Gemspec cops check for style and bad practices in gemspec files, e.g. rubocop.gemspec.

    +
    +
    +
    +
    +

    Available cops

    +
    +
    +

    In the following section you find all available cops:

    +
    + + +
    +

    Department Layout

    +
    + +
    +
    +
    +

    Department Lint

    +
    + +
    +
    + +
    +

    Department Migration

    + +
    + + +
    +

    Department Style

    +
    + +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_bundler.html b/docs/rubocop/1.69/cops_bundler.html new file mode 100644 index 000000000..7171d44db --- /dev/null +++ b/docs/rubocop/1.69/cops_bundler.html @@ -0,0 +1,1520 @@ + + + + + + Bundler :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Bundler

    +
    +

    Bundler/DuplicatedGem

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.46

    1.40

    +
    +

    A Gem’s requirements should be listed only once in a Gemfile.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +gem 'rubocop'
    +gem 'rubocop'
    +
    +# bad
    +group :development do
    +  gem 'rubocop'
    +end
    +
    +group :test do
    +  gem 'rubocop'
    +end
    +
    +# good
    +group :development, :test do
    +  gem 'rubocop'
    +end
    +
    +# good
    +gem 'rubocop', groups: [:development, :test]
    +
    +# good - conditional declaration
    +if Dir.exist?(local)
    +  gem 'rubocop', path: local
    +elsif ENV['RUBOCOP_VERSION'] == 'master'
    +  gem 'rubocop', git: 'https://github.com/rubocop/rubocop.git'
    +else
    +  gem 'rubocop', '~> 0.90.0'
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    +
    +
    +
    +
    +

    Bundler/DuplicatedGroup

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    1.56

    -

    +
    +

    A Gem group, or a set of groups, should be listed only once in a Gemfile.

    +
    +
    +

    For example, if the values of source, git, platforms, or path +surrounding group are different, no offense will be registered:

    +
    +
    +
    +
    platforms :ruby do
    +  group :default do
    +    gem 'openssl'
    +  end
    +end
    +
    +platforms :jruby do
    +  group :default do
    +    gem 'jruby-openssl'
    +  end
    +end
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +group :development do
    +  gem 'rubocop'
    +end
    +
    +group :development do
    +  gem 'rubocop-rails'
    +end
    +
    +# bad (same set of groups declared twice)
    +group :development, :test do
    +  gem 'rubocop'
    +end
    +
    +group :test, :development do
    +  gem 'rspec'
    +end
    +
    +# good
    +group :development do
    +  gem 'rubocop'
    +end
    +
    +group :development, :test do
    +  gem 'rspec'
    +end
    +
    +# good
    +gem 'rubocop', groups: [:development, :test]
    +gem 'rspec', groups: [:development, :test]
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    +
    +
    +
    +
    +

    Bundler/GemComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.59

    0.85

    +
    +

    Each gem in the Gemfile should have a comment explaining +its purpose in the project, or the reason for its version +or source.

    +
    +
    +

    The optional "OnlyFor" configuration array +can be used to only register offenses when the gems +use certain options or have version specifiers.

    +
    +
    +

    When "version_specifiers" is included, a comment +will be enforced if the gem has any version specifier.

    +
    +
    +

    When "restrictive_version_specifiers" is included, a comment +will be enforced if the gem has a version specifier that +holds back the version of the gem.

    +
    +
    +

    For any other value in the array, a comment will be enforced for +a gem if an option by the same name is present. +A useful use case is to enforce a comment when using +options that change the source of a gem:

    +
    +
    +
      +
    • +

      bitbucket

      +
    • +
    • +

      gist

      +
    • +
    • +

      git

      +
    • +
    • +

      github

      +
    • +
    • +

      source

      +
    • +
    +
    +
    +

    For a full list of options supported by bundler, +see https://bundler.io/man/gemfile.5.html +.

    +
    +
    +

    Examples

    +
    +

    OnlyFor: [] (default)

    +
    +
    +
    # bad
    +
    +gem 'foo'
    +
    +# good
    +
    +# Helpers for the foo things.
    +gem 'foo'
    +
    +
    +
    +
    +

    OnlyFor: ['version_specifiers']

    +
    +
    +
    # bad
    +
    +gem 'foo', '< 2.1'
    +
    +# good
    +
    +# Version 2.1 introduces breaking change baz
    +gem 'foo', '< 2.1'
    +
    +
    +
    +
    +

    OnlyFor: ['restrictive_version_specifiers']

    +
    +
    +
    # bad
    +
    +gem 'foo', '< 2.1'
    +
    +# good
    +
    +gem 'foo', '>= 1.0'
    +
    +# Version 2.1 introduces breaking change baz
    +gem 'foo', '< 2.1'
    +
    +
    +
    +
    +

    OnlyFor: ['version_specifiers', 'github']

    +
    +
    +
    # bad
    +
    +gem 'foo', github: 'some_account/some_fork_of_foo'
    +
    +gem 'bar', '< 2.1'
    +
    +# good
    +
    +# Using this fork because baz
    +gem 'foo', github: 'some_account/some_fork_of_foo'
    +
    +# Version 2.1 introduces breaking change baz
    +gem 'bar', '< 2.1'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    IgnoredGems

    []

    Array

    OnlyFor

    []

    Array

    +
    +
    +
    +
    +

    Bundler/GemFilename

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    1.20

    -

    +
    +

    Verifies that a project contains Gemfile or gems.rb file and correct +associated lock file based on the configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: Gemfile (default)

    +
    +
    +
    # bad
    +Project contains gems.rb and gems.locked files
    +
    +# bad
    +Project contains Gemfile and gems.locked file
    +
    +# good
    +Project contains Gemfile and Gemfile.lock
    +
    +
    +
    +
    +

    EnforcedStyle: gems.rb

    +
    +
    +
    # bad
    +Project contains Gemfile and Gemfile.lock files
    +
    +# bad
    +Project contains gems.rb and Gemfile.lock file
    +
    +# good
    +Project contains gems.rb and gems.locked files
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    Gemfile

    Gemfile, gems.rb

    Include

    **/Gemfile, **/gems.rb, **/Gemfile.lock, **/gems.locked

    Array

    +
    +
    +
    +
    +

    Bundler/GemVersion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    1.14

    -

    +
    +

    Enforce that Gem version specifications or a commit reference (branch, +ref, or tag) are either required or forbidden.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: required (default)

    +
    +
    +
    # bad
    +gem 'rubocop'
    +
    +# good
    +gem 'rubocop', '~> 1.12'
    +
    +# good
    +gem 'rubocop', '>= 1.10.0'
    +
    +# good
    +gem 'rubocop', '>= 1.5.0', '< 1.10.0'
    +
    +# good
    +gem 'rubocop', branch: 'feature-branch'
    +
    +# good
    +gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
    +
    +# good
    +gem 'rubocop', tag: 'v1.17.0'
    +
    +
    +
    +
    +

    EnforcedStyle: forbidden

    +
    +
    +
    # good
    +gem 'rubocop'
    +
    +# bad
    +gem 'rubocop', '~> 1.12'
    +
    +# bad
    +gem 'rubocop', '>= 1.10.0'
    +
    +# bad
    +gem 'rubocop', '>= 1.5.0', '< 1.10.0'
    +
    +# bad
    +gem 'rubocop', branch: 'feature-branch'
    +
    +# bad
    +gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
    +
    +# bad
    +gem 'rubocop', tag: 'v1.17.0'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    required

    required, forbidden

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    AllowedGems

    []

    Array

    +
    +
    +
    +
    +

    Bundler/InsecureProtocolSource

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    1.40

    +
    +

    Passing symbol arguments to source (e.g. source :rubygems) is +deprecated because they default to using HTTP requests. Instead, specify +'https://rubygems.org' if possible, or 'http://rubygems.org' if not.

    +
    +
    +

    When autocorrecting, this cop will replace symbol arguments with +'https://rubygems.org'.

    +
    +
    +

    This cop will not replace existing sources that use http://. This may +be necessary where HTTPS is not available. For example, where using an +internal gem server via an intranet, or where HTTPS is prohibited. +However, you should strongly prefer https:// where possible, as it is +more secure.

    +
    +
    +

    If you don’t allow http://, please set false to AllowHttpProtocol. +This option is true by default for safe autocorrection.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +source :gemcutter
    +source :rubygems
    +source :rubyforge
    +
    +# good
    +source 'https://rubygems.org' # strongly recommended
    +
    +
    +
    +

    AllowHttpProtocol: true (default)

    +
    +
    +
    # good
    +source 'http://rubygems.org' # use only if HTTPS is unavailable
    +
    +
    +
    +
    +

    AllowHttpProtocol: false

    +
    +
    +
    # bad
    +source 'http://rubygems.org'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    AllowHttpProtocol

    true

    Boolean

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    +
    +
    +
    +
    +

    Bundler/OrderedGems

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.46

    0.47

    +
    +

    Gems should be alphabetically sorted within groups.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +gem 'rubocop'
    +gem 'rspec'
    +
    +# good
    +gem 'rspec'
    +gem 'rubocop'
    +
    +# good
    +gem 'rubocop'
    +
    +gem 'rspec'
    +
    +
    +
    +

    TreatCommentsAsGroupSeparators: true (default)

    +
    +
    +
    # good
    +# For code quality
    +gem 'rubocop'
    +# For tests
    +gem 'rspec'
    +
    +
    +
    +
    +

    TreatCommentsAsGroupSeparators: false

    +
    +
    +
    # bad
    +# For code quality
    +gem 'rubocop'
    +# For tests
    +gem 'rspec'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    TreatCommentsAsGroupSeparators

    true

    Boolean

    ConsiderPunctuation

    false

    Boolean

    Include

    **/*.gemfile, **/Gemfile, **/gems.rb

    Array

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_gemspec.html b/docs/rubocop/1.69/cops_gemspec.html new file mode 100644 index 000000000..1c871ff00 --- /dev/null +++ b/docs/rubocop/1.69/cops_gemspec.html @@ -0,0 +1,1712 @@ + + + + + + Gemspec :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Gemspec

    +
    +

    Gemspec/AddRuntimeDependency

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.65

    -

    +
    +

    Prefer add_dependency over add_runtime_dependency as the latter is +considered soft-deprecated.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  spec.add_runtime_dependency('rubocop')
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_dependency('rubocop')
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Include

    **/*.gemspec

    Array

    +
    + +
    +
    +
    +

    Gemspec/DependencyVersion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    1.29

    -

    +
    +

    Enforce that gem dependency version specifications or a commit reference (branch, +ref, or tag) are either required or forbidden.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: required (default)

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  spec.add_dependency 'parser'
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.add_development_dependency 'parser'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_dependency 'parser', '>= 2.3.3.1', '< 3.0'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_development_dependency 'parser', '>= 2.3.3.1', '< 3.0'
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: forbidden

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  spec.add_dependency 'parser', '>= 2.3.3.1', '< 3.0'
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.add_development_dependency 'parser', '>= 2.3.3.1', '< 3.0'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_dependency 'parser'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_development_dependency 'parser'
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    required

    required, forbidden

    Include

    **/*.gemspec

    Array

    AllowedGems

    []

    Array

    +
    +
    +
    +
    +

    Gemspec/DeprecatedAttributeAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.30

    1.40

    +
    +

    Checks that deprecated attributes are not set in a gemspec file. +Removing deprecated attributes allows the user to receive smaller packed gems.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  spec.name = 'your_cool_gem_name'
    +  spec.test_files = Dir.glob('test/**/*')
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.name = 'your_cool_gem_name'
    +  spec.test_files += Dir.glob('test/**/*')
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.name = 'your_cool_gem_name'
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Gemspec/DevelopmentDependencies

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.44

    -

    +
    +

    Enforce that development dependencies for a gem are specified in +Gemfile, rather than in the gemspec using +add_development_dependency. Alternatively, using EnforcedStyle: +gemspec, enforce that all dependencies are specified in gemspec, +rather than in Gemfile.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: Gemfile (default)

    +
    +
    +
    # Specify runtime dependencies in your gemspec,
    +# but all other dependencies in your Gemfile.
    +
    +# bad
    +# example.gemspec
    +s.add_development_dependency "foo"
    +
    +# good
    +# Gemfile
    +gem "foo"
    +
    +# good
    +# gems.rb
    +gem "foo"
    +
    +# good (with AllowedGems: ["bar"])
    +# example.gemspec
    +s.add_development_dependency "bar"
    +
    +
    +
    +
    +

    EnforcedStyle: gems.rb

    +
    +
    +
    # Specify runtime dependencies in your gemspec,
    +# but all other dependencies in your Gemfile.
    +#
    +# Identical to `EnforcedStyle: Gemfile`, but with a different error message.
    +# Rely on Bundler/GemFilename to enforce the use of `Gemfile` vs `gems.rb`.
    +
    +# bad
    +# example.gemspec
    +s.add_development_dependency "foo"
    +
    +# good
    +# Gemfile
    +gem "foo"
    +
    +# good
    +# gems.rb
    +gem "foo"
    +
    +# good (with AllowedGems: ["bar"])
    +# example.gemspec
    +s.add_development_dependency "bar"
    +
    +
    +
    +
    +

    EnforcedStyle: gemspec

    +
    +
    +
    # Specify all dependencies in your gemspec.
    +
    +# bad
    +# Gemfile
    +gem "foo"
    +
    +# good
    +# example.gemspec
    +s.add_development_dependency "foo"
    +
    +# good (with AllowedGems: ["bar"])
    +# Gemfile
    +gem "bar"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    Gemfile

    Gemfile, gems.rb, gemspec

    AllowedGems

    []

    Array

    Include

    **/*.gemspec, **/Gemfile, **/gems.rb

    Array

    +
    +
    +
    +
    +

    Gemspec/DuplicatedAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.52

    1.40

    +
    +

    An attribute assignment method calls should be listed only once +in a gemspec.

    +
    +
    +

    Assigning to an attribute with the same name using spec.foo = will be +an unintended usage. On the other hand, duplication of methods such +as spec.requirements, spec.add_runtime_dependency, and others are +permitted because it is the intended use of appending values.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  spec.name = 'rubocop'
    +  spec.name = 'rubocop2'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.name = 'rubocop'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.requirements << 'libmagick, v6.0'
    +  spec.requirements << 'A good graphics card'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_dependency('parallel', '~> 1.10')
    +  spec.add_dependency('parser', '>= 2.3.3.1', '< 3.0')
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Gemspec/OrderedDependencies

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.51

    -

    +
    +

    Dependencies in the gemspec should be alphabetically sorted.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +spec.add_dependency 'rubocop'
    +spec.add_dependency 'rspec'
    +
    +# good
    +spec.add_dependency 'rspec'
    +spec.add_dependency 'rubocop'
    +
    +# good
    +spec.add_dependency 'rubocop'
    +
    +spec.add_dependency 'rspec'
    +
    +# bad
    +spec.add_development_dependency 'rubocop'
    +spec.add_development_dependency 'rspec'
    +
    +# good
    +spec.add_development_dependency 'rspec'
    +spec.add_development_dependency 'rubocop'
    +
    +# good
    +spec.add_development_dependency 'rubocop'
    +
    +spec.add_development_dependency 'rspec'
    +
    +# bad
    +spec.add_runtime_dependency 'rubocop'
    +spec.add_runtime_dependency 'rspec'
    +
    +# good
    +spec.add_runtime_dependency 'rspec'
    +spec.add_runtime_dependency 'rubocop'
    +
    +# good
    +spec.add_runtime_dependency 'rubocop'
    +
    +spec.add_runtime_dependency 'rspec'
    +
    +
    +
    +

    TreatCommentsAsGroupSeparators: true (default)

    +
    +
    +
    # good
    +# For code quality
    +spec.add_dependency 'rubocop'
    +# For tests
    +spec.add_dependency 'rspec'
    +
    +
    +
    +
    +

    TreatCommentsAsGroupSeparators: false

    +
    +
    +
    # bad
    +# For code quality
    +spec.add_dependency 'rubocop'
    +# For tests
    +spec.add_dependency 'rspec'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    TreatCommentsAsGroupSeparators

    true

    Boolean

    ConsiderPunctuation

    false

    Boolean

    Include

    **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Gemspec/RequireMFA

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.23

    1.40

    +
    +

    Requires a gemspec to have rubygems_mfa_required metadata set.

    +
    +
    +

    This setting tells RubyGems that MFA (Multi-Factor Authentication) is +required for accounts to be able perform privileged operations, such as +(see RubyGems' documentation for the full list of privileged +operations):

    +
    +
    +
      +
    • +

      gem push

      +
    • +
    • +

      gem yank

      +
    • +
    • +

      gem owner --add/remove

      +
    • +
    • +

      adding or removing owners using gem ownership page

      +
    • +
    +
    +
    +

    This helps make your gem more secure, as users can be more +confident that gem updates were pushed by maintainers.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  # no `rubygems_mfa_required` metadata specified
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.metadata = {
    +    'rubygems_mfa_required' => 'true'
    +  }
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.metadata['rubygems_mfa_required'] = 'true'
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.metadata = {
    +    'rubygems_mfa_required' => 'false'
    +  }
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.metadata = {
    +    'rubygems_mfa_required' => 'true'
    +  }
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.metadata['rubygems_mfa_required'] = 'false'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.metadata['rubygems_mfa_required'] = 'true'
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemspec

    Array

    +
    + +
    +
    +
    +

    Gemspec/RequiredRubyVersion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.52

    1.40

    +
    +

    Checks that required_ruby_version in a gemspec file is set to a valid +value (non-blank) and matches TargetRubyVersion as set in RuboCop’s +configuration for the gem.

    +
    +
    +

    This ensures that RuboCop is using the same Ruby version as the gem.

    +
    +
    +

    Examples

    +
    +
    +
    # When `TargetRubyVersion` of .rubocop.yml is `2.5`.
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  # no `required_ruby_version` specified
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = '>= 2.4.0'
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = '>= 2.6.0'
    +end
    +
    +# bad
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = ''
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = '>= 2.5.0'
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = '>= 2.5'
    +end
    +
    +# accepted but not recommended
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = ['>= 2.5.0', '< 2.7.0']
    +end
    +
    +# accepted but not recommended, since
    +# Ruby does not really follow semantic versioning
    +Gem::Specification.new do |spec|
    +  spec.required_ruby_version = '~> 2.5'
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Gemspec/RubyVersionGlobalsUsage

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.72

    1.40

    +
    +

    Checks that RUBY_VERSION constant is not used in gemspec. +Using RUBY_VERSION is dangerous because value of the +constant is determined by rake release. +It’s possible to have dependency based on ruby version used +to execute rake release and not user’s ruby version.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Gem::Specification.new do |spec|
    +  if RUBY_VERSION >= '3.0'
    +    spec.add_dependency 'gem_a'
    +  else
    +    spec.add_dependency 'gem_b'
    +  end
    +end
    +
    +# good
    +Gem::Specification.new do |spec|
    +  spec.add_dependency 'gem_a'
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Severity

    warning

    String

    Include

    **/*.gemspec

    Array

    +
    + +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_layout.html b/docs/rubocop/1.69/cops_layout.html new file mode 100644 index 000000000..04d1b93f5 --- /dev/null +++ b/docs/rubocop/1.69/cops_layout.html @@ -0,0 +1,12182 @@ + + + + + + Layout :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Layout

    +
    +

    Layout/AccessModifierIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Bare access modifiers (those not applying to specific methods) should be +indented as deep as method definitions, or as deep as the class/module +keyword, depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: indent (default)

    +
    +
    +
    # bad
    +class Plumbus
    +private
    +  def smooth; end
    +end
    +
    +# good
    +class Plumbus
    +  private
    +  def smooth; end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: outdent

    +
    +
    +
    # bad
    +class Plumbus
    +  private
    +  def smooth; end
    +end
    +
    +# good
    +class Plumbus
    +private
    +  def smooth; end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    indent

    outdent, indent

    IndentationWidth

    <none>

    Integer

    +
    + +
    +
    +
    +

    Layout/ArgumentAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.68

    0.77

    +
    +

    Check that the arguments on a multi-line method definition are aligned.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: with_first_argument (default)

    +
    +
    +
    # good
    +
    +foo :bar,
    +    :baz,
    +    key: value
    +
    +foo(
    +  :bar,
    +  :baz,
    +  key: value
    +)
    +
    +# bad
    +
    +foo :bar,
    +  :baz,
    +  key: value
    +
    +foo(
    +  :bar,
    +    :baz,
    +    key: value
    +)
    +
    +
    +
    +
    +

    EnforcedStyle: with_fixed_indentation

    +
    +
    +
    # good
    +
    +foo :bar,
    +  :baz,
    +  key: value
    +
    +# bad
    +
    +foo :bar,
    +    :baz,
    +    key: value
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    with_first_argument

    with_first_argument, with_fixed_indentation

    IndentationWidth

    <none>

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/ArrayAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.77

    +
    +

    Check that the elements of a multi-line array literal are +aligned.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: with_first_element (default)

    +
    +
    +
    # good
    +
    +array = [1, 2, 3,
    +         4, 5, 6]
    +array = ['run',
    +         'forrest',
    +         'run']
    +
    +# bad
    +
    +array = [1, 2, 3,
    +  4, 5, 6]
    +array = ['run',
    +     'forrest',
    +     'run']
    +
    +
    +
    +
    +

    EnforcedStyle: with_fixed_indentation

    +
    +
    +
    # good
    +
    +array = [1, 2, 3,
    +  4, 5, 6]
    +
    +# bad
    +
    +array = [1, 2, 3,
    +         4, 5, 6]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    with_first_element

    with_first_element, with_fixed_indentation

    IndentationWidth

    <none>

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/AssignmentIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.45

    +
    +

    Checks the indentation of the first line of the +right-hand-side of a multi-line assignment.

    +
    +
    +

    The indentation of the remaining lines can be corrected with +other cops such as Layout/IndentationConsistency and Layout/EndAlignment.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +value =
    +if foo
    +  'bar'
    +end
    +
    +# good
    +value =
    +  if foo
    +    'bar'
    +  end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/BeginEndAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.91

    -

    +
    +

    Checks whether the end keyword of begin is aligned properly.

    +
    +
    +

    Two modes are supported through the EnforcedStyleAlignWith configuration +parameter. If it’s set to start_of_line (which is the default), the +end shall be aligned with the start of the line where the begin +keyword is. If it’s set to begin, the end shall be aligned with the +begin keyword.

    +
    +
    +

    Layout/EndAlignment cop aligns with keywords (e.g. if, while, case) +by default. On the other hand, ||= begin that this cop targets tends to +align with the start of the line, it defaults to EnforcedStyleAlignWith: start_of_line. +These style can be configured by each cop.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyleAlignWith: start_of_line (default)

    +
    +
    +
    # bad
    +foo ||= begin
    +          do_something
    +        end
    +
    +# good
    +foo ||= begin
    +  do_something
    +end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: begin

    +
    +
    +
    # bad
    +foo ||= begin
    +  do_something
    +end
    +
    +# good
    +foo ||= begin
    +          do_something
    +        end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleAlignWith

    start_of_line

    start_of_line, begin

    Severity

    warning

    String

    +
    +
    +
    +
    +

    Layout/BlockAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks whether the end keywords are aligned properly for do +end blocks.

    +
    +
    +

    Three modes are supported through the EnforcedStyleAlignWith +configuration parameter:

    +
    +
    +

    start_of_block : the end shall be aligned with the +start of the line where the do appeared.

    +
    +
    +

    start_of_line : the end shall be aligned with the +start of the line where the expression started.

    +
    +
    +

    either (which is the default) : the end is allowed to be in either +location. The autocorrect will default to start_of_line.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyleAlignWith: either (default)

    +
    +
    +
    # bad
    +
    +foo.bar
    +  .each do
    +    baz
    +      end
    +
    +# good
    +
    +foo.bar
    +  .each do
    +    baz
    +end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: start_of_block

    +
    +
    +
    # bad
    +
    +foo.bar
    +  .each do
    +    baz
    +      end
    +
    +# good
    +
    +foo.bar
    +  .each do
    +    baz
    +  end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: start_of_line

    +
    +
    +
    # bad
    +
    +foo.bar
    +  .each do
    +    baz
    +      end
    +
    +# good
    +
    +foo.bar
    +  .each do
    +    baz
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleAlignWith

    either

    either, start_of_block, start_of_line

    +
    +
    +
    +
    +

    Layout/BlockEndNewline

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks whether the end statement of a do..end block +is on its own line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +blah do |i|
    +  foo(i) end
    +
    +# good
    +blah do |i|
    +  foo(i)
    +end
    +
    +# bad
    +blah { |i|
    +  foo(i) }
    +
    +# good
    +blah { |i|
    +  foo(i)
    +}
    +
    +
    +
    +
    +
    +
    +

    Layout/CaseIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.16

    +
    +

    Checks how the when and ins of a case expression +are indented in relation to its case or end keyword.

    +
    +
    +

    It will register a separate offense for each misaligned when and in.

    +
    +
    +

    Examples

    +
    +
    +
    # If Layout/EndAlignment is set to keyword style (default)
    +# *case* and *end* should always be aligned to same depth,
    +# and therefore *when* should always be aligned to both -
    +# regardless of configuration.
    +
    +# bad for all styles
    +case n
    +  when 0
    +    x * 2
    +  else
    +    y / 3
    +end
    +
    +case n
    +  in pattern
    +    x * 2
    +  else
    +    y / 3
    +end
    +
    +# good for all styles
    +case n
    +when 0
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +case n
    +in pattern
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +
    +
    +

    EnforcedStyle: case (default)

    +
    +
    +
    # if EndAlignment is set to other style such as
    +# start_of_line (as shown below), then *when* alignment
    +# configuration does have an effect.
    +
    +# bad
    +a = case n
    +when 0
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +a = case n
    +in pattern
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +# good
    +a = case n
    +    when 0
    +      x * 2
    +    else
    +      y / 3
    +end
    +
    +a = case n
    +    in pattern
    +      x * 2
    +    else
    +      y / 3
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: end

    +
    +
    +
    # bad
    +a = case n
    +    when 0
    +      x * 2
    +    else
    +      y / 3
    +end
    +
    +a = case n
    +    in pattern
    +      x * 2
    +    else
    +      y / 3
    +end
    +
    +# good
    +a = case n
    +when 0
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +a = case n
    +in pattern
    +  x * 2
    +else
    +  y / 3
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    case

    case, end

    IndentOneStep

    false

    Boolean

    IndentationWidth

    <none>

    Integer

    +
    + +
    +
    +
    +

    Layout/ClassStructure

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always (Unsafe)

    0.52

    1.53

    +
    +

    Checks if the code style follows the ExpectedOrder configuration:

    +
    +
    +

    Categories allows us to map macro names into a category.

    +
    +
    +

    Consider an example of code style that covers the following order:

    +
    +
    +
      +
    • +

      Module inclusion (include, prepend, extend)

      +
    • +
    • +

      Constants

      +
    • +
    • +

      Associations (has_one, has_many)

      +
    • +
    • +

      Public attribute macros (attr_accessor, attr_writer, attr_reader)

      +
    • +
    • +

      Other macros (validates, validate)

      +
    • +
    • +

      Public class methods

      +
    • +
    • +

      Initializer

      +
    • +
    • +

      Public instance methods

      +
    • +
    • +

      Protected attribute macros (attr_accessor, attr_writer, attr_reader)

      +
    • +
    • +

      Protected instance methods

      +
    • +
    • +

      Private attribute macros (attr_accessor, attr_writer, attr_reader)

      +
    • +
    • +

      Private instance methods

      +
    • +
    +
    +
    +

    You can configure the following order:

    +
    +
    +
    +
     Layout/ClassStructure:
    +   ExpectedOrder:
    +     - module_inclusion
    +     - constants
    +     - association
    +     - public_attribute_macros
    +     - public_delegate
    +     - macros
    +     - public_class_methods
    +     - initializer
    +     - public_methods
    +     - protected_attribute_macros
    +     - protected_methods
    +     - private_attribute_macros
    +     - private_delegate
    +     - private_methods
    +
    +
    +
    +

    Instead of putting all literals in the expected order, is also +possible to group categories of macros. Visibility levels are handled +automatically.

    +
    +
    +
    +
     Layout/ClassStructure:
    +   Categories:
    +     association:
    +       - has_many
    +       - has_one
    +     attribute_macros:
    +       - attr_accessor
    +       - attr_reader
    +       - attr_writer
    +     macros:
    +       - validates
    +       - validate
    +     module_inclusion:
    +       - include
    +       - prepend
    +       - extend
    +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because class methods and module inclusion +can behave differently, based on which methods or constants have +already been defined.

    +
    +
    +

    Constants will only be moved when they are assigned with literals.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# Expect extend be before constant
    +class Person < ApplicationRecord
    +  has_many :orders
    +  ANSWER = 42
    +
    +  extend SomeModule
    +  include AnotherModule
    +end
    +
    +# good
    +class Person
    +  # extend and include go first
    +  extend SomeModule
    +  include AnotherModule
    +
    +  # inner classes
    +  CustomError = Class.new(StandardError)
    +
    +  # constants are next
    +  SOME_CONSTANT = 20
    +
    +  # afterwards we have public attribute macros
    +  attr_reader :name
    +
    +  # followed by other macros (if any)
    +  validates :name
    +
    +  # then we have public delegate macros
    +  delegate :to_s, to: :name
    +
    +  # public class methods are next in line
    +  def self.some_method
    +  end
    +
    +  # initialization goes between class methods and instance methods
    +  def initialize
    +  end
    +
    +  # followed by other public instance methods
    +  def some_method
    +  end
    +
    +  # protected attribute macros and methods go next
    +  protected
    +
    +  attr_reader :protected_name
    +
    +  def some_protected_method
    +  end
    +
    +  # private attribute macros, delegate macros and methods
    +  # are grouped near the end
    +  private
    +
    +  attr_reader :private_name
    +
    +  delegate :some_private_delegate, to: :name
    +
    +  def some_private_method
    +  end
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Categories

    {"module_inclusion"⇒["include", "prepend", "extend"]}

    ExpectedOrder

    module_inclusion, constants, public_class_methods, initializer, public_methods, protected_methods, private_methods

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/ClosingHeredocIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.57

    -

    +
    +

    Checks the indentation of here document closings.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  def bar
    +    <<~SQL
    +      'Hi'
    +  SQL
    +  end
    +end
    +
    +# good
    +class Foo
    +  def bar
    +    <<~SQL
    +      'Hi'
    +    SQL
    +  end
    +end
    +
    +# bad
    +
    +# heredoc contents is before closing heredoc.
    +foo arg,
    +    <<~EOS
    +  Hi
    +    EOS
    +
    +# good
    +foo arg,
    +    <<~EOS
    +  Hi
    +EOS
    +
    +# good
    +foo arg,
    +    <<~EOS
    +      Hi
    +    EOS
    +
    +
    +
    +
    +
    +
    +

    Layout/ClosingParenthesisIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the indentation of hanging closing parentheses in +method calls, method definitions, and grouped expressions. A hanging +closing parenthesis means ) preceded by a line break.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_method(
    +  a,
    +  b
    +  )
    +
    +some_method(
    +  a, b
    +  )
    +
    +some_method(a, b, c
    +  )
    +
    +some_method(a,
    +            b,
    +            c
    +  )
    +
    +some_method(a,
    +  x: 1,
    +  y: 2
    +  )
    +
    +# Scenario 1: When First Parameter Is On Its Own Line
    +
    +# good: when first param is on a new line, right paren is *always*
    +#       outdented by IndentationWidth
    +some_method(
    +  a,
    +  b
    +)
    +
    +# good
    +some_method(
    +  a, b
    +)
    +
    +# Scenario 2: When First Parameter Is On The Same Line
    +
    +# good: when all other params are also on the same line, outdent
    +#       right paren by IndentationWidth
    +some_method(a, b, c
    +           )
    +
    +# good: when all other params are on multiple lines, but are lined
    +#       up, align right paren with left paren
    +some_method(a,
    +            b,
    +            c
    +           )
    +
    +# good: when other params are not lined up on multiple lines, outdent
    +#       right paren by IndentationWidth
    +some_method(a,
    +  x: 1,
    +  y: 2
    +)
    +
    +
    +
    +
    +
    +
    +

    Layout/CommentIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.24

    +
    +

    Checks the indentation of comments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +  # comment here
    +def method_name
    +end
    +
    +  # comment here
    +a = 'hello'
    +
    +# yet another comment
    +  if true
    +    true
    +  end
    +
    +# good
    +# comment here
    +def method_name
    +end
    +
    +# comment here
    +a = 'hello'
    +
    +# yet another comment
    +if true
    +  true
    +end
    +
    +
    +
    +

    AllowForAlignment: false (default)

    +
    +
    +
    # bad
    +a = 1 # A really long comment
    +      # spanning two lines.
    +
    +# good
    +# A really long comment spanning one line.
    +a = 1
    +
    +
    +
    +
    +

    AllowForAlignment: true

    +
    +
    +
    # good
    +a = 1 # A really long comment
    +      # spanning two lines.
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowForAlignment

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/ConditionPosition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    0.83

    +
    +

    Checks for conditions that are not on the same line as +if/while/until.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if
    +  some_condition
    +  do_something
    +end
    +
    +# good
    +if some_condition
    +  do_something
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/DefEndAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks whether the end keywords of method definitions are +aligned properly.

    +
    +
    +

    Two modes are supported through the EnforcedStyleAlignWith configuration +parameter. If it’s set to start_of_line (which is the default), the +end shall be aligned with the start of the line where the def +keyword is. If it’s set to def, the end shall be aligned with the +def keyword.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyleAlignWith: start_of_line (default)

    +
    +
    +
    # bad
    +
    +private def foo
    +            end
    +
    +# good
    +
    +private def foo
    +end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: def

    +
    +
    +
    # bad
    +
    +private def foo
    +            end
    +
    +# good
    +
    +private def foo
    +        end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleAlignWith

    start_of_line

    start_of_line, def

    Severity

    warning

    String

    +
    +
    +
    +
    +

    Layout/DotPosition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the . position in multi-line method calls.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: leading (default)

    +
    +
    +
    # bad
    +something.
    +  method
    +
    +# good
    +something
    +  .method
    +
    +
    +
    +
    +

    EnforcedStyle: trailing

    +
    +
    +
    # bad
    +something
    +  .method
    +
    +# good
    +something.
    +  method
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    leading

    leading, trailing

    +
    + +
    +
    +
    +

    Layout/ElseAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the alignment of else keywords. Normally they should +be aligned with an if/unless/while/until/begin/def/rescue keyword, but there +are special cases when they should follow the same rules as the +alignment of end.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if something
    +  code
    + else
    +  code
    +end
    +
    +# bad
    +if something
    +  code
    + elsif something
    +  code
    +end
    +
    +# good
    +if something
    +  code
    +else
    +  code
    +end
    +
    +
    +
    +
    +
    +
    +

    Layout/EmptyComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.53

    1.61

    +
    +

    Checks empty comment.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +#
    +class Foo
    +end
    +
    +# good
    +
    +#
    +# Description of `Foo` class.
    +#
    +class Foo
    +end
    +
    +
    +
    +

    AllowBorderComment: true (default)

    +
    +
    +
    # good
    +
    +def foo
    +end
    +
    +#################
    +
    +def bar
    +end
    +
    +
    +
    +
    +

    AllowBorderComment: false

    +
    +
    +
    # bad
    +
    +def foo
    +end
    +
    +#################
    +
    +def bar
    +end
    +
    +
    +
    +
    +

    AllowMarginComment: true (default)

    +
    +
    +
    # good
    +
    +#
    +# Description of `Foo` class.
    +#
    +class Foo
    +end
    +
    +
    +
    +
    +

    AllowMarginComment: false

    +
    +
    +
    # bad
    +
    +#
    +# Description of `Foo` class.
    +#
    +class Foo
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowBorderComment

    true

    Boolean

    AllowMarginComment

    true

    Boolean

    +
    +
    +
    +
    +

    Layout/EmptyLineAfterGuardClause

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.56

    0.59

    +
    +

    Enforces empty line after guard clause.

    +
    +
    +

    This cop allows # :nocov: directive after guard clause because +SimpleCov excludes code from the coverage report by wrapping it in # :nocov::

    +
    +
    +
    +
    def foo
    +  # :nocov:
    +  return if condition
    +  # :nocov:
    +  bar
    +end
    +
    +
    +
    +

    Refer to SimpleCov’s documentation for more details: +https://github.com/simplecov-ruby/simplecov#ignoringskipping-code

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo
    +  return if need_return?
    +  bar
    +end
    +
    +# good
    +def foo
    +  return if need_return?
    +
    +  bar
    +end
    +
    +# good
    +def foo
    +  return if something?
    +  return if something_different?
    +
    +  bar
    +end
    +
    +# also good
    +def foo
    +  if something?
    +    do_something
    +    return if need_return?
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Layout/EmptyLineAfterMagicComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for a newline after the final magic comment.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +# frozen_string_literal: true
    +
    +# Some documentation for Person
    +class Person
    +  # Some code
    +end
    +
    +# bad
    +# frozen_string_literal: true
    +# Some documentation for Person
    +class Person
    +  # Some code
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLineAfterMultilineCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.90

    -

    +
    +

    Enforces empty line after multiline condition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if multiline &&
    +  condition
    +  do_something
    +end
    +
    +# good
    +if multiline &&
    +  condition
    +
    +  do_something
    +end
    +
    +# bad
    +case x
    +when foo,
    +  bar
    +  do_something
    +end
    +
    +# good
    +case x
    +when foo,
    +  bar
    +
    +  do_something
    +end
    +
    +# bad
    +begin
    +  do_something
    +rescue FooError,
    +  BarError
    +  handle_error
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue FooError,
    +  BarError
    +
    +  handle_error
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLineBetweenDefs

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.23

    +
    +

    Checks whether class/module/method definitions are +separated by one or more empty lines.

    +
    +
    +

    NumberOfEmptyLines can be an integer (default is 1) or +an array (e.g. [1, 2]) to specify a minimum and maximum +number of empty lines permitted.

    +
    +
    +

    AllowAdjacentOneLineDefs configures whether adjacent +one-line definitions are considered an offense.

    +
    +
    +

    Examples

    +
    +

    EmptyLineBetweenMethodDefs: true (default)

    +
    +
    +
    # checks for empty lines between method definitions.
    +
    +# bad
    +def a
    +end
    +def b
    +end
    +
    +
    +
    +
    +
    # good
    +def a
    +end
    +
    +def b
    +end
    +
    +
    +
    +
    +

    EmptyLineBetweenClassDefs: true (default)

    +
    +
    +
    # checks for empty lines between class definitions.
    +
    +# bad
    +class A
    +end
    +class B
    +end
    +def b
    +end
    +
    +
    +
    +
    +
    # good
    +class A
    +end
    +
    +class B
    +end
    +
    +def b
    +end
    +
    +
    +
    +
    +

    EmptyLineBetweenModuleDefs: true (default)

    +
    +
    +
    # checks for empty lines between module definitions.
    +
    +# bad
    +module A
    +end
    +module B
    +end
    +def b
    +end
    +
    +
    +
    +
    +
    # good
    +module A
    +end
    +
    +module B
    +end
    +
    +def b
    +end
    +
    +
    +
    +
    +

    AllowAdjacentOneLineDefs: true (default)

    +
    +
    +
    # good
    +class ErrorA < BaseError; end
    +class ErrorB < BaseError; end
    +class ErrorC < BaseError; end
    +
    +# good
    +class ErrorA < BaseError; end
    +
    +class ErrorB < BaseError; end
    +
    +class ErrorC < BaseError; end
    +
    +
    +
    +
    +

    AllowAdjacentOneLineDefs: false

    +
    +
    +
    # bad
    +class ErrorA < BaseError; end
    +class ErrorB < BaseError; end
    +class ErrorC < BaseError; end
    +
    +# good
    +class ErrorA < BaseError; end
    +
    +class ErrorB < BaseError; end
    +
    +class ErrorC < BaseError; end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EmptyLineBetweenMethodDefs

    true

    Boolean

    EmptyLineBetweenClassDefs

    true

    Boolean

    EmptyLineBetweenModuleDefs

    true

    Boolean

    DefLikeMacros

    []

    Array

    AllowAdjacentOneLineDefs

    true

    Boolean

    NumberOfEmptyLines

    1

    Integer

    +
    + +
    +
    +
    +

    Layout/EmptyLines

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for two or more consecutive blank lines.

    +
    +
    +

    Examples

    +
    +
    +
    # bad - It has two empty lines.
    +some_method
    +# one empty line
    +# two empty lines
    +some_method
    +
    +# good
    +some_method
    +# one empty line
    +some_method
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundAccessModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Access modifiers should be surrounded by blank lines.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: around (default)

    +
    +
    +
    # bad
    +class Foo
    +  def bar; end
    +  private
    +  def baz; end
    +end
    +
    +# good
    +class Foo
    +  def bar; end
    +
    +  private
    +
    +  def baz; end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: only_before

    +
    +
    +
    # bad
    +class Foo
    +  def bar; end
    +  private
    +  def baz; end
    +end
    +
    +# good
    +class Foo
    +  def bar; end
    +
    +  private
    +  def baz; end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    around

    around, only_before

    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks if empty lines exist around the arguments +of a method invocation.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something(
    +  foo
    +
    +)
    +
    +process(bar,
    +
    +        baz: qux,
    +        thud: fred)
    +
    +some_method(
    +
    +  [1,2,3],
    +  x: y
    +)
    +
    +# good
    +do_something(
    +  foo
    +)
    +
    +process(bar,
    +        baz: qux,
    +        thud: fred)
    +
    +some_method(
    +  [1,2,3],
    +  x: y
    +)
    +
    +
    +
    +
    +
    +
    +

    Layout/EmptyLinesAroundAttributeAccessor

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.83

    0.84

    +
    +

    Checks for a newline after an attribute accessor or a group of them. +alias syntax and alias_method, public, protected, and private methods are allowed +by default. These are customizable with AllowAliasSyntax and AllowedMethods options.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +attr_accessor :foo
    +def do_something
    +end
    +
    +# good
    +attr_accessor :foo
    +
    +def do_something
    +end
    +
    +# good
    +attr_accessor :foo
    +attr_reader :bar
    +attr_writer :baz
    +attr :qux
    +
    +def do_something
    +end
    +
    +
    +
    +

    AllowAliasSyntax: true (default)

    +
    +
    +
    # good
    +attr_accessor :foo
    +alias :foo? :foo
    +
    +def do_something
    +end
    +
    +
    +
    +
    +

    AllowAliasSyntax: false

    +
    +
    +
    # bad
    +attr_accessor :foo
    +alias :foo? :foo
    +
    +def do_something
    +end
    +
    +# good
    +attr_accessor :foo
    +
    +alias :foo? :foo
    +
    +def do_something
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['private']

    +
    +
    +
    # good
    +attr_accessor :foo
    +private :foo
    +
    +def do_something
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowAliasSyntax

    true

    Boolean

    AllowedMethods

    alias_method, public, protected, private

    Array

    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundBeginBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks if empty lines exist around the bodies of begin-end +blocks.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +
    +begin
    +  # ...
    +end
    +
    +# bad
    +
    +begin
    +
    +  # ...
    +
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundBlockBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks if empty lines around the bodies of blocks match +the configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_empty_lines (default)

    +
    +
    +
    # good
    +
    +foo do |bar|
    +  # ...
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines

    +
    +
    +
    # good
    +
    +foo do |bar|
    +
    +  # ...
    +
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_empty_lines

    empty_lines, no_empty_lines

    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundClassBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.53

    +
    +

    Checks if empty lines around the bodies of classes match +the configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_empty_lines (default)

    +
    +
    +
    # good
    +
    +class Foo
    +  def bar
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines

    +
    +
    +
    # good
    +
    +class Foo
    +
    +  def bar
    +    # ...
    +  end
    +
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines_except_namespace

    +
    +
    +
    # good
    +
    +class Foo
    +  class Bar
    +
    +    # ...
    +
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines_special

    +
    +
    +
    # good
    +class Foo
    +
    +  def bar; end
    +
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: beginning_only

    +
    +
    +
    # good
    +
    +class Foo
    +
    +  def bar
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: ending_only

    +
    +
    +
    # good
    +
    +class Foo
    +  def bar
    +    # ...
    +  end
    +
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_empty_lines

    empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only

    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundExceptionHandlingKeywords

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks if empty lines exist around the bodies of begin +sections. This cop doesn’t check empty lines at begin body +beginning/end and around method definition body. +Style/EmptyLinesAroundBeginBody or Style/EmptyLinesAroundMethodBody +can be used for this purpose.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +
    +begin
    +  do_something
    +rescue
    +  do_something2
    +else
    +  do_something3
    +ensure
    +  do_something4
    +end
    +
    +# good
    +
    +def foo
    +  do_something
    +rescue
    +  do_something2
    +end
    +
    +# bad
    +
    +begin
    +  do_something
    +
    +rescue
    +
    +  do_something2
    +
    +else
    +
    +  do_something3
    +
    +ensure
    +
    +  do_something4
    +end
    +
    +# bad
    +
    +def foo
    +  do_something
    +
    +rescue
    +
    +  do_something2
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundMethodBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks if empty lines exist around the bodies of methods.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +
    +def foo
    +  # ...
    +end
    +
    +# bad
    +
    +def bar
    +
    +  # ...
    +
    +end
    +
    +
    +
    + +
    +
    +
    +

    Layout/EmptyLinesAroundModuleBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks if empty lines around the bodies of modules match +the configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_empty_lines (default)

    +
    +
    +
    # good
    +
    +module Foo
    +  def bar
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines

    +
    +
    +
    # good
    +
    +module Foo
    +
    +  def bar
    +    # ...
    +  end
    +
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines_except_namespace

    +
    +
    +
    # good
    +
    +module Foo
    +  module Bar
    +
    +    # ...
    +
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty_lines_special

    +
    +
    +
    # good
    +module Foo
    +
    +  def bar; end
    +
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_empty_lines

    empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines

    +
    + +
    +
    +
    +

    Layout/EndAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks whether the end keywords are aligned properly.

    +
    +
    +

    Three modes are supported through the EnforcedStyleAlignWith +configuration parameter:

    +
    +
    +

    If it’s set to keyword (which is the default), the end +shall be aligned with the start of the keyword (if, class, etc.).

    +
    +
    +

    If it’s set to variable the end shall be aligned with the +left-hand-side of the variable assignment, if there is one.

    +
    +
    +

    If it’s set to start_of_line, the end shall be aligned with the +start of the line where the matching keyword appears.

    +
    +
    +

    This Layout/EndAlignment cop aligns with keywords (e.g. if, while, case) +by default. On the other hand, Layout/BeginEndAlignment cop aligns with +EnforcedStyleAlignWith: start_of_line by default due to ||= begin tends +to align with the start of the line. Layout/DefEndAlignment cop also aligns with +EnforcedStyleAlignWith: start_of_line by default. +These style can be configured by each cop.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyleAlignWith: keyword (default)

    +
    +
    +
    # bad
    +
    +variable = if true
    +    end
    +
    +# good
    +
    +variable = if true
    +           end
    +
    +variable =
    +  if true
    +  end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: variable

    +
    +
    +
    # bad
    +
    +variable = if true
    +    end
    +
    +# good
    +
    +variable = if true
    +end
    +
    +variable =
    +  if true
    +  end
    +
    +
    +
    +
    +

    EnforcedStyleAlignWith: start_of_line

    +
    +
    +
    # bad
    +
    +variable = if true
    +    end
    +
    +puts(if true
    +     end)
    +
    +# good
    +
    +variable = if true
    +end
    +
    +puts(if true
    +end)
    +
    +variable =
    +  if true
    +  end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleAlignWith

    keyword

    keyword, variable, start_of_line

    Severity

    warning

    String

    +
    +
    +
    +
    +

    Layout/EndOfLine

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.49

    -

    +
    +

    Checks for Windows-style line endings in the source code.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: native (default)

    +
    +
    +
    # The `native` style means that CR+LF (Carriage Return + Line Feed) is
    +# enforced on Windows, and LF is enforced on other platforms.
    +
    +# bad
    +puts 'Hello' # Return character is LF on Windows.
    +puts 'Hello' # Return character is CR+LF on other than Windows.
    +
    +# good
    +puts 'Hello' # Return character is CR+LF on Windows.
    +puts 'Hello' # Return character is LF on other than Windows.
    +
    +
    +
    +
    +

    EnforcedStyle: lf

    +
    +
    +
    # The `lf` style means that LF (Line Feed) is enforced on
    +# all platforms.
    +
    +# bad
    +puts 'Hello' # Return character is CR+LF on all platforms.
    +
    +# good
    +puts 'Hello' # Return character is LF on all platforms.
    +
    +
    +
    +
    +

    EnforcedStyle: crlf

    +
    +
    +
    # The `crlf` style means that CR+LF (Carriage Return + Line Feed) is
    +# enforced on all platforms.
    +
    +# bad
    +puts 'Hello' # Return character is LF on all platforms.
    +
    +# good
    +puts 'Hello' # Return character is CR+LF on all platforms.
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    native

    native, lf, crlf

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/ExtraSpacing

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for extra/unnecessary whitespace.

    +
    +
    +

    Examples

    +
    +
    +
    # good if AllowForAlignment is true
    +name      = "RuboCop"
    +# Some comment and an empty line
    +
    +website  += "/rubocop/rubocop" unless cond
    +puts        "rubocop"          if     debug
    +
    +# bad for any configuration
    +set_app("RuboCop")
    +website  = "https://github.com/rubocop/rubocop"
    +
    +# good only if AllowBeforeTrailingComments is true
    +object.method(arg)  # this is a comment
    +
    +# good even if AllowBeforeTrailingComments is false or not set
    +object.method(arg) # this is a comment
    +
    +# good with either AllowBeforeTrailingComments or AllowForAlignment
    +object.method(arg)         # this is a comment
    +another_object.method(arg) # this is another comment
    +some_object.method(arg)    # this is some comment
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowForAlignment

    true

    Boolean

    AllowBeforeTrailingComments

    false

    Boolean

    ForceEqualSignAlignment

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/FirstArgumentIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.68

    0.77

    +
    +

    Checks the indentation of the first argument in a method call. +Arguments after the first one are checked by Layout/ArgumentAlignment, +not by this cop.

    +
    +
    +

    For indenting the first parameter of method definitions, check out +Layout/FirstParameterIndentation.

    +
    +
    +

    This cop will respect Layout/ArgumentAlignment and will not work when +EnforcedStyle: with_fixed_indentation is specified for Layout/ArgumentAlignment.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_method(
    +first_param,
    +second_param)
    +
    +foo = some_method(
    +first_param,
    +second_param)
    +
    +foo = some_method(nested_call(
    +nested_first_param),
    +second_param)
    +
    +foo = some_method(
    +nested_call(
    +nested_first_param),
    +second_param)
    +
    +some_method nested_call(
    +nested_first_param),
    +second_param
    +
    +
    +
    +

    EnforcedStyle: special_for_inner_method_call_in_parentheses (default)

    +
    +
    +
    # Same as `special_for_inner_method_call` except that the special rule
    +# only applies if the outer method call encloses its arguments in
    +# parentheses.
    +
    +# good
    +some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(nested_call(
    +                    nested_first_param),
    +second_param)
    +
    +foo = some_method(
    +  nested_call(
    +    nested_first_param),
    +second_param)
    +
    +some_method nested_call(
    +  nested_first_param),
    +second_param
    +
    +
    +
    +
    +

    EnforcedStyle: consistent

    +
    +
    +
    # The first argument should always be indented one step more than the
    +# preceding line.
    +
    +# good
    +some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(nested_call(
    +  nested_first_param),
    +second_param)
    +
    +foo = some_method(
    +  nested_call(
    +    nested_first_param),
    +second_param)
    +
    +some_method nested_call(
    +  nested_first_param),
    +second_param
    +
    +
    +
    +
    +

    EnforcedStyle: consistent_relative_to_receiver

    +
    +
    +
    # The first argument should always be indented one level relative to
    +# the parent that is receiving the argument
    +
    +# good
    +some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(
    +        first_param,
    +second_param)
    +
    +foo = some_method(nested_call(
    +                    nested_first_param),
    +second_param)
    +
    +foo = some_method(
    +        nested_call(
    +          nested_first_param),
    +second_param)
    +
    +some_method nested_call(
    +              nested_first_param),
    +second_params
    +
    +
    +
    +
    +

    EnforcedStyle: special_for_inner_method_call

    +
    +
    +
    # The first argument should normally be indented one step more than
    +# the preceding line, but if it's an argument for a method call that
    +# is itself an argument in a method call, then the inner argument
    +# should be indented relative to the inner method.
    +
    +# good
    +some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(
    +  first_param,
    +second_param)
    +
    +foo = some_method(nested_call(
    +                    nested_first_param),
    +second_param)
    +
    +foo = some_method(
    +  nested_call(
    +    nested_first_param),
    +second_param)
    +
    +some_method nested_call(
    +              nested_first_param),
    +second_param
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    special_for_inner_method_call_in_parentheses

    consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/FirstArrayElementIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.68

    0.77

    +
    +

    Checks the indentation of the first element in an array literal +where the opening bracket and the first element are on separate lines. +The other elements' indentations are handled by Layout/ArrayAlignment cop.

    +
    +
    +

    This cop will respect Layout/ArrayAlignment and will not work when +EnforcedStyle: with_fixed_indentation is specified for Layout/ArrayAlignment.

    +
    +
    +

    By default, array literals that are arguments in a method call with +parentheses, and where the opening square bracket of the array is on the +same line as the opening parenthesis of the method call, shall have +their first element indented one step (two spaces) more than the +position inside the opening parenthesis.

    +
    +
    +

    Other array literals shall have their first element indented one step +more than the start of the line where the opening square bracket is.

    +
    +
    +

    This default style is called 'special_inside_parentheses'. Alternative +styles are 'consistent' and 'align_brackets'. Here are examples:

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: special_inside_parentheses (default)

    +
    +
    +
    # The `special_inside_parentheses` style enforces that the first
    +# element in an array literal where the opening bracket and first
    +# element are on separate lines is indented one step (two spaces) more
    +# than the position inside the opening parenthesis.
    +
    +# bad
    +array = [
    +  :value
    +]
    +and_in_a_method_call([
    +  :no_difference
    +                     ])
    +
    +# good
    +array = [
    +  :value
    +]
    +but_in_a_method_call([
    +                       :its_like_this
    +                     ])
    +
    +
    +
    +
    +

    EnforcedStyle: consistent

    +
    +
    +
    # The `consistent` style enforces that the first element in an array
    +# literal where the opening bracket and the first element are on
    +# separate lines is indented the same as an array literal which is not
    +# defined inside a method call.
    +
    +# bad
    +array = [
    +  :value
    +]
    +but_in_a_method_call([
    +                       :its_like_this
    +])
    +
    +# good
    +array = [
    +  :value
    +]
    +and_in_a_method_call([
    +  :no_difference
    +])
    +
    +
    +
    +
    +

    EnforcedStyle: align_brackets

    +
    +
    +
    # The `align_brackets` style enforces that the opening and closing
    +# brackets are indented to the same position.
    +
    +# bad
    +and_now_for_something = [
    +                          :completely_different
    +]
    +
    +# good
    +and_now_for_something = [
    +                          :completely_different
    +                        ]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    special_inside_parentheses

    special_inside_parentheses, consistent, align_brackets

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/FirstArrayElementLineBreak

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for a line break before the first element in a +multi-line array.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[ :a,
    +  :b]
    +
    +# good
    +[
    +  :a,
    +  :b]
    +
    +# good
    +[:a, :b]
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +[ :a, {
    +  :b => :c
    +}]
    +
    +# good
    +[
    +  :a, {
    +  :b => :c
    +}]
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +[:a, {
    +  :b => :c
    +}]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/FirstHashElementIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.68

    0.77

    +
    +

    Checks the indentation of the first key in a hash literal +where the opening brace and the first key are on separate lines. The +other keys' indentations are handled by the HashAlignment cop.

    +
    +
    +

    By default, Hash literals that are arguments in a method call with +parentheses, and where the opening curly brace of the hash is on the +same line as the opening parenthesis of the method call, shall have +their first key indented one step (two spaces) more than the position +inside the opening parenthesis.

    +
    +
    +

    Other hash literals shall have their first key indented one step more +than the start of the line where the opening curly brace is.

    +
    +
    +

    This default style is called 'special_inside_parentheses'. Alternative +styles are 'consistent' and 'align_braces'. Here are examples:

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: special_inside_parentheses (default)

    +
    +
    +
    # The `special_inside_parentheses` style enforces that the first key
    +# in a hash literal where the opening brace and the first key are on
    +# separate lines is indented one step (two spaces) more than the
    +# position inside the opening parentheses.
    +
    +# bad
    +hash = {
    +  key: :value
    +}
    +and_in_a_method_call({
    +  no: :difference
    +                     })
    +takes_multi_pairs_hash(x: {
    +  a: 1,
    +  b: 2
    +},
    +                       y: {
    +                         c: 1,
    +                         d: 2
    +                       })
    +
    +# good
    +special_inside_parentheses
    +hash = {
    +  key: :value
    +}
    +but_in_a_method_call({
    +                       its_like: :this
    +                     })
    +takes_multi_pairs_hash(x: {
    +                         a: 1,
    +                         b: 2
    +                       },
    +                       y: {
    +                         c: 1,
    +                         d: 2
    +                       })
    +
    +
    +
    +
    +

    EnforcedStyle: consistent

    +
    +
    +
    # The `consistent` style enforces that the first key in a hash
    +# literal where the opening brace and the first key are on
    +# separate lines is indented the same as a hash literal which is not
    +# defined inside a method call.
    +
    +# bad
    +hash = {
    +  key: :value
    +}
    +but_in_a_method_call({
    +                       its_like: :this
    +                      })
    +
    +# good
    +hash = {
    +  key: :value
    +}
    +and_in_a_method_call({
    +  no: :difference
    +})
    +
    +
    +
    +
    +

    EnforcedStyle: align_braces

    +
    +
    +
    # The `align_brackets` style enforces that the opening and closing
    +# braces are indented to the same position.
    +
    +# bad
    +and_now_for_something = {
    +                          completely: :different
    +}
    +takes_multi_pairs_hash(x: {
    +  a: 1,
    +  b: 2
    +},
    +                        y: {
    +                             c: 1,
    +                             d: 2
    +                           })
    +
    +# good
    +and_now_for_something = {
    +                          completely: :different
    +                        }
    +takes_multi_pairs_hash(x: {
    +                            a: 1,
    +                            b: 2
    +                          },
    +                       y: {
    +                            c: 1,
    +                            d: 2
    +                          })
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    special_inside_parentheses

    special_inside_parentheses, consistent, align_braces

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/FirstHashElementLineBreak

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for a line break before the first element in a +multi-line hash.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{ a: 1,
    +  b: 2}
    +
    +# good
    +{
    +  a: 1,
    +  b: 2 }
    +
    +# good
    +{
    +  a: 1, b: {
    +  c: 3
    +}}
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +{ a: 1, b: {
    +  c: 3
    +}}
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # bad
    +{ a: 1,
    +  b: {
    +  c: 3
    +}}
    +
    +# good
    +{ a: 1, b: {
    +  c: 3
    +}}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/FirstMethodArgumentLineBreak

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for a line break before the first argument in a +multi-line method call.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +method(foo, bar,
    +  baz)
    +
    +# good
    +method(
    +  foo, bar,
    +  baz)
    +
    +  # ignored
    +  method foo, bar,
    +    baz
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +method(foo, bar, {
    +  baz: "a",
    +  qux: "b",
    +})
    +
    +# good
    +method(
    +  foo, bar, {
    +  baz: "a",
    +  qux: "b",
    +})
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # bad
    +method(foo,
    +  bar,
    +  {
    +    baz: "a",
    +    qux: "b",
    +  }
    +)
    +
    +# good
    +method(foo, bar, {
    +  baz: "a",
    +  qux: "b",
    +})
    +
    +# good
    +method(
    +  foo,
    +  bar,
    +  {
    +    baz: "a",
    +    qux: "b",
    +  }
    +)
    +
    +
    +
    +
    +

    AllowedMethods: ['some_method']

    +
    +
    +
    # good
    +some_method(foo, bar,
    +  baz)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    AllowedMethods

    []

    Array

    +
    +
    +
    +
    +

    Layout/FirstMethodParameterLineBreak

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for a line break before the first parameter in a +multi-line method parameter definition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def method(foo, bar,
    +    baz)
    +  do_something
    +end
    +
    +# good
    +def method(
    +    foo, bar,
    +    baz)
    +  do_something
    +end
    +
    +# ignored
    +def method foo,
    +    bar
    +  do_something
    +end
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +def method(foo, bar, baz = {
    +  :a => "b",
    +})
    +  do_something
    +end
    +
    +# good
    +def method(
    +  foo, bar, baz = {
    +  :a => "b",
    +})
    +  do_something
    +end
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +def method(foo, bar, baz = {
    +  :a => "b",
    +})
    +  do_something
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/FirstParameterIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.77

    +
    +

    Checks the indentation of the first parameter in a method +definition. Parameters after the first one are checked by +Layout/ParameterAlignment, not by this cop.

    +
    +
    +

    For indenting the first argument of method calls, check out +Layout/FirstArgumentIndentation, which supports options related to +nesting that are irrelevant for method definitions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method(
    +first_param,
    +second_param)
    +  123
    +end
    +
    +
    +
    +

    EnforcedStyle: consistent (default)

    +
    +
    +
    # The first parameter should always be indented one step more than the
    +# preceding line.
    +
    +# good
    +def some_method(
    +  first_param,
    +second_param)
    +  123
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: align_parentheses

    +
    +
    +
    # The first parameter should always be indented one step more than the
    +# opening parenthesis.
    +
    +# good
    +def some_method(
    +                 first_param,
    +second_param)
    +  123
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    consistent

    consistent, align_parentheses

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/HashAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.16

    +
    +

    Check that the keys, separators, and values of a multi-line hash +literal are aligned according to configuration. The configuration +options are:

    +
    +
    +
      +
    • +

      key (left align keys, one space before hash rockets and values)

      +
    • +
    • +

      separator (align hash rockets and colons, right align keys)

      +
    • +
    • +

      table (left align keys, hash rockets, and values)

      +
    • +
    +
    +
    +

    The treatment of hashes passed as the last argument to a method call +can also be configured. The options are:

    +
    +
    +
      +
    • +

      always_inspect

      +
    • +
    • +

      always_ignore

      +
    • +
    • +

      ignore_implicit (without curly braces)

      +
    • +
    +
    +
    +

    Alternatively you can specify multiple allowed styles. That’s done by +passing a list of styles to EnforcedStyles.

    +
    +
    +

    Examples

    +
    +

    EnforcedHashRocketStyle: key (default)

    +
    +
    +
    # bad
    +{
    +  :foo => bar,
    +   :ba => baz
    +}
    +{
    +  :foo => bar,
    +  :ba  => baz
    +}
    +
    +# good
    +{
    +  :foo => bar,
    +  :ba => baz
    +}
    +
    +
    +
    +
    +

    EnforcedHashRocketStyle: separator

    +
    +
    +
    # bad
    +{
    +  :foo => bar,
    +  :ba => baz
    +}
    +{
    +  :foo => bar,
    +  :ba  => baz
    +}
    +
    +# good
    +{
    +  :foo => bar,
    +   :ba => baz
    +}
    +
    +
    +
    +
    +

    EnforcedHashRocketStyle: table

    +
    +
    +
    # bad
    +{
    +  :foo => bar,
    +   :ba => baz
    +}
    +
    +# good
    +{
    +  :foo => bar,
    +  :ba  => baz
    +}
    +
    +
    +
    +
    +

    EnforcedColonStyle: key (default)

    +
    +
    +
    # bad
    +{
    +  foo: bar,
    +   ba: baz
    +}
    +{
    +  foo: bar,
    +  ba:  baz
    +}
    +
    +# good
    +{
    +  foo: bar,
    +  ba: baz
    +}
    +
    +
    +
    +
    +

    EnforcedColonStyle: separator

    +
    +
    +
    # bad
    +{
    +  foo: bar,
    +  ba: baz
    +}
    +
    +# good
    +{
    +  foo: bar,
    +   ba: baz
    +}
    +
    +
    +
    +
    +

    EnforcedColonStyle: table

    +
    +
    +
    # bad
    +{
    +  foo: bar,
    +  ba: baz
    +}
    +
    +# good
    +{
    +  foo: bar,
    +  ba:  baz
    +}
    +
    +
    +
    +
    +

    EnforcedLastArgumentHashStyle: always_inspect (default)

    +
    +
    +
    # Inspect both implicit and explicit hashes.
    +
    +# bad
    +do_something(foo: 1,
    +  bar: 2)
    +
    +# bad
    +do_something({foo: 1,
    +  bar: 2})
    +
    +# good
    +do_something(foo: 1,
    +             bar: 2)
    +
    +# good
    +do_something(
    +  foo: 1,
    +  bar: 2
    +)
    +
    +# good
    +do_something({foo: 1,
    +              bar: 2})
    +
    +# good
    +do_something({
    +  foo: 1,
    +  bar: 2
    +})
    +
    +
    +
    +
    +

    EnforcedLastArgumentHashStyle: always_ignore

    +
    +
    +
    # Ignore both implicit and explicit hashes.
    +
    +# good
    +do_something(foo: 1,
    +  bar: 2)
    +
    +# good
    +do_something({foo: 1,
    +  bar: 2})
    +
    +
    +
    +
    +

    EnforcedLastArgumentHashStyle: ignore_implicit

    +
    +
    +
    # Ignore only implicit hashes.
    +
    +# bad
    +do_something({foo: 1,
    +  bar: 2})
    +
    +# good
    +do_something(foo: 1,
    +  bar: 2)
    +
    +
    +
    +
    +

    EnforcedLastArgumentHashStyle: ignore_explicit

    +
    +
    +
    # Ignore only explicit hashes.
    +
    +# bad
    +do_something(foo: 1,
    +  bar: 2)
    +
    +# good
    +do_something({foo: 1,
    +  bar: 2})
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedHashRocketStyle

    key

    key, separator, table

    EnforcedColonStyle

    key

    key, separator, table

    EnforcedLastArgumentHashStyle

    always_inspect

    always_inspect, always_ignore, ignore_implicit, ignore_explicit

    +
    +
    +
    +
    +

    Layout/HeredocArgumentClosingParenthesis

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.68

    -

    +
    +

    Checks for the placement of the closing parenthesis +in a method call that passes a HEREDOC string as an argument. +It should be placed at the end of the line containing the +opening HEREDOC tag.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +   foo(<<-SQL
    +     bar
    +   SQL
    +   )
    +
    +   foo(<<-SQL, 123, <<-NOSQL,
    +     bar
    +   SQL
    +     baz
    +   NOSQL
    +   )
    +
    +   foo(
    +     bar(<<-SQL
    +       baz
    +     SQL
    +     ),
    +     123,
    +   )
    +
    +# good
    +
    +   foo(<<-SQL)
    +     bar
    +   SQL
    +
    +   foo(<<-SQL, 123, <<-NOSQL)
    +     bar
    +   SQL
    +     baz
    +   NOSQL
    +
    +   foo(
    +     bar(<<-SQL),
    +       baz
    +     SQL
    +     123,
    +   )
    +
    +
    +
    + +
    +
    +
    +

    Layout/HeredocIndentation

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.3 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.85

    +
    +

    Checks the indentation of the here document bodies. The bodies +are indented one step.

    +
    +
    + + + + + +
    + + +When Layout/LineLength's AllowHeredoc is false (not default), + this cop does not add any offenses for long here documents to + avoid Layout/LineLength's offenses. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +<<-RUBY
    +something
    +RUBY
    +
    +# good
    +<<~RUBY
    +  something
    +RUBY
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/IndentationConsistency

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for inconsistent indentation.

    +
    +
    +

    The difference between indented_internal_methods and normal is +that the indented_internal_methods style prescribes that in +classes and modules the protected and private modifier keywords +shall be indented the same as public methods and that protected and +private members shall be indented one step more than the modifiers. +Other than that, both styles mean that entities on the same logical +depth shall have the same indentation.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: normal (default)

    +
    +
    +
    # bad
    +class A
    +  def test
    +    puts 'hello'
    +     puts 'world'
    +  end
    +end
    +
    +# bad
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +
    +  protected
    +
    +    def foo
    +    end
    +
    +  private
    +
    +    def bar
    +    end
    +end
    +
    +# good
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +end
    +
    +# good
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +
    +  protected
    +
    +  def foo
    +  end
    +
    +  private
    +
    +  def bar
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: indented_internal_methods

    +
    +
    +
    # bad
    +class A
    +  def test
    +    puts 'hello'
    +     puts 'world'
    +  end
    +end
    +
    +# bad
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +
    +  protected
    +
    +  def foo
    +  end
    +
    +  private
    +
    +  def bar
    +  end
    +end
    +
    +# good
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +end
    +
    +# good
    +class A
    +  def test
    +    puts 'hello'
    +    puts 'world'
    +  end
    +
    +  protected
    +
    +    def foo
    +    end
    +
    +  private
    +
    +    def bar
    +    end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    normal

    normal, indented_internal_methods

    +
    + +
    +
    +
    +

    Layout/IndentationStyle

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.82

    +
    +

    Checks that the indentation method is consistent. +Either tabs only or spaces only are used for indentation.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: spaces (default)

    +
    +
    +
    # bad
    +# This example uses a tab to indent bar.
    +def foo
    +  bar
    +end
    +
    +# good
    +# This example uses spaces to indent bar.
    +def foo
    +  bar
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: tabs

    +
    +
    +
    # bad
    +# This example uses spaces to indent bar.
    +def foo
    +  bar
    +end
    +
    +# good
    +# This example uses a tab to indent bar.
    +def foo
    +  bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IndentationWidth

    <none>

    Integer

    EnforcedStyle

    spaces

    spaces, tabs

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/IndentationWidth

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for indentation that doesn’t use the specified number of spaces. +The indentation width can be configured using the Width setting. The default width is 2.

    +
    +
    +

    See also the Layout/IndentationConsistency cop which is the companion to this one.

    +
    +
    +

    Examples

    +
    +

    Width: 2 (default)

    +
    +
    +
    # bad
    +class A
    + def test
    +  puts 'hello'
    + end
    +end
    +
    +# good
    +class A
    +  def test
    +    puts 'hello'
    +  end
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: ['^\s*module']

    +
    +
    +
    # bad
    +module A
    +class B
    +  def test
    +  puts 'hello'
    +  end
    +end
    +end
    +
    +# good
    +module A
    +class B
    +  def test
    +    puts 'hello'
    +  end
    +end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Width

    2

    Integer

    AllowedPatterns

    []

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/InitialIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for indentation of the first non-blank non-comment +line in a file.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +   class A
    +     def foo; end
    +   end
    +
    +# good
    +class A
    +  def foo; end
    +end
    +
    +
    +
    +
    +
    +
    +

    Layout/LeadingCommentSpace

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.73

    +
    +

    Checks whether comments have a leading space after the + denoting the start of the comment. The leading space is not +required for some RDoc special syntax, like ++, --, +:nodoc, =begin- and =end comments, "shebang" directives, +or rackup options.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +#Some comment
    +
    +# good
    +# Some comment
    +
    +
    +
    +

    AllowDoxygenCommentStyle: false (default)

    +
    +
    +
    # bad
    +
    +#**
    +# Some comment
    +# Another line of comment
    +#*
    +
    +
    +
    +
    +

    AllowDoxygenCommentStyle: true

    +
    +
    +
    # good
    +
    +#**
    +# Some comment
    +# Another line of comment
    +#*
    +
    +
    +
    +
    +

    AllowGemfileRubyComment: false (default)

    +
    +
    +
    # bad
    +
    +#ruby=2.7.0
    +#ruby-gemset=myproject
    +
    +
    +
    +
    +

    AllowGemfileRubyComment: true

    +
    +
    +
    # good
    +
    +#ruby=2.7.0
    +#ruby-gemset=myproject
    +
    +
    +
    +
    +

    AllowRBSInlineAnnotation: false (default)

    +
    +
    +
    # bad
    +
    +include Enumerable #[Integer]
    +
    +attr_reader :name #: String
    +attr_reader :age  #: Integer?
    +
    +
    +
    +
    +

    AllowRBSInlineAnnotation: true

    +
    +
    +
    # good
    +
    +include Enumerable #[Integer]
    +
    +attr_reader :name #: String
    +attr_reader :age  #: Integer?
    +
    +
    +
    +
    +

    AllowSteepAnnotation: false (default)

    +
    +
    +
    # bad
    +[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
    +  list << n
    +end
    +
    +name = 'John'      #: String
    +
    +
    +
    +
    +

    AllowSteepAnnotation: true

    +
    +
    +
    # good
    +
    +[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
    +  list << n
    +end
    +
    +name = 'John'      #: String
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowDoxygenCommentStyle

    false

    Boolean

    AllowGemfileRubyComment

    false

    Boolean

    AllowRBSInlineAnnotation

    false

    Boolean

    AllowSteepAnnotation

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/LeadingEmptyLines

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.57

    0.77

    +
    +

    Checks for unnecessary leading blank lines at the beginning +of a file.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# (start of file)
    +
    +class Foo
    +end
    +
    +# bad
    +# (start of file)
    +
    +# a comment
    +
    +# good
    +# (start of file)
    +class Foo
    +end
    +
    +# good
    +# (start of file)
    +# a comment
    +
    +
    +
    +
    +
    +
    +

    Layout/LineContinuationLeadingSpace

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.31

    1.45

    +
    +

    Checks that strings broken over multiple lines (by a backslash) contain +trailing spaces instead of leading spaces (default) or leading spaces +instead of trailing spaces.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: trailing (default)

    +
    +
    +
    # bad
    +'this text contains a lot of' \
    +'               spaces'
    +
    +# good
    +'this text contains a lot of               ' \
    +'spaces'
    +
    +# bad
    +'this text is too' \
    +' long'
    +
    +# good
    +'this text is too ' \
    +'long'
    +
    +
    +
    +
    +

    EnforcedStyle: leading

    +
    +
    +
    # bad
    +'this text contains a lot of               ' \
    +'spaces'
    +
    +# good
    +'this text contains a lot of' \
    +'               spaces'
    +
    +# bad
    +'this text is too ' \
    +'long'
    +
    +# good
    +'this text is too' \
    +' long'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    trailing

    leading, trailing

    +
    +
    +
    +
    +

    Layout/LineContinuationSpacing

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.31

    -

    +
    +

    Checks that the backslash of a line continuation is separated from +preceding text by exactly one space (default) or zero spaces.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: space (default)

    +
    +
    +
    # bad
    +'a'\
    +'b'  \
    +'c'
    +
    +# good
    +'a' \
    +'b' \
    +'c'
    +
    +
    +
    +
    +

    EnforcedStyle: no_space

    +
    +
    +
    # bad
    +'a' \
    +'b'  \
    +'c'
    +
    +# good
    +'a'\
    +'b'\
    +'c'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    space

    space, no_space

    +
    +
    +
    +
    +

    Layout/LineEndStringConcatenationIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.18

    -

    +
    +

    Checks the indentation of the next line after a line that ends with a string +literal and a backslash.

    +
    +
    +

    If EnforcedStyle: aligned is set, the concatenated string parts shall be aligned with the +first part. There are some exceptions, such as implicit return values, where the +concatenated string parts shall be indented regardless of EnforcedStyle configuration.

    +
    +
    +

    If EnforcedStyle: indented is set, it’s the second line that shall be indented one step +more than the first line. Lines 3 and forward shall be aligned with line 2.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  'x' \
    +  'y' \
    +  'z'
    +end
    +
    +my_hash = {
    +  first: 'a message' \
    +    'in two parts'
    +}
    +
    +# good
    +def some_method
    +  'x' \
    +    'y' \
    +    'z'
    +end
    +
    +
    +
    +

    EnforcedStyle: aligned (default)

    +
    +
    +
    # bad
    +puts 'x' \
    +  'y'
    +
    +my_hash = {
    +  first: 'a message' \
    +    'in two parts'
    +}
    +
    +# good
    +puts 'x' \
    +     'y'
    +
    +my_hash = {
    +  first: 'a message' \
    +         'in two parts'
    +}
    +
    +
    +
    +
    +

    EnforcedStyle: indented

    +
    +
    +
    # bad
    +result = 'x' \
    +         'y'
    +
    +my_hash = {
    +  first: 'a message' \
    +         'in two parts'
    +}
    +
    +# good
    +result = 'x' \
    +  'y'
    +
    +my_hash = {
    +  first: 'a message' \
    +    'in two parts'
    +}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    aligned

    aligned, indented

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/LineLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.25

    1.69

    +
    +

    Checks the length of lines in the source code. +The maximum length is configurable. +The tab size is configured in the IndentationWidth +of the Layout/IndentationStyle cop. +It also ignores a shebang line by default.

    +
    +
    +

    This cop has some autocorrection capabilities. +It can programmatically shorten certain long lines by +inserting line breaks into expressions that can be safely +split across lines. These include arrays, hashes, and +method calls with argument lists.

    +
    +
    +

    If autocorrection is enabled, the following cops +are recommended to further format the broken lines. +(Many of these are enabled by default.)

    +
    +
    +
      +
    • +

      Layout/ArgumentAlignment

      +
    • +
    • +

      Layout/ArrayAlignment

      +
    • +
    • +

      Layout/BlockAlignment

      +
    • +
    • +

      Layout/BlockEndNewline

      +
    • +
    • +

      Layout/ClosingParenthesisIndentation

      +
    • +
    • +

      Layout/FirstArgumentIndentation

      +
    • +
    • +

      Layout/FirstArrayElementIndentation

      +
    • +
    • +

      Layout/FirstHashElementIndentation

      +
    • +
    • +

      Layout/FirstParameterIndentation

      +
    • +
    • +

      Layout/HashAlignment

      +
    • +
    • +

      Layout/IndentationWidth

      +
    • +
    • +

      Layout/MultilineArrayLineBreaks

      +
    • +
    • +

      Layout/MultilineBlockLayout

      +
    • +
    • +

      Layout/MultilineHashBraceLayout

      +
    • +
    • +

      Layout/MultilineHashKeyLineBreaks

      +
    • +
    • +

      Layout/MultilineMethodArgumentLineBreaks

      +
    • +
    • +

      Layout/MultilineMethodParameterLineBreaks

      +
    • +
    • +

      Layout/ParameterAlignment

      +
    • +
    • +

      Style/BlockDelimiters

      +
    • +
    +
    +
    +

    Together, these cops will pretty print hashes, arrays, +method calls, etc. For example, let’s say the max columns +is 25:

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{foo: "0000000000", bar: "0000000000", baz: "0000000000"}
    +
    +# good
    +{foo: "0000000000",
    +bar: "0000000000", baz: "0000000000"}
    +
    +# good (with recommended cops enabled)
    +{
    +  foo: "0000000000",
    +  bar: "0000000000",
    +  baz: "0000000000",
    +}
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Max

    120

    Integer

    AllowHeredoc

    true

    Boolean

    AllowURI

    true

    Boolean

    URISchemes

    http, https

    Array

    IgnoreCopDirectives

    true

    Boolean

    AllowedPatterns

    []

    Array

    SplitStrings

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/MultilineArrayBraceLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that the closing brace in an array literal is either +on the same line as the last array element or on a new line.

    +
    +
    +

    When using the symmetrical (default) style:

    +
    +
    +

    If an array’s opening brace is on the same line as the first element +of the array, then the closing brace should be on the same line as +the last element of the array.

    +
    +
    +

    If an array’s opening brace is on the line above the first element +of the array, then the closing brace should be on the line below +the last element of the array.

    +
    +
    +

    When using the new_line style:

    +
    +
    +

    The closing brace of a multi-line array literal must be on the line +after the last element of the array.

    +
    +
    +

    When using the same_line style:

    +
    +
    +

    The closing brace of a multi-line array literal must be on the same +line as the last element of the array.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: symmetrical (default)

    +
    +
    +
    # bad
    +[ :a,
    +  :b
    +]
    +
    +# bad
    +[
    +  :a,
    +  :b ]
    +
    +# good
    +[ :a,
    +  :b ]
    +
    +# good
    +[
    +  :a,
    +  :b
    +]
    +
    +
    +
    +
    +

    EnforcedStyle: new_line

    +
    +
    +
    # bad
    +[
    +  :a,
    +  :b ]
    +
    +# bad
    +[ :a,
    +  :b ]
    +
    +# good
    +[ :a,
    +  :b
    +]
    +
    +# good
    +[
    +  :a,
    +  :b
    +]
    +
    +
    +
    +
    +

    EnforcedStyle: same_line

    +
    +
    +
    # bad
    +[ :a,
    +  :b
    +]
    +
    +# bad
    +[
    +  :a,
    +  :b
    +]
    +
    +# good
    +[
    +  :a,
    +  :b ]
    +
    +# good
    +[ :a,
    +  :b ]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    symmetrical

    symmetrical, new_line, same_line

    +
    +
    +
    +
    +

    Layout/MultilineArrayLineBreaks

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.67

    -

    +
    +

    Ensures that each item in a multi-line array +starts on a separate line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[
    +  a, b,
    +  c
    +]
    +
    +# good
    +[
    +  a,
    +  b,
    +  c
    +]
    +
    +# good
    +[
    +  a,
    +  b,
    +  foo(
    +    bar
    +  )
    +]
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +[a, b, foo(
    +  bar
    +)]
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +[a, b, foo(
    +  bar
    +)]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/MultilineAssignmentLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks whether the multiline assignments have a newline +after the assignment operator.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: new_line (default)

    +
    +
    +
    # bad
    +foo = if expression
    +  'bar'
    +end
    +
    +# good
    +foo =
    +  if expression
    +    'bar'
    +  end
    +
    +# good
    +foo =
    +  begin
    +    compute
    +  rescue => e
    +    nil
    +  end
    +
    +
    +
    +
    +

    EnforcedStyle: same_line

    +
    +
    +
    # good
    +foo = if expression
    +  'bar'
    +end
    +
    +
    +
    +
    +

    SupportedTypes: ['block', 'case', 'class', 'if', 'kwbegin', 'module'] (default)

    +
    +
    +
    # good
    +foo =
    +  if expression
    +    'bar'
    +  end
    +
    +# good
    +foo =
    +  [1].map do |i|
    +    i + 1
    +  end
    +
    +
    +
    +
    +

    SupportedTypes: ['block']

    +
    +
    +
    # good
    +foo = if expression
    +  'bar'
    +end
    +
    +# good
    +foo =
    +  [1].map do |i|
    +    'bar' * i
    +  end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    new_line

    same_line, new_line

    +
    + +
    +
    +
    +

    Layout/MultilineBlockLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks whether the multiline do end blocks have a newline +after the start of the block. Additionally, it checks whether the block +arguments, if any, are on the same line as the start of the +block. Putting block arguments on separate lines, because the whole +line would otherwise be too long, is accepted.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +blah do |i| foo(i)
    +  bar(i)
    +end
    +
    +# bad
    +blah do
    +  |i| foo(i)
    +  bar(i)
    +end
    +
    +# good
    +blah do |i|
    +  foo(i)
    +  bar(i)
    +end
    +
    +# bad
    +blah { |i| foo(i)
    +  bar(i)
    +}
    +
    +# good
    +blah { |i|
    +  foo(i)
    +  bar(i)
    +}
    +
    +# good
    +blah { |
    +  long_list,
    +  of_parameters,
    +  that_would_not,
    +  fit_on_one_line
    +|
    +  foo(i)
    +  bar(i)
    +}
    +
    +
    +
    +
    +
    +
    +

    Layout/MultilineHashBraceLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that the closing brace in a hash literal is either +on the same line as the last hash element, or a new line.

    +
    +
    +

    When using the symmetrical (default) style:

    +
    +
    +

    If a hash’s opening brace is on the same line as the first element +of the hash, then the closing brace should be on the same line as +the last element of the hash.

    +
    +
    +

    If a hash’s opening brace is on the line above the first element +of the hash, then the closing brace should be on the line below +the last element of the hash.

    +
    +
    +

    When using the new_line style:

    +
    +
    +

    The closing brace of a multi-line hash literal must be on the line +after the last element of the hash.

    +
    +
    +

    When using the same_line style:

    +
    +
    +

    The closing brace of a multi-line hash literal must be on the same +line as the last element of the hash.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: symmetrical (default)

    +
    +
    +
    # bad
    +{ a: 1,
    +  b: 2
    +}
    +# bad
    +{
    +  a: 1,
    +  b: 2 }
    +
    +# good
    +{ a: 1,
    +  b: 2 }
    +
    +# good
    +{
    +  a: 1,
    +  b: 2
    +}
    +
    +
    +
    +
    +

    EnforcedStyle: new_line

    +
    +
    +
    # bad
    +{
    +  a: 1,
    +  b: 2 }
    +
    +# bad
    +{ a: 1,
    +  b: 2 }
    +
    +# good
    +{ a: 1,
    +  b: 2
    +}
    +
    +# good
    +{
    +  a: 1,
    +  b: 2
    +}
    +
    +
    +
    +
    +

    EnforcedStyle: same_line

    +
    +
    +
    # bad
    +{ a: 1,
    +  b: 2
    +}
    +
    +# bad
    +{
    +  a: 1,
    +  b: 2
    +}
    +
    +# good
    +{
    +  a: 1,
    +  b: 2 }
    +
    +# good
    +{ a: 1,
    +  b: 2 }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    symmetrical

    symmetrical, new_line, same_line

    +
    +
    +
    +
    +

    Layout/MultilineHashKeyLineBreaks

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.67

    -

    +
    +

    Ensures that each key in a multi-line hash +starts on a separate line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{
    +  a: 1, b: 2,
    +  c: 3
    +}
    +
    +# good
    +{
    +  a: 1,
    +  b: 2,
    +  c: 3
    +}
    +
    +# good
    +{
    +  a: 1,
    +  b: {
    +    c: 3,
    +  }
    +}
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +{ a: 1, b: {
    +  c: 3,
    +}}
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +{ a: 1, b: {
    +  c: 3,
    +}}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/MultilineMethodArgumentLineBreaks

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.67

    -

    +
    +

    Ensures that each argument in a multi-line method call +starts on a separate line.

    +
    +
    + + + + + +
    + + +This cop does not move the first argument, if you want that to +be on a separate line, see Layout/FirstMethodArgumentLineBreak. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo(a, b,
    +  c
    +)
    +
    +# bad
    +foo(a, b, {
    +  foo: "bar",
    +})
    +
    +# good
    +foo(
    +  a,
    +  b,
    +  c
    +)
    +
    +# good
    +foo(a, b, c)
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # good
    +foo(
    +  a,
    +  b,
    +  {
    +    foo: "bar",
    +  }
    +)
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +foo(
    +  a,
    +  b,
    +  {
    +    foo: "bar",
    +  }
    +)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/MultilineMethodCallBraceLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that the closing brace in a method call is either +on the same line as the last method argument, or a new line.

    +
    +
    +

    When using the symmetrical (default) style:

    +
    +
    +

    If a method call’s opening brace is on the same line as the first +argument of the call, then the closing brace should be on the same +line as the last argument of the call.

    +
    +
    +

    If a method call’s opening brace is on the line above the first +argument of the call, then the closing brace should be on the line +below the last argument of the call.

    +
    +
    +

    When using the new_line style:

    +
    +
    +

    The closing brace of a multi-line method call must be on the line +after the last argument of the call.

    +
    +
    +

    When using the same_line style:

    +
    +
    +

    The closing brace of a multi-line method call must be on the same +line as the last argument of the call.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: symmetrical (default)

    +
    +
    +
    # bad
    +foo(a,
    +  b
    +)
    +
    +# bad
    +foo(
    +  a,
    +  b)
    +
    +# good
    +foo(a,
    +  b)
    +
    +# good
    +foo(
    +  a,
    +  b
    +)
    +
    +
    +
    +
    +

    EnforcedStyle: new_line

    +
    +
    +
    # bad
    +foo(
    +  a,
    +  b)
    +
    +# bad
    +foo(a,
    +  b)
    +
    +# good
    +foo(a,
    +  b
    +)
    +
    +# good
    +foo(
    +  a,
    +  b
    +)
    +
    +
    +
    +
    +

    EnforcedStyle: same_line

    +
    +
    +
    # bad
    +foo(a,
    +  b
    +)
    +
    +# bad
    +foo(
    +  a,
    +  b
    +)
    +
    +# good
    +foo(
    +  a,
    +  b)
    +
    +# good
    +foo(a,
    +  b)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    symmetrical

    symmetrical, new_line, same_line

    +
    +
    +
    +
    +

    Layout/MultilineMethodCallIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the indentation of the method name part in method calls +that span more than one line.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: aligned (default)

    +
    +
    +
    # bad
    +while myvariable
    +.b
    +  # do something
    +end
    +
    +# good
    +while myvariable
    +      .b
    +  # do something
    +end
    +
    +# good
    +Thing.a
    +     .b
    +     .c
    +
    +
    +
    +
    +

    EnforcedStyle: indented

    +
    +
    +
    # good
    +while myvariable
    +  .b
    +
    +  # do something
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: indented_relative_to_receiver

    +
    +
    +
    # good
    +while myvariable
    +        .a
    +        .b
    +
    +  # do something
    +end
    +
    +# good
    +myvariable = Thing
    +               .a
    +               .b
    +               .c
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    aligned

    aligned, indented, indented_relative_to_receiver

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/MultilineMethodDefinitionBraceLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that the closing brace in a method definition is either +on the same line as the last method parameter, or a new line.

    +
    +
    +

    When using the symmetrical (default) style:

    +
    +
    +

    If a method definition’s opening brace is on the same line as the +first parameter of the definition, then the closing brace should be +on the same line as the last parameter of the definition.

    +
    +
    +

    If a method definition’s opening brace is on the line above the first +parameter of the definition, then the closing brace should be on the +line below the last parameter of the definition.

    +
    +
    +

    When using the new_line style:

    +
    +
    +

    The closing brace of a multi-line method definition must be on the line +after the last parameter of the definition.

    +
    +
    +

    When using the same_line style:

    +
    +
    +

    The closing brace of a multi-line method definition must be on the same +line as the last parameter of the definition.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: symmetrical (default)

    +
    +
    +
    # bad
    +def foo(a,
    +  b
    +)
    +end
    +
    +# bad
    +def foo(
    +  a,
    +  b)
    +end
    +
    +# good
    +def foo(a,
    +  b)
    +end
    +
    +# good
    +def foo(
    +  a,
    +  b
    +)
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: new_line

    +
    +
    +
    # bad
    +def foo(
    +  a,
    +  b)
    +end
    +
    +# bad
    +def foo(a,
    +  b)
    +end
    +
    +# good
    +def foo(a,
    +  b
    +)
    +end
    +
    +# good
    +def foo(
    +  a,
    +  b
    +)
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: same_line

    +
    +
    +
    # bad
    +def foo(a,
    +  b
    +)
    +end
    +
    +# bad
    +def foo(
    +  a,
    +  b
    +)
    +end
    +
    +# good
    +def foo(
    +  a,
    +  b)
    +end
    +
    +# good
    +def foo(a,
    +  b)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    symmetrical

    symmetrical, new_line, same_line

    +
    +
    +
    +
    +

    Layout/MultilineMethodParameterLineBreaks

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    1.32

    -

    +
    +

    Ensures that each parameter in a multi-line method definition +starts on a separate line.

    +
    +
    + + + + + +
    + + +This cop does not move the first argument, if you want that to +be on a separate line, see Layout/FirstMethodParameterLineBreak. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo(a, b,
    +  c
    +)
    +end
    +
    +# good
    +def foo(
    +  a,
    +  b,
    +  c
    +)
    +end
    +
    +# good
    +def foo(
    +  a,
    +  b = {
    +    foo: "bar",
    +  }
    +)
    +end
    +
    +# good
    +def foo(a, b, c)
    +end
    +
    +
    +
    +

    AllowMultilineFinalElement: false (default)

    +
    +
    +
    # bad
    +def foo(a, b = {
    +  foo: "bar",
    +})
    +end
    +
    +
    +
    +
    +

    AllowMultilineFinalElement: true

    +
    +
    +
    # good
    +def foo(a, b = {
    +  foo: "bar",
    +})
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultilineFinalElement

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/MultilineOperationIndentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the indentation of the right hand side operand in binary operations that +span more than one line.

    +
    +
    +

    The aligned style checks that operators are aligned if they are part of an if or while +condition, an explicit return statement, etc. In other contexts, the second operand should +be indented regardless of enforced style.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: aligned (default)

    +
    +
    +
    # bad
    +if a +
    +    b
    +  something &&
    +  something_else
    +end
    +
    +# good
    +if a +
    +   b
    +  something &&
    +    something_else
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: indented

    +
    +
    +
    # bad
    +if a +
    +   b
    +  something &&
    +  something_else
    +end
    +
    +# good
    +if a +
    +    b
    +  something &&
    +    something_else
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    aligned

    aligned, indented

    IndentationWidth

    <none>

    Integer

    +
    +
    +
    +
    +

    Layout/ParameterAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.77

    +
    +

    Check that the parameters on a multi-line method call or definition are aligned.

    +
    +
    +

    To set the alignment of the first argument, use the +Layout/FirstParameterIndentation cop.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: with_first_parameter (default)

    +
    +
    +
    # good
    +
    +def foo(bar,
    +        baz)
    +  123
    +end
    +
    +def foo(
    +  bar,
    +  baz
    +)
    +  123
    +end
    +
    +# bad
    +
    +def foo(bar,
    +     baz)
    +  123
    +end
    +
    +# bad
    +
    +def foo(
    +  bar,
    +     baz)
    +  123
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: with_fixed_indentation

    +
    +
    +
    # good
    +
    +def foo(bar,
    +  baz)
    +  123
    +end
    +
    +def foo(
    +  bar,
    +  baz
    +)
    +  123
    +end
    +
    +# bad
    +
    +def foo(bar,
    +        baz)
    +  123
    +end
    +
    +# bad
    +
    +def foo(
    +  bar,
    +     baz)
    +  123
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    with_first_parameter

    with_first_parameter, with_fixed_indentation

    IndentationWidth

    <none>

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/RedundantLineBreak

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    1.13

    -

    +
    +

    Checks whether certain expressions, e.g. method calls, that could fit +completely on a single line, are broken up into multiple lines unnecessarily.

    +
    +
    +

    Examples

    +
    +

    any configuration

    +
    +
    +
    # bad
    +foo(
    +  a,
    +  b
    +)
    +
    +puts 'string that fits on ' \
    +     'a single line'
    +
    +things
    +  .select { |thing| thing.cond? }
    +  .join('-')
    +
    +# good
    +foo(a, b)
    +
    +puts 'string that fits on a single line'
    +
    +things.select { |thing| thing.cond? }.join('-')
    +
    +
    +
    +
    +

    InspectBlocks: false (default)

    +
    +
    +
    # good
    +foo(a) do |x|
    +  puts x
    +end
    +
    +
    +
    +
    +

    InspectBlocks: true

    +
    +
    +
    # bad
    +foo(a) do |x|
    +  puts x
    +end
    +
    +# good
    +foo(a) { |x| puts x }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    InspectBlocks

    false

    Boolean

    +
    +
    +
    +
    +

    Layout/RescueEnsureAlignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks whether the rescue and ensure keywords are aligned +properly.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  something
    +  rescue
    +  puts 'error'
    +end
    +
    +# good
    +begin
    +  something
    +rescue
    +  puts 'error'
    +end
    +
    +
    +
    +
    +
    +
    +

    Layout/SingleLineBlockChain

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    1.14

    -

    +
    +

    Checks if method calls are chained onto single line blocks. It considers that a +line break before the dot improves the readability of the code.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +example.select { |item| item.cond? }.join('-')
    +
    +# good
    +example.select { |item| item.cond? }
    +       .join('-')
    +
    +# good (not a concern for this cop)
    +example.select do |item|
    +  item.cond?
    +end.join('-')
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceAfterColon

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for colon (:) not followed by some kind of space. +N.B. this cop does not handle spaces after a ternary operator, which are +instead handled by Layout/SpaceAroundOperators.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def f(a:, b:2); {a:3}; end
    +
    +# good
    +def f(a:, b: 2); {a: 3}; end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceAfterComma

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for comma (,) not followed by some kind of space.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[1,2]
    +{ foo:bar,}
    +
    +# good
    +[1, 2]
    +{ foo:bar, }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceAfterMethodName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for space between a method name and a left parenthesis in defs.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def func (x) end
    +def method= (y) end
    +
    +# good
    +def func(x) end
    +def method=(y) end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceAfterNot

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for space after !.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +! something
    +
    +# good
    +!something
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceAfterSemicolon

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for semicolon (;) not followed by some kind of space.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x = 1;y = 2
    +
    +# good
    +x = 1; y = 2
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceAroundBlockParameters

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the spacing inside and after block parameters pipes. Line breaks +inside parameter pipes are checked by Layout/MultilineBlockLayout and +not by this cop.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyleInsidePipes: no_space (default)

    +
    +
    +
    # bad
    +{}.each { | x,  y |puts x }
    +->( x,  y ) { puts x }
    +
    +# good
    +{}.each { |x, y| puts x }
    +->(x, y) { puts x }
    +
    +
    +
    +
    +

    EnforcedStyleInsidePipes: space

    +
    +
    +
    # bad
    +{}.each { |x,  y| puts x }
    +->(x,  y) { puts x }
    +
    +# good
    +{}.each { | x, y | puts x }
    +->( x, y ) { puts x }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleInsidePipes

    no_space

    space, no_space

    +
    +
    +
    +
    +

    Layout/SpaceAroundEqualsInParameterDefault

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that the equals signs in parameter default assignments +have or don’t have surrounding space depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: space (default)

    +
    +
    +
    # bad
    +def some_method(arg1=:default, arg2=nil, arg3=[])
    +  # do something...
    +end
    +
    +# good
    +def some_method(arg1 = :default, arg2 = nil, arg3 = [])
    +  # do something...
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: no_space

    +
    +
    +
    # bad
    +def some_method(arg1 = :default, arg2 = nil, arg3 = [])
    +  # do something...
    +end
    +
    +# good
    +def some_method(arg1=:default, arg2=nil, arg3=[])
    +  # do something...
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    space

    space, no_space

    +
    + +
    +
    +
    +

    Layout/SpaceAroundKeyword

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks the spacing around the keywords.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something 'test'do|x|
    +end
    +
    +while(something)
    +end
    +
    +something = 123if test
    +
    +# good
    +something 'test' do |x|
    +end
    +
    +while (something)
    +end
    +
    +something = 123 if test
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceAroundMethodCallOperator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.82

    -

    +
    +

    Checks method call operators to not have spaces around them.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo. bar
    +foo .bar
    +foo . bar
    +foo. bar .buzz
    +foo
    +  . bar
    +  . buzz
    +foo&. bar
    +foo &.bar
    +foo &. bar
    +foo &. bar&. buzz
    +RuboCop:: Cop
    +RuboCop:: Cop:: Base
    +:: RuboCop::Cop
    +
    +# good
    +foo.bar
    +foo.bar.buzz
    +foo
    +  .bar
    +  .buzz
    +foo&.bar
    +foo&.bar&.buzz
    +RuboCop::Cop
    +RuboCop::Cop::Base
    +::RuboCop::Cop
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceAroundOperators

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that operators have space around them, except for ** which +should or shouldn’t have surrounding space depending on configuration. +It allows vertical alignment consisting of one or more whitespace +around operators.

    +
    +
    +

    This cop has AllowForAlignment option. When true, allows most +uses of extra spacing if the intent is to align with an operator on +the previous or next line, not counting empty lines or comment lines.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +total = 3*4
    +"apple"+"juice"
    +my_number = 38/4
    +
    +# good
    +total = 3 * 4
    +"apple" + "juice"
    +my_number = 38 / 4
    +
    +
    +
    +

    AllowForAlignment: true (default)

    +
    +
    +
    # good
    +{
    +  1 =>  2,
    +  11 => 3
    +}
    +
    +
    +
    +
    +

    AllowForAlignment: false

    +
    +
    +
    # bad
    +{
    +  1 =>  2,
    +  11 => 3
    +}
    +
    +
    +
    +
    +

    EnforcedStyleForExponentOperator: no_space (default)

    +
    +
    +
    # bad
    +a ** b
    +
    +# good
    +a**b
    +
    +
    +
    +
    +

    EnforcedStyleForExponentOperator: space

    +
    +
    +
    # bad
    +a**b
    +
    +# good
    +a ** b
    +
    +
    +
    +
    +

    EnforcedStyleForRationalLiterals: no_space (default)

    +
    +
    +
    # bad
    +1 / 48r
    +
    +# good
    +1/48r
    +
    +
    +
    +
    +

    EnforcedStyleForRationalLiterals: space

    +
    +
    +
    # bad
    +1/48r
    +
    +# good
    +1 / 48r
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowForAlignment

    true

    Boolean

    EnforcedStyleForExponentOperator

    no_space

    space, no_space

    EnforcedStyleForRationalLiterals

    no_space

    space, no_space

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceBeforeBlockBraces

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.52

    +
    +

    Checks that block braces have or don’t have a space before the opening +brace depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: space (default)

    +
    +
    +
    # bad
    +foo.map{ |a|
    +  a.bar.to_s
    +}
    +
    +# good
    +foo.map { |a|
    +  a.bar.to_s
    +}
    +
    +
    +
    +
    +

    EnforcedStyle: no_space

    +
    +
    +
    # bad
    +foo.map { |a|
    +  a.bar.to_s
    +}
    +
    +# good
    +foo.map{ |a|
    +  a.bar.to_s
    +}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: space (default)

    +
    +
    +
    # bad
    +7.times{}
    +
    +# good
    +7.times {}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: no_space

    +
    +
    +
    # bad
    +7.times {}
    +
    +# good
    +7.times{}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    space

    space, no_space

    EnforcedStyleForEmptyBraces

    space

    space, no_space

    +
    +
    +
    +
    +

    Layout/SpaceBeforeBrackets

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.7

    -

    +
    +

    Checks for space between the name of a receiver and a left +brackets.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +collection [index_or_key]
    +
    +# good
    +collection[index_or_key]
    +
    +
    +
    + +
    +
    +
    +

    Layout/SpaceBeforeComma

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for comma (,) preceded by space.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[1 , 2 , 3]
    +a(1 , 2)
    +each { |a , b| }
    +
    +# good
    +[1, 2, 3]
    +a(1, 2)
    +each { |a, b| }
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceBeforeComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for missing space between a token and a comment on the +same line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +1 + 1# this operation does ...
    +
    +# good
    +1 + 1 # this operation does ...
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceBeforeFirstArg

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that exactly one space is used between a method name and the +first argument for method calls without parentheses.

    +
    +
    +

    Alternatively, extra spaces can be added to align the argument with +something on a preceding or following line, if the AllowForAlignment +config parameter is true.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something  x
    +something   y, z
    +something'hello'
    +
    +# good
    +something x
    +something y, z
    +something 'hello'
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowForAlignment

    true

    Boolean

    +
    +
    +
    +
    +

    Layout/SpaceBeforeSemicolon

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for semicolon (;) preceded by space.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x = 1 ; y = 2
    +
    +# good
    +x = 1; y = 2
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceInLambdaLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for spaces between and opening parameter +parenthesis (() in lambda literals.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: require_no_space (default)

    +
    +
    +
    # bad
    +a = -> (x, y) { x + y }
    +
    +# good
    +a = ->(x, y) { x + y }
    +
    +
    +
    +
    +

    EnforcedStyle: require_space

    +
    +
    +
    # bad
    +a = ->(x, y) { x + y }
    +
    +# good
    +a = -> (x, y) { x + y }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    require_no_space

    require_no_space, require_space

    +
    +
    +
    +
    +

    Layout/SpaceInsideArrayLiteralBrackets

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks that brackets used for array literals have or don’t have +surrounding space depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_space (default)

    +
    +
    +
    # The `no_space` style enforces that array literals have
    +# no surrounding space.
    +
    +# bad
    +array = [ a, b, c, d ]
    +array = [ a, [ b, c ]]
    +
    +# good
    +array = [a, b, c, d]
    +array = [a, [b, c]]
    +
    +
    +
    +
    +

    EnforcedStyle: space

    +
    +
    +
    # The `space` style enforces that array literals have
    +# surrounding space.
    +
    +# bad
    +array = [a, b, c, d]
    +array = [ a, [ b, c ]]
    +
    +# good
    +array = [ a, b, c, d ]
    +array = [ a, [ b, c ] ]
    +
    +
    +
    +
    +

    EnforcedStyle: compact

    +
    +
    +
    # The `compact` style normally requires a space inside
    +# array brackets, with the exception that successive left
    +# or right brackets are collapsed together in nested arrays.
    +
    +# bad
    +array = [a, b, c, d]
    +array = [ a, [ b, c ] ]
    +array = [
    +  [ a ],
    +  [ b, c ]
    +]
    +
    +# good
    +array = [ a, b, c, d ]
    +array = [ a, [ b, c ]]
    +array = [[ a ],
    +  [ b, c ]]
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBrackets: no_space (default)

    +
    +
    +
    # The `no_space` EnforcedStyleForEmptyBrackets style enforces that
    +# empty array brackets do not contain spaces.
    +
    +# bad
    +foo = [ ]
    +bar = [     ]
    +
    +# good
    +foo = []
    +bar = []
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBrackets: space

    +
    +
    +
    # The `space` EnforcedStyleForEmptyBrackets style enforces that
    +# empty array brackets contain exactly one space.
    +
    +# bad
    +foo = []
    +bar = [    ]
    +
    +# good
    +foo = [ ]
    +bar = [ ]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_space

    space, no_space, compact

    EnforcedStyleForEmptyBrackets

    no_space

    space, no_space

    +
    +
    +
    +
    +

    Layout/SpaceInsideArrayPercentLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for unnecessary additional spaces inside array percent literals +(i.e. %i/%w).

    +
    +
    +

    Note that blank percent literals (e.g. %i( )) are checked by +Layout/SpaceInsidePercentLiteralDelimiters.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%w(foo  bar  baz)
    +# good
    +%i(foo bar baz)
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceInsideBlockBraces

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that block braces have or don’t have surrounding space inside +them on configuration. For blocks taking parameters, it checks that the +left brace has or doesn’t have trailing space depending on +configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: space (default)

    +
    +
    +
    # The `space` style enforces that block braces have
    +# surrounding space.
    +
    +# bad
    +some_array.each {puts e}
    +
    +# good
    +some_array.each { puts e }
    +
    +
    +
    +
    +

    EnforcedStyle: no_space

    +
    +
    +
    # The `no_space` style enforces that block braces don't
    +# have surrounding space.
    +
    +# bad
    +some_array.each { puts e }
    +
    +# good
    +some_array.each {puts e}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: no_space (default)

    +
    +
    +
    # The `no_space` EnforcedStyleForEmptyBraces style enforces that
    +# block braces don't have a space in between when empty.
    +
    +# bad
    +some_array.each {   }
    +some_array.each {  }
    +some_array.each { }
    +
    +# good
    +some_array.each {}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: space

    +
    +
    +
    # The `space` EnforcedStyleForEmptyBraces style enforces that
    +# block braces have at least a space in between when empty.
    +
    +# bad
    +some_array.each {}
    +
    +# good
    +some_array.each { }
    +some_array.each {  }
    +some_array.each {   }
    +
    +
    +
    +
    +

    SpaceBeforeBlockParameters: true (default)

    +
    +
    +
    # The SpaceBeforeBlockParameters style set to `true` enforces that
    +# there is a space between `{` and `|`. Overrides `EnforcedStyle`
    +# if there is a conflict.
    +
    +# bad
    +[1, 2, 3].each {|n| n * 2 }
    +
    +# good
    +[1, 2, 3].each { |n| n * 2 }
    +
    +
    +
    +
    +

    SpaceBeforeBlockParameters: false

    +
    +
    +
    # The SpaceBeforeBlockParameters style set to `false` enforces that
    +# there is no space between `{` and `|`. Overrides `EnforcedStyle`
    +# if there is a conflict.
    +
    +# bad
    +[1, 2, 3].each { |n| n * 2 }
    +
    +# good
    +[1, 2, 3].each {|n| n * 2 }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    space

    space, no_space

    EnforcedStyleForEmptyBraces

    no_space

    space, no_space

    SpaceBeforeBlockParameters

    true

    Boolean

    +
    +
    +
    +
    +

    Layout/SpaceInsideHashLiteralBraces

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks that braces used for hash literals have or don’t have +surrounding space depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: space (default)

    +
    +
    +
    # The `space` style enforces that hash literals have
    +# surrounding space.
    +
    +# bad
    +h = {a: 1, b: 2}
    +foo = {{ a: 1 } => { b: { c: 2 }}}
    +
    +# good
    +h = { a: 1, b: 2 }
    +foo = { { a: 1 } => { b: { c: 2 } } }
    +
    +
    +
    +
    +

    EnforcedStyle: no_space

    +
    +
    +
    # The `no_space` style enforces that hash literals have
    +# no surrounding space.
    +
    +# bad
    +h = { a: 1, b: 2 }
    +foo = {{ a: 1 } => { b: { c: 2 }}}
    +
    +# good
    +h = {a: 1, b: 2}
    +foo = {{a: 1} => {b: {c: 2}}}
    +
    +
    +
    +
    +

    EnforcedStyle: compact

    +
    +
    +
    # The `compact` style normally requires a space inside
    +# hash braces, with the exception that successive left
    +# braces or right braces are collapsed together in nested hashes.
    +
    +# bad
    +h = { a: { b: 2 } }
    +foo = { { a: 1 } => { b: { c: 2 } } }
    +
    +# good
    +h = { a: { b: 2 }}
    +foo = {{ a: 1 } => { b: { c: 2 }}}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: no_space (default)

    +
    +
    +
    # The `no_space` EnforcedStyleForEmptyBraces style enforces that
    +# empty hash braces do not contain spaces.
    +
    +# bad
    +foo = { }
    +bar = {    }
    +baz = {
    +}
    +
    +# good
    +foo = {}
    +bar = {}
    +baz = {}
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBraces: space

    +
    +
    +
    # The `space` EnforcedStyleForEmptyBraces style enforces that
    +# empty hash braces contain space.
    +
    +# bad
    +foo = {}
    +
    +# good
    +foo = { }
    +foo = {    }
    +foo = {
    +}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    space

    space, no_space, compact

    EnforcedStyleForEmptyBraces

    no_space

    space, no_space

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceInsideParens

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.22

    +
    +

    Checks for spaces inside ordinary round parentheses.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_space (default)

    +
    +
    +
    # The `no_space` style enforces that parentheses do not have spaces.
    +
    +# bad
    +f( 3)
    +g = (a + 3 )
    +f( )
    +
    +# good
    +f(3)
    +g = (a + 3)
    +f()
    +
    +
    +
    +
    +

    EnforcedStyle: space

    +
    +
    +
    # The `space` style enforces that parentheses have a space at the
    +# beginning and end.
    +# Note: Empty parentheses should not have spaces.
    +
    +# bad
    +f(3)
    +g = (a + 3)
    +y( )
    +
    +# good
    +f( 3 )
    +g = ( a + 3 )
    +y()
    +
    +
    +
    +
    +

    EnforcedStyle: compact

    +
    +
    +
    # The `compact` style enforces that parentheses have a space at the
    +# beginning with the exception that successive parentheses are allowed.
    +# Note: Empty parentheses should not have spaces.
    +
    +# bad
    +f(3)
    +g = (a + 3)
    +y( )
    +g( f( x ) )
    +g( f( x( 3 ) ), 5 )
    +g( ( ( 3 + 5 ) * f) ** x, 5 )
    +
    +# good
    +f( 3 )
    +g = ( a + 3 )
    +y()
    +g( f( x ))
    +g( f( x( 3 )), 5 )
    +g((( 3 + 5 ) * f ) ** x, 5 )
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_space

    space, compact, no_space

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/SpaceInsidePercentLiteralDelimiters

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for unnecessary additional spaces inside the delimiters of +%i/%w/%x literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%i( foo bar baz )
    +
    +# good
    +%i(foo bar baz)
    +
    +# bad
    +%w( foo bar baz )
    +
    +# good
    +%w(foo bar baz)
    +
    +# bad
    +%x(  ls -l )
    +
    +# good
    +%x(ls -l)
    +
    +# bad
    +%w( )
    +%w(
    +)
    +
    +# good
    +%w()
    +
    +
    +
    +
    +
    +
    +

    Layout/SpaceInsideRangeLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for spaces inside range literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +1 .. 3
    +
    +# good
    +1..3
    +
    +# bad
    +'a' .. 'z'
    +
    +# good
    +'a'..'z'
    +
    +
    +
    + +
    +
    +
    +

    Layout/SpaceInsideReferenceBrackets

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    0.53

    +
    +

    Checks that reference brackets have or don’t have +surrounding space depending on configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_space (default)

    +
    +
    +
    # The `no_space` style enforces that reference brackets have
    +# no surrounding space.
    +
    +# bad
    +hash[ :key ]
    +array[ index ]
    +
    +# good
    +hash[:key]
    +array[index]
    +
    +
    +
    +
    +

    EnforcedStyle: space

    +
    +
    +
    # The `space` style enforces that reference brackets have
    +# surrounding space.
    +
    +# bad
    +hash[:key]
    +array[index]
    +
    +# good
    +hash[ :key ]
    +array[ index ]
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBrackets: no_space (default)

    +
    +
    +
    # The `no_space` EnforcedStyleForEmptyBrackets style enforces that
    +# empty reference brackets do not contain spaces.
    +
    +# bad
    +foo[ ]
    +foo[     ]
    +foo[
    +]
    +
    +# good
    +foo[]
    +
    +
    +
    +
    +

    EnforcedStyleForEmptyBrackets: space

    +
    +
    +
    # The `space` EnforcedStyleForEmptyBrackets style enforces that
    +# empty reference brackets contain exactly one space.
    +
    +# bad
    +foo[]
    +foo[    ]
    +foo[
    +]
    +
    +# good
    +foo[ ]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_space

    space, no_space

    EnforcedStyleForEmptyBrackets

    no_space

    space, no_space

    +
    +
    +
    +
    +

    Layout/SpaceInsideStringInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Checks for whitespace within string interpolations.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: no_space (default)

    +
    +
    +
    # bad
    +   var = "This is the #{ space } example"
    +
    +# good
    +   var = "This is the #{no_space} example"
    +
    +
    +
    +
    +

    EnforcedStyle: space

    +
    +
    +
    # bad
    +   var = "This is the #{no_space} example"
    +
    +# good
    +   var = "This is the #{ space } example"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    no_space

    space, no_space

    +
    + +
    +
    +
    +

    Layout/TrailingEmptyLines

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.77

    +
    +

    Looks for trailing blank lines and a final newline in the +source code.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: final_newline (default)

    +
    +
    +
    # `final_newline` looks for one newline at the end of files.
    +
    +# bad
    +class Foo; end
    +
    +# EOF
    +
    +# bad
    +class Foo; end # EOF
    +
    +# good
    +class Foo; end
    +# EOF
    +
    +
    +
    +
    +

    EnforcedStyle: final_blank_line

    +
    +
    +
    # `final_blank_line` looks for one blank line followed by a new line
    +# at the end of files.
    +
    +# bad
    +class Foo; end
    +# EOF
    +
    +# bad
    +class Foo; end # EOF
    +
    +# good
    +class Foo; end
    +
    +# EOF
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    final_newline

    final_newline, final_blank_line

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Layout/TrailingWhitespace

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.0

    +
    +

    Looks for trailing whitespace in the source code.

    +
    +
    +

    Examples

    +
    +
    +
    # The line in this example contains spaces after the 0.
    +# bad
    +x = 0
    +
    +# The line in this example ends directly after the 0.
    +# good
    +x = 0
    +
    +
    +
    +

    AllowInHeredoc: false (default)

    +
    +
    +
    # The line in this example contains spaces after the 0.
    +# bad
    +code = <<~RUBY
    +  x = 0
    +RUBY
    +
    +# ok
    +code = <<~RUBY
    +  x = 0 #{}
    +RUBY
    +
    +# good
    +trailing_whitespace = ' '
    +code = <<~RUBY
    +  x = 0#{trailing_whitespace}
    +RUBY
    +
    +
    +
    +
    +

    AllowInHeredoc: true

    +
    +
    +
    # The line in this example contains spaces after the 0.
    +# good
    +code = <<~RUBY
    +  x = 0
    +RUBY
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowInHeredoc

    false

    Boolean

    +
    + +
    +
    +
    +

    AllowMultilineFinalElement

    +
    +
    +

    AllowMultilineFinalElement is a boolean option (false by default) that +is available on the following Layout cops:

    +
    +
    +
      +
    • +

      FirstArrayElementLineBreak

      +
    • +
    • +

      FirstHashElementLineBreak

      +
    • +
    • +

      FirstMethodArgumentLineBreak

      +
    • +
    • +

      FirstMethodParameterLineBreak

      +
    • +
    • +

      MultilineArrayLineBreaks

      +
    • +
    • +

      MultilineHashKeyLineBreaks

      +
    • +
    • +

      MultilineMethodArgumentLineBreaks

      +
    • +
    • +

      MultilineMethodParameterLineBreaks

      +
    • +
    +
    +
    +

    Those cops ignore their respective expressions if all or the elements of +the expression are on the same line. If AllowMultilineFinalElement is +set to true, the cop will also ignore multiline expressions if the last +element starts on the same line, but ends on a different one.

    +
    +
    +

    This works well with Layout/LineLength to present elements on +individual lines when wrapping, while not affecting expressions +that are already short enough, and only considered multiline +because of their last element.

    +
    +
    +

    Each cop can be configured independently allowing for more fine-grained +control over what is considered good ok in the codebase.

    +
    +
    +

    Examples

    +
    +

    Here are some examples of real world expressions that get wrapped +by their respective cops, but that are considered ok when setting +AllowMultilineFinalElement to true on those same cops.

    +
    +
    +
    +
    # good
    +
    +# FirstArrayElementLineBreak and MultilineArrayLineBreaks
    +
    +# Array of error containing a single error
    +errors = [{
    +  error: "Something went wrong",
    +  error_code: error_code,
    +}]
    +
    +# Array of flags, with last flag computed
    +flags = [:a, :b, foo(
    +  bar,
    +  baz
    +)]
    +
    +# FirstHashElementLineBreak and MultilineHashKeyLineBreaks
    +hash = { foo: 1, bar: 2, baz: {
    +  c: 1,
    +  d: 2
    +}}
    +
    +# FirstMethodArgumentLineBreak and MultilineMethodArgumentLineBreaks
    +single_argument_hash_method_call({
    +  a: 1,
    +  b: 2,
    +  c: 3
    +})
    +
    +# Call some method, with a long last argument, that is a hash
    +write_log(:error,  {
    +  "job_class" => job.class.name,
    +  "resource" => resource.id,
    +  "message" => "Something wrong happened here",
    +})
    +
    +# Rails before action with long last argument
    +before_action :load_something, only: [
    +  :show,
    +  :list,
    +  :some_other_long_action_name,
    +]
    +
    +# Rails validation with inline callback
    +validate :name, presence: true, on: [:create, :activate], if: -> {
    +  active? && some_relationship.any?
    +}
    +
    +# Rails after commit hook, with some Sorbet bindings
    +after_commit :geolocate, unless: -> {
    +  T.bind(self, Address)
    +  archived? || invalid?
    +}
    +
    +# FirstMethodParameterLineBreak and MultilineMethodParameterLineBreaks
    +
    +# Method with a long last parameter default value
    +def foo(foo, bar, baz = {
    +  a: 1,
    +  b: 2,
    +  c: 3
    +})
    +  do_something
    +end
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_lint.html b/docs/rubocop/1.69/cops_lint.html new file mode 100644 index 000000000..63661e0a2 --- /dev/null +++ b/docs/rubocop/1.69/cops_lint.html @@ -0,0 +1,12738 @@ + + + + + + Lint :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Lint

    +
    +

    Lint/AmbiguousAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.7

    -

    +
    +

    Checks for mistyped shorthand assignments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x =- y
    +x =+ y
    +x =* y
    +x =! y
    +
    +# good
    +x -= y # or x = -y
    +x += y # or x = +y
    +x *= y # or x = *y
    +x != y # or x = !y
    +
    +
    +
    +
    +
    +
    +

    Lint/AmbiguousBlockAssociation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.48

    1.13

    +
    +

    Checks for ambiguous block association with method +when param passed without parentheses.

    +
    +
    +

    This cop can customize allowed methods with AllowedMethods. +By default, there are no methods to allowed.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_method a { |val| puts val }
    +
    +# good
    +# With parentheses, there's no ambiguity.
    +some_method(a { |val| puts val })
    +# or (different meaning)
    +some_method(a) { |val| puts val }
    +
    +# good
    +# Operator methods require no disambiguation
    +foo == bar { |b| b.baz }
    +
    +# good
    +# Lambda arguments require no disambiguation
    +foo = ->(bar) { bar.baz }
    +
    +
    +
    +

    AllowedMethods: [] (default)

    +
    +
    +
    # bad
    +expect { do_something }.to change { object.attribute }
    +
    +
    +
    +
    +

    AllowedMethods: [change]

    +
    +
    +
    # good
    +expect { do_something }.to change { object.attribute }
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +expect { do_something }.to change { object.attribute }
    +
    +
    +
    +
    +

    AllowedPatterns: ['change']

    +
    +
    +
    # good
    +expect { do_something }.to change { object.attribute }
    +expect { do_something }.to not_change { object.attribute }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    +
    +
    +
    +

    Lint/AmbiguousOperator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.17

    0.83

    +
    +

    Checks for ambiguous operators in the first argument of a +method invocation without parentheses.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# The `*` is interpreted as a splat operator but it could possibly be
    +# a `*` method invocation (i.e. `do_something.*(some_array)`).
    +do_something *some_array
    +
    +# good
    +
    +# With parentheses, there's no ambiguity.
    +do_something(*some_array)
    +
    +
    +
    + +
    +
    +
    +

    Lint/AmbiguousOperatorPrecedence

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.21

    -

    +
    +

    Looks for expressions containing multiple binary operators +where precedence is ambiguous due to lack of parentheses. For example, +in 1 + 2 * 3, the multiplication will happen before the addition, but +lexically it appears that the addition will happen first.

    +
    +
    +

    The cop does not consider unary operators (ie. !a or -b) or comparison +operators (ie. a =~ b) because those are not ambiguous.

    +
    +
    + + + + + +
    + + +Ranges are handled by Lint/AmbiguousRange. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a + b * c
    +a || b && c
    +a ** b + c
    +
    +# good (different precedence)
    +a + (b * c)
    +a || (b && c)
    +(a ** b) + c
    +
    +# good (same precedence)
    +a + b + c
    +a * b / c % d
    +
    +
    +
    +
    +
    +
    +

    Lint/AmbiguousRange

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.19

    -

    +
    +

    Checks for ambiguous ranges.

    +
    +
    +

    Ranges have quite low precedence, which leads to unexpected behavior when +using a range with other operators. This cop avoids that by making ranges +explicit by requiring parenthesis around complex range boundaries (anything +that is not a literal: numerics, strings, symbols, etc.).

    +
    +
    +

    This cop can be configured with RequireParenthesesForMethodChains in order to +specify whether method chains (including self.foo) should be wrapped in parens +by this cop.

    +
    +
    + + + + + +
    + + +Regardless of this configuration, if a method receiver is a basic literal +value, it will be wrapped in order to prevent the ambiguity of 1..2.to_a. +
    +
    +
    +

    Safety

    +
    +

    The cop autocorrects by wrapping the entire boundary in parentheses, which +makes the outcome more explicit but is possible to not be the intention of the +programmer. For this reason, this cop’s autocorrect is unsafe (it will not +change the behavior of the code, but will not necessarily match the +intent of the program).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x || 1..2
    +(x || 1..2)
    +1..2.to_a
    +
    +# good, unambiguous
    +1..2
    +'a'..'z'
    +:bar..:baz
    +MyClass::MIN..MyClass::MAX
    +@min..@max
    +a..b
    +-a..b
    +
    +# good, ambiguity removed
    +x || (1..2)
    +(x || 1)..2
    +(x || 1)..(y || 2)
    +(1..2).to_a
    +
    +
    +
    +

    RequireParenthesesForMethodChains: false (default)

    +
    +
    +
    # good
    +a.foo..b.bar
    +(a.foo)..(b.bar)
    +
    +
    +
    +
    +

    RequireParenthesesForMethodChains: true

    +
    +
    +
    # bad
    +a.foo..b.bar
    +
    +# good
    +(a.foo)..(b.bar)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    RequireParenthesesForMethodChains

    false

    Boolean

    +
    +
    +
    +
    +

    Lint/AmbiguousRegexpLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.17

    0.83

    +
    +

    Checks for ambiguous regexp literals in the first argument of +a method invocation without parentheses.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# This is interpreted as a method invocation with a regexp literal,
    +# but it could possibly be `/` method invocations.
    +# (i.e. `do_something./(pattern)./(i)`)
    +do_something /pattern/i
    +
    +# good
    +
    +# With parentheses, there's no ambiguity.
    +do_something(/pattern/i)
    +
    +
    +
    +
    +
    +
    +

    Lint/AssignmentInCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.9

    1.45

    +
    +

    Checks for assignments in the conditions of +if/while/until.

    +
    +
    +

    AllowSafeAssignment option for safe assignment. +By safe assignment we mean putting parentheses around +an assignment to indicate "I know I’m using an assignment +as a condition. It’s not a mistake."

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because it assumes that +the author meant to use an assignment result as a condition.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if some_var = value
    +  do_something
    +end
    +
    +# good
    +if some_var == value
    +  do_something
    +end
    +
    +
    +
    +

    AllowSafeAssignment: true (default)

    +
    +
    +
    # good
    +if (some_var = value)
    +  do_something
    +end
    +
    +
    +
    +
    +

    AllowSafeAssignment: false

    +
    +
    +
    # bad
    +if (some_var = value)
    +  do_something
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowSafeAssignment

    true

    Boolean

    +
    + +
    +
    +
    +

    Lint/BigDecimalNew

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    BigDecimal.new() is deprecated since BigDecimal 1.3.3. +This cop identifies places where BigDecimal.new() +can be replaced by BigDecimal().

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +BigDecimal.new(123.456, 3)
    +
    +# good
    +BigDecimal(123.456, 3)
    +
    +
    +
    +
    +
    +
    +

    Lint/BinaryOperatorWithIdenticalOperands

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.89

    1.69

    +
    +

    Checks for places where binary operator has identical operands.

    +
    +
    +

    It covers comparison operators: ==, ===, =~, >, >=, <, ; +bitwise operators: |, ^, &; +boolean operators: &&, || +and "spaceship" operator - <⇒.

    +
    +
    +

    Simple arithmetic operations are allowed by this cop: +, , *, << and >>. +Although these can be rewritten in a different way, it should not be necessary to +do so. Operations such as - or / where the result will always be the same +(x - x will always be 0; x / x will always be 1) are offenses, but these +are covered by Lint/NumericOperationWithConstantResult instead.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe as it does not consider side effects when calling methods +and thus can generate false positives, for example:

    +
    +
    +
    +
    if wr.take_char == '\0' && wr.take_char == '\0'
    +  # ...
    +end
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x.top >= x.top
    +
    +if a.x != 0 && a.x != 0
    +  do_something
    +end
    +
    +def child?
    +  left_child || left_child
    +end
    +
    +# good
    +x + x
    +1 << 1
    +
    +
    +
    +
    +
    +
    +

    Lint/BooleanSymbol

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.50

    1.22

    +
    +

    Checks for :true and :false symbols. +In most cases it would be a typo.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe for this cop because code relying +on :true or :false symbols will break when those are +changed to actual booleans.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +:true
    +
    +# good
    +true
    +
    +# bad
    +:false
    +
    +# good
    +false
    +
    +
    +
    +
    +
    +
    +

    Lint/CircularArgumentReference

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.33

    -

    +
    +

    Checks for circular argument references in optional keyword +arguments and optional ordinal arguments.

    +
    +
    +

    This cop mirrors a warning produced by MRI since 2.2.

    +
    +
    + + + + + +
    + + +This syntax is no longer valid on Ruby 2.7 or higher. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def bake(pie: pie)
    +  pie.heat_up
    +end
    +
    +# good
    +def bake(pie:)
    +  pie.refrigerate
    +end
    +
    +# good
    +def bake(pie: self.pie)
    +  pie.feed_to(user)
    +end
    +
    +# bad
    +def cook(dry_ingredients = dry_ingredients)
    +  dry_ingredients.reduce(&:+)
    +end
    +
    +# good
    +def cook(dry_ingredients = self.dry_ingredients)
    +  dry_ingredients.combine
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/ConstantDefinitionInBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.91

    1.3

    +
    +

    Do not define constants within a block, since the block’s scope does not +isolate or namespace the constant in any way.

    +
    +
    +

    If you are trying to define that constant once, define it outside of +the block instead, or use a variable or method if defining the constant +in the outer scope would be problematic.

    +
    +
    +

    For meta-programming, use const_set.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +task :lint do
    +  FILES_TO_LINT = Dir['lib/*.rb']
    +end
    +
    +# bad
    +describe 'making a request' do
    +  class TestRequest; end
    +end
    +
    +# bad
    +module M
    +  extend ActiveSupport::Concern
    +  included do
    +    LIST = []
    +  end
    +end
    +
    +# good
    +task :lint do
    +  files_to_lint = Dir['lib/*.rb']
    +end
    +
    +# good
    +describe 'making a request' do
    +  let(:test_request) { Class.new }
    +  # see also `stub_const` for RSpec
    +end
    +
    +# good
    +module M
    +  extend ActiveSupport::Concern
    +  included do
    +    const_set(:LIST, [])
    +  end
    +end
    +
    +
    +
    +

    AllowedMethods: ['enums'] (default)

    +
    +
    +
    # good
    +
    +# `enums` for Typed Enums via `T::Enum` in Sorbet.
    +# https://sorbet.org/docs/tenum
    +class TestEnum < T::Enum
    +  enums do
    +    Foo = new("foo")
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    enums

    Array

    +
    + +
    +
    +
    +

    Lint/ConstantOverwrittenInRescue

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.31

    -

    +
    +

    Checks for overwriting an exception with an exception result by use rescue ⇒.

    +
    +
    +

    You intended to write as rescue StandardError. +However, you have written rescue ⇒ StandardError. +In that case, the result of rescue will overwrite StandardError.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  something
    +rescue => StandardError
    +end
    +
    +# good
    +begin
    +  something
    +rescue StandardError
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/ConstantResolution

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.86

    -

    +
    +

    Check that certain constants are fully qualified.

    +
    +
    +

    This is not enabled by default because it would mark a lot of offenses +unnecessarily.

    +
    +
    +

    Generally, gems should fully qualify all constants to avoid conflicts with +the code that uses the gem. Enable this cop without using Only/Ignore

    +
    +
    +

    Large projects will over time end up with one or two constant names that +are problematic because of a conflict with a library or just internally +using the same name a namespace and a class. To avoid too many unnecessary +offenses, Enable this cop with Only: [The, Constant, Names, Causing, Issues]

    +
    +
    + + + + + +
    + + +Style/RedundantConstantBase cop is disabled if this cop is enabled to prevent +conflicting rules. Because it respects user configurations that want to enable +this cop which is disabled by default. +
    +
    +
    +

    Examples

    +
    +
    +
    # By default checks every constant
    +
    +# bad
    +User
    +
    +# bad
    +User::Login
    +
    +# good
    +::User
    +
    +# good
    +::User::Login
    +
    +
    +
    +

    Only: ['Login']

    +
    +
    +
    # Restrict this cop to only being concerned about certain constants
    +
    +# bad
    +Login
    +
    +# good
    +::Login
    +
    +# good
    +User::Login
    +
    +
    +
    +
    +

    Ignore: ['Login']

    +
    +
    +
    # Restrict this cop not being concerned about certain constants
    +
    +# bad
    +User
    +
    +# good
    +::User::Login
    +
    +# good
    +Login
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Only

    []

    Array

    Ignore

    []

    Array

    +
    +
    +
    +
    +

    Lint/Debugger

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.14

    1.63

    +
    +

    Checks for debug calls (such as debugger or binding.pry) that should +not be kept for production code.

    +
    +
    +

    The cop can be configured using DebuggerMethods. By default, a number of gems +debug entrypoints are configured (Kernel, Byebug, Capybara, debug.rb, +Pry, Rails, RubyJard, and WebConsole). Additional methods can be added.

    +
    +
    +

    Specific default groups can be disabled if necessary:

    +
    +
    +
    +
    Lint/Debugger:
    +  DebuggerMethods:
    +    WebConsole: ~
    +
    +
    +
    +

    You can also add your own methods by adding a new category:

    +
    +
    +
    +
    Lint/Debugger:
    +  DebuggerMethods:
    +    MyDebugger:
    +      MyDebugger.debug_this
    +
    +
    +
    +

    Some gems also ship files that will start a debugging session when required, +for example require 'debug/start' from ruby/debug. These requires can +be configured through DebuggerRequires. It has the same structure as +DebuggerMethods, which you can read about above.

    +
    +
    +

    Examples

    +
    +
    +
    # bad (ok during development)
    +
    +# using pry
    +def some_method
    +  binding.pry
    +  do_something
    +end
    +
    +# bad (ok during development)
    +
    +# using byebug
    +def some_method
    +  byebug
    +  do_something
    +end
    +
    +# good
    +
    +def some_method
    +  do_something
    +end
    +
    +
    +
    +

    DebuggerMethods: [my_debugger]

    +
    +
    +
    # bad (ok during development)
    +
    +def some_method
    +  my_debugger
    +end
    +
    +
    +
    +
    +

    DebuggerRequires: [my_debugger/start]

    +
    +
    +
    # bad (ok during development)
    +
    +require 'my_debugger/start'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    DebuggerMethods

    {"Kernel"⇒["binding.irb", "Kernel.binding.irb"], "Byebug"⇒["byebug", "remote_byebug", "Kernel.byebug", "Kernel.remote_byebug"], "Capybara"⇒["page.save_and_open_page", "page.save_and_open_screenshot", "page.save_page", "page.save_screenshot", "save_and_open_page", "save_and_open_screenshot", "save_page", "save_screenshot"], "debug.rb"⇒["binding.b", "binding.break", "Kernel.binding.b", "Kernel.binding.break"], "Pry"⇒["binding.pry", "binding.remote_pry", "binding.pry_remote", "Kernel.binding.pry", "Kernel.binding.remote_pry", "Kernel.binding.pry_remote", "Pry.rescue", "pry"], "Rails"⇒["debugger", "Kernel.debugger"], "RubyJard"⇒["jard"], "WebConsole"⇒["binding.console"]}

    DebuggerRequires

    {"debug.rb"⇒["debug/open", "debug/start"]}

    +
    +
    +
    +
    +

    Lint/DeprecatedClassMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    -

    +
    +

    Checks for uses of the deprecated class method usages.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +File.exists?(some_path)
    +Dir.exists?(some_path)
    +iterator?
    +attr :name, true
    +attr :name, false
    +ENV.freeze # Calling `Env.freeze` raises `TypeError` since Ruby 2.7.
    +ENV.clone
    +ENV.dup # Calling `Env.dup` raises `TypeError` since Ruby 3.1.
    +Socket.gethostbyname(host)
    +Socket.gethostbyaddr(host)
    +
    +# good
    +File.exist?(some_path)
    +Dir.exist?(some_path)
    +block_given?
    +attr_accessor :name
    +attr_reader :name
    +ENV # `ENV.freeze` cannot prohibit changes to environment variables.
    +ENV.to_h
    +ENV.to_h # `ENV.dup` cannot dup `ENV`, use `ENV.to_h` to get a copy of `ENV` as a hash.
    +Addrinfo.getaddrinfo(nodename, service)
    +Addrinfo.tcp(host, port).getnameinfo
    +
    +
    +
    +
    +
    +
    +

    Lint/DeprecatedConstants

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.8

    1.40

    +
    +

    Checks for deprecated constants.

    +
    +
    +

    It has DeprecatedConstants config. If there is an alternative method, you can set +alternative value as Alternative. And you can set the deprecated version as +DeprecatedVersion. These options can be omitted if they are not needed.

    +
    +
    +
    +
    DeprecatedConstants:
    +  'DEPRECATED_CONSTANT':
    +    Alternative: 'alternative_value'
    +    DeprecatedVersion: 'deprecated_version'
    +
    +
    +
    +

    By default, NIL, TRUE, FALSE, Net::HTTPServerException, `Random::DEFAULT, +Struct::Group, and Struct::Passwd are configured.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +NIL
    +TRUE
    +FALSE
    +Net::HTTPServerException
    +Random::DEFAULT # Return value of Ruby 2 is `Random` instance, Ruby 3.0 is `Random` class.
    +Struct::Group
    +Struct::Passwd
    +
    +# good
    +nil
    +true
    +false
    +Net::HTTPClientException
    +Random.new # `::DEFAULT` has been deprecated in Ruby 3, `.new` is compatible with Ruby 2.
    +Etc::Group
    +Etc::Passwd
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    DeprecatedConstants

    {"NIL"⇒{"Alternative"⇒"nil", "DeprecatedVersion"⇒"2.4"}, "TRUE"⇒{"Alternative"⇒"true", "DeprecatedVersion"⇒"2.4"}, "FALSE"⇒{"Alternative"⇒"false", "DeprecatedVersion"⇒"2.4"}, "Net::HTTPServerException"⇒{"Alternative"⇒"Net::HTTPClientException", "DeprecatedVersion"⇒"2.6"}, "Random::DEFAULT"⇒{"Alternative"⇒"Random.new", "DeprecatedVersion"⇒"3.0"}, "Struct::Group"⇒{"Alternative"⇒"Etc::Group", "DeprecatedVersion"⇒"3.0"}, "Struct::Passwd"⇒{"Alternative"⇒"Etc::Passwd", "DeprecatedVersion"⇒"3.0"}}

    +
    +
    +
    +
    +

    Lint/DeprecatedOpenSSLConstant

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.84

    -

    +
    +

    Algorithmic constants for OpenSSL::Cipher and OpenSSL::Digest +deprecated since OpenSSL version 2.2.0. Prefer passing a string +instead.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +OpenSSL::Cipher::AES.new(128, :GCM)
    +
    +# good
    +OpenSSL::Cipher.new('aes-128-gcm')
    +
    +# bad
    +OpenSSL::Digest::SHA256.new
    +
    +# good
    +OpenSSL::Digest.new('SHA256')
    +
    +# bad
    +OpenSSL::Digest::SHA256.digest('foo')
    +
    +# good
    +OpenSSL::Digest.digest('SHA256', 'foo')
    +
    +
    +
    +
    +
    +
    +

    Lint/DisjunctiveAssignmentInConstructor

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.62

    0.88

    +
    +

    Checks constructors for disjunctive assignments (||=) that should +be plain assignments.

    +
    +
    +

    So far, this cop is only concerned with disjunctive assignment of +instance variables.

    +
    +
    +

    In ruby, an instance variable is nil until a value is assigned, so the +disjunction is unnecessary. A plain assignment has the same effect.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it can register a false positive when a +method is redefined in a subclass that calls super. For example:

    +
    +
    +
    +
    class Base
    +  def initialize
    +    @config ||= 'base'
    +  end
    +end
    +
    +class Derived < Base
    +  def initialize
    +    @config = 'derived'
    +    super
    +  end
    +end
    +
    +
    +
    +

    Without the disjunctive assignment, Derived will be unable to override +the value for @config.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def initialize
    +  @x ||= 1
    +end
    +
    +# good
    +def initialize
    +  @x = 1
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateBranch

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.3

    1.7

    +
    +

    Checks that there are no repeated bodies +within if/unless, case-when, case-in and rescue constructs.

    +
    +
    +

    With IgnoreLiteralBranches: true, branches are not registered +as offenses if they return a basic literal value (string, symbol, +integer, float, rational, complex, true, false, or nil), or +return an array, hash, regexp or range that only contains one of +the above basic literal values.

    +
    +
    +

    With IgnoreConstantBranches: true, branches are not registered +as offenses if they return a constant value.

    +
    +
    +

    With IgnoreDuplicateElseBranch: true, in conditionals with multiple branches, +duplicate 'else' branches are not registered as offenses.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if foo
    +  do_foo
    +  do_something_else
    +elsif bar
    +  do_foo
    +  do_something_else
    +end
    +
    +# good
    +if foo || bar
    +  do_foo
    +  do_something_else
    +end
    +
    +# bad
    +case x
    +when foo
    +  do_foo
    +when bar
    +  do_foo
    +else
    +  do_something_else
    +end
    +
    +# good
    +case x
    +when foo, bar
    +  do_foo
    +else
    +  do_something_else
    +end
    +
    +# bad
    +begin
    +  do_something
    +rescue FooError
    +  handle_error
    +rescue BarError
    +  handle_error
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue FooError, BarError
    +  handle_error
    +end
    +
    +
    +
    +

    IgnoreLiteralBranches: true

    +
    +
    +
    # good
    +case size
    +when "small" then 100
    +when "medium" then 250
    +when "large" then 1000
    +else 250
    +end
    +
    +
    +
    +
    +

    IgnoreConstantBranches: true

    +
    +
    +
    # good
    +case size
    +when "small" then SMALL_SIZE
    +when "medium" then MEDIUM_SIZE
    +when "large" then LARGE_SIZE
    +else MEDIUM_SIZE
    +end
    +
    +
    +
    +
    +

    IgnoreDuplicateElseBranch: true

    +
    +
    +
    # good
    +if foo
    +  do_foo
    +elsif bar
    +  do_bar
    +else
    +  do_foo
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IgnoreLiteralBranches

    false

    Boolean

    IgnoreConstantBranches

    false

    Boolean

    IgnoreDuplicateElseBranch

    false

    Boolean

    +
    +
    +
    +
    +

    Lint/DuplicateCaseCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.45

    -

    +
    +

    Checks that there are no repeated conditions +used in case 'when' expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case x
    +when 'first'
    +  do_something
    +when 'first'
    +  do_something_else
    +end
    +
    +# good
    +case x
    +when 'first'
    +  do_something
    +when 'second'
    +  do_something_else
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateElsifCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.88

    -

    +
    +

    Checks that there are no repeated conditions used in if 'elsif'.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if x == 1
    +  do_something
    +elsif x == 1
    +  do_something_else
    +end
    +
    +# good
    +if x == 1
    +  do_something
    +elsif x == 2
    +  do_something_else
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateHashKey

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.34

    0.77

    +
    +

    Checks for duplicated keys in hash literals. +This cop considers both primitive types and constants for the hash keys.

    +
    +
    +

    This cop mirrors a warning in Ruby 2.2.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +hash = { food: 'apple', food: 'orange' }
    +
    +# good
    +hash = { food: 'apple', other_food: 'orange' }
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateMagicComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.37

    -

    +
    +

    Checks for duplicated magic comments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# encoding: ascii
    +# encoding: ascii
    +
    +# good
    +
    +# encoding: ascii
    +
    +# bad
    +
    +# frozen_string_literal: true
    +# frozen_string_literal: true
    +
    +# good
    +
    +# frozen_string_literal: true
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateMatchPattern

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.50

    -

    +
    +

    Checks that there are no repeated patterns used in in keywords.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case x
    +in 'first'
    +  do_something
    +in 'first'
    +  do_something_else
    +end
    +
    +# good
    +case x
    +in 'first'
    +  do_something
    +in 'second'
    +  do_something_else
    +end
    +
    +# bad - repeated alternate patterns with the same conditions don't depend on the order
    +case x
    +in foo | bar
    +  first_method
    +in bar | foo
    +  second_method
    +end
    +
    +# good
    +case x
    +in foo | bar
    +  first_method
    +in bar | baz
    +  second_method
    +end
    +
    +# bad - repeated hash patterns with the same conditions don't depend on the order
    +case x
    +in foo: a, bar: b
    +  first_method
    +in bar: b, foo: a
    +  second_method
    +end
    +
    +# good
    +case x
    +in foo: a, bar: b
    +  first_method
    +in bar: b, baz: c
    +  second_method
    +end
    +
    +# bad - repeated array patterns with elements in the same order
    +case x
    +in [foo, bar]
    +  first_method
    +in [foo, bar]
    +  second_method
    +end
    +
    +# good
    +case x
    +in [foo, bar]
    +  first_method
    +in [bar, foo]
    +  second_method
    +end
    +
    +# bad - repeated the same patterns and guard conditions
    +case x
    +in foo if bar
    +  first_method
    +in foo if bar
    +  second_method
    +end
    +
    +# good
    +case x
    +in foo if bar
    +  first_method
    +in foo if baz
    +  second_method
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.29

    -

    +
    +

    Checks for duplicated instance (or singleton) method +definitions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo
    +  1
    +end
    +
    +def foo
    +  2
    +end
    +
    +# bad
    +def foo
    +  1
    +end
    +
    +alias foo bar
    +
    +# good
    +def foo
    +  1
    +end
    +
    +def bar
    +  2
    +end
    +
    +# good
    +def foo
    +  1
    +end
    +
    +alias bar foo
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateRegexpCharacterClassElement

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.1

    -

    +
    +

    Checks for duplicate elements in Regexp character classes.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +r = /[xyx]/
    +
    +# bad
    +r = /[0-9x0-9]/
    +
    +# good
    +r = /[xy]/
    +
    +# good
    +r = /[0-9x]/
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateRequire

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.90

    1.28

    +
    +

    Checks for duplicate requires and require_relatives.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because it may break the dependency order +of require.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +require 'foo'
    +require 'bar'
    +require 'foo'
    +
    +# good
    +require 'foo'
    +require 'bar'
    +
    +# good
    +require 'foo'
    +require_relative 'foo'
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateRescueException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.89

    -

    +
    +

    Checks that there are no repeated exceptions +used in 'rescue' expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  something
    +rescue FirstException
    +  handle_exception
    +rescue FirstException
    +  handle_other_exception
    +end
    +
    +# good
    +begin
    +  something
    +rescue FirstException
    +  handle_exception
    +rescue SecondException
    +  handle_other_exception
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/DuplicateSetElement

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.67

    -

    +
    +

    Checks for duplicate literal, constant, or variable elements in Set.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Set[:foo, :bar, :foo]
    +
    +# good
    +Set[:foo, :bar]
    +
    +# bad
    +Set.new([:foo, :bar, :foo])
    +
    +# good
    +Set.new([:foo, :bar])
    +
    +# bad
    +[:foo, :bar, :foo].to_set
    +
    +# good
    +[:foo, :bar].to_set
    +
    +
    +
    +
    +
    +
    +

    Lint/EachWithObjectArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.31

    -

    +
    +

    Checks if each_with_object is called with an immutable +argument. Since the argument is the object that the given block shall +make calls on to build something based on the enumerable that +each_with_object iterates over, an immutable argument makes no sense. +It’s definitely a bug.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +sum = numbers.each_with_object(0) { |e, a| a += e }
    +
    +# good
    +num = 0
    +sum = numbers.each_with_object(num) { |e, a| a += e }
    +
    +
    +
    +
    +
    +
    +

    Lint/ElseLayout

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.17

    1.2

    +
    +

    Checks for odd else block layout - like +having an expression on the same line as the else keyword, +which is usually a mistake.

    +
    +
    +

    Its autocorrection tweaks layout to keep the syntax. So, this autocorrection +is compatible correction for bad case syntax, but if your code makes a mistake +with elsif and else, you will have to correct it manually.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +if something
    +  # ...
    +else do_this
    +  do_that
    +end
    +
    +# good
    +
    +# This code is compatible with the bad case. It will be autocorrected like this.
    +if something
    +  # ...
    +else
    +  do_this
    +  do_that
    +end
    +
    +# This code is incompatible with the bad case.
    +# If `do_this` is a condition, `elsif` should be used instead of `else`.
    +if something
    +  # ...
    +elsif do_this
    +  do_that
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/EmptyBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.1

    1.15

    +
    +

    Checks for blocks without a body. +Such empty blocks are typically an oversight or we should provide a comment +to clarify what we’re aiming for.

    +
    +
    +

    Empty lambdas and procs are ignored by default.

    +
    +
    + + + + + +
    + + +For backwards compatibility, the configuration that allows/disallows +empty lambdas and procs is called AllowEmptyLambdas, even though it also +applies to procs. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +items.each { |item| }
    +
    +# good
    +items.each { |item| puts item }
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +items.each do |item|
    +  # TODO: implement later (inner comment)
    +end
    +
    +items.each { |item| } # TODO: implement later (inline comment)
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +items.each do |item|
    +  # TODO: implement later (inner comment)
    +end
    +
    +items.each { |item| } # TODO: implement later (inline comment)
    +
    +
    +
    +
    +

    AllowEmptyLambdas: true (default)

    +
    +
    +
    # good
    +allow(subject).to receive(:callable).and_return(-> {})
    +
    +placeholder = lambda do
    +end
    +(callable || placeholder).call
    +
    +proc { }
    +
    +Proc.new { }
    +
    +
    +
    +
    +

    AllowEmptyLambdas: false

    +
    +
    +
    # bad
    +allow(subject).to receive(:callable).and_return(-> {})
    +
    +placeholder = lambda do
    +end
    +(callable || placeholder).call
    +
    +proc { }
    +
    +Proc.new { }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    AllowEmptyLambdas

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/EmptyClass

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.3

    -

    +
    +

    Checks for classes and metaclasses without a body. +Such empty classes and metaclasses are typically an oversight or we should provide a comment +to be clearer what we’re aiming for.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +end
    +
    +class Bar
    +  class << self
    +  end
    +end
    +
    +class << obj
    +end
    +
    +# good
    +class Foo
    +  def do_something
    +    # ... code
    +  end
    +end
    +
    +class Bar
    +  class << self
    +    attr_reader :bar
    +  end
    +end
    +
    +class << obj
    +  attr_reader :bar
    +end
    +
    +
    +
    +

    AllowComments: false (default)

    +
    +
    +
    # bad
    +class Foo
    +  # TODO: implement later
    +end
    +
    +class Bar
    +  class << self
    +    # TODO: implement later
    +  end
    +end
    +
    +class << obj
    +  # TODO: implement later
    +end
    +
    +
    +
    +
    +

    AllowComments: true

    +
    +
    +
    # good
    +class Foo
    +  # TODO: implement later
    +end
    +
    +class Bar
    +  class << self
    +    # TODO: implement later
    +  end
    +end
    +
    +class << obj
    +  # TODO: implement later
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    false

    Boolean

    +
    +
    +
    +
    +

    Lint/EmptyConditionalBody

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only (Unsafe)

    0.89

    1.61

    +
    +

    Checks for the presence of if, elsif and unless branches without a body.

    +
    +
    + + + + + +
    + + +empty else branches are handled by Style/EmptyElse. +
    +
    +
    +

    Safety

    +
    +

    Autocorrection for this cop is not safe. The conditions for empty branches that +the autocorrection removes may have side effects, or the logic in subsequent +branches may change due to the removal of a previous condition.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition
    +end
    +
    +# bad
    +unless condition
    +end
    +
    +# bad
    +if condition
    +  do_something
    +elsif other_condition
    +end
    +
    +# good
    +if condition
    +  do_something
    +end
    +
    +# good
    +unless condition
    +  do_something
    +end
    +
    +# good
    +if condition
    +  do_something
    +elsif other_condition
    +  do_something_else
    +end
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +if condition
    +  do_something
    +elsif other_condition
    +  # noop
    +end
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +if condition
    +  do_something
    +elsif other_condition
    +  # noop
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/EmptyEnsure

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.10

    1.61

    +
    +

    Checks for empty ensure blocks.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  do_something
    +ensure
    +end
    +
    +# bad
    +begin
    +  do_something
    +ensure
    +end
    +
    +# good
    +def some_method
    +  do_something
    +ensure
    +  do_something_else
    +end
    +
    +# good
    +begin
    +  do_something
    +ensure
    +  do_something_else
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/EmptyExpression

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.45

    -

    +
    +

    Checks for the presence of empty expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +foo = ()
    +if ()
    +  bar
    +end
    +
    +
    +
    +
    +
    # good
    +
    +foo = (some_expression)
    +if (some_expression)
    +  bar
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/EmptyFile

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.90

    -

    +
    +

    Enforces that Ruby source files are not empty.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# Empty file
    +
    +# good
    +# File containing non commented source lines
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +# File consisting only of comments
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +# File consisting only of comments
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/EmptyInPattern

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.16

    -

    +
    +

    Checks for the presence of in pattern branches without a body.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case condition
    +in [a]
    +  do_something
    +in [a, b]
    +end
    +
    +# good
    +case condition
    +in [a]
    +  do_something
    +in [a, b]
    +  nil
    +end
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +case condition
    +in [a]
    +  do_something
    +in [a, b]
    +  # noop
    +end
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +case condition
    +in [a]
    +  do_something
    +in [a, b]
    +  # noop
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/EmptyInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.20

    1.61

    +
    +

    Checks for empty interpolation.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"result is #{}"
    +
    +# good
    +"result is #{some_result}"
    +
    +
    +
    +
    +
    +
    +

    Lint/EmptyWhen

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.45

    0.83

    +
    +

    Checks for the presence of when branches without a body.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case foo
    +when bar
    +  do_something
    +when baz
    +end
    +
    +# good
    +case condition
    +when foo
    +  do_something
    +when bar
    +  nil
    +end
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +case condition
    +when foo
    +  do_something
    +when bar
    +  # noop
    +end
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +case condition
    +when foo
    +  do_something
    +when bar
    +  # do nothing
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/EnsureReturn

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    0.83

    +
    +

    Checks for return from an ensure block. +return from an ensure block is a dangerous code smell as it +will take precedence over any exception being raised, +and the exception will be silently thrown away as if it were rescued.

    +
    +
    +

    If you want to rescue some (or all) exceptions, best to do it explicitly

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo
    +  do_something
    +ensure
    +  cleanup
    +  return self
    +end
    +
    +# good
    +def foo
    +  do_something
    +  self
    +ensure
    +  cleanup
    +end
    +
    +# good
    +def foo
    +  begin
    +    do_something
    +  rescue SomeException
    +    # Let's ignore this exception
    +  end
    +  self
    +ensure
    +  cleanup
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/ErbNewArguments

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.6 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.56

    -

    +
    +

    Emulates the following Ruby warnings in Ruby 2.6.

    +
    +
    +
    +
    $ cat example.rb
    +ERB.new('hi', nil, '-', '@output_buffer')
    +$ ruby -rerb example.rb
    +example.rb:1: warning: Passing safe_level with the 2nd argument of ERB.new is
    +deprecated. Do not use it, and specify other arguments as keyword arguments.
    +example.rb:1: warning: Passing trim_mode with the 3rd argument of ERB.new is
    +deprecated. Use keyword argument like ERB.new(str, trim_mode:...) instead.
    +example.rb:1: warning: Passing eoutvar with the 4th argument of ERB.new is
    +deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.
    +
    +
    +
    +

    Now non-keyword arguments other than first one are softly deprecated +and will be removed when Ruby 2.5 becomes EOL. +ERB.new with non-keyword arguments is deprecated since ERB 2.2.0. +Use :trim_mode and :eoutvar keyword arguments to ERB.new. +This cop identifies places where ERB.new(str, trim_mode, eoutvar) can +be replaced by ERB.new(str, :trim_mode: trim_mode, eoutvar: eoutvar).

    +
    +
    +

    Examples

    +
    +
    +
    # Target codes supports Ruby 2.6 and higher only
    +# bad
    +ERB.new(str, nil, '-', '@output_buffer')
    +
    +# good
    +ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
    +
    +# Target codes supports Ruby 2.5 and lower only
    +# good
    +ERB.new(str, nil, '-', '@output_buffer')
    +
    +# Target codes supports Ruby 2.6, 2.5 and lower
    +# bad
    +ERB.new(str, nil, '-', '@output_buffer')
    +
    +# good
    +# Ruby standard library style
    +# https://github.com/ruby/ruby/commit/3406c5d
    +if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
    +  ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
    +else
    +  ERB.new(str, nil, '-', '@output_buffer')
    +end
    +
    +# good
    +# Use `RUBY_VERSION` style
    +if RUBY_VERSION >= '2.6'
    +  ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
    +else
    +  ERB.new(str, nil, '-', '@output_buffer')
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/FlipFlop

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.16

    -

    +
    +

    Looks for uses of flip-flop operator +based on the Ruby Style Guide.

    +
    +
    +

    Here is the history of flip-flops in Ruby. +flip-flop operator is deprecated in Ruby 2.6.0 and +the deprecation has been reverted by Ruby 2.7.0 and +backported to Ruby 2.6. +See: https://bugs.ruby-lang.org/issues/5400

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(1..20).each do |x|
    +  puts x if (x == 5) .. (x == 10)
    +end
    +
    +# good
    +(1..20).each do |x|
    +  puts x if (x >= 5) && (x <= 10)
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/FloatComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.89

    -

    +
    +

    Checks for the presence of precise comparison of floating point numbers.

    +
    +
    +

    Floating point values are inherently inaccurate, and comparing them for exact equality +is almost never the desired semantics. Comparison via the ==/!= operators checks +floating-point value representation to be exactly the same, which is very unlikely +if you perform any arithmetic operations involving precision loss.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x == 0.1
    +x != 0.1
    +
    +# good - using BigDecimal
    +x.to_d == 0.1.to_d
    +
    +# good - comparing against zero
    +x == 0.0
    +x != 0.0
    +
    +# good
    +(x - 0.1).abs < Float::EPSILON
    +
    +# good
    +tolerance = 0.0001
    +(x - 0.1).abs < tolerance
    +
    +# good - comparing against nil
    +Float(x, exception: false) == nil
    +
    +# Or some other epsilon based type of comparison:
    +# https://www.embeddeduse.com/2019/08/26/qt-compare-two-floats/
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/FloatOutOfRange

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.36

    -

    +
    +

    Identifies Float literals which are, like, really really really +really really really really really big. Too big. No-one needs Floats +that big. If you need a float that big, something is wrong with you.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +float = 3.0e400
    +
    +# good
    +float = 42.9
    +
    +
    +
    +
    +
    +
    +

    Lint/FormatParameterMismatch

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.33

    -

    +
    +

    This lint sees if there is a mismatch between the number of +expected fields for format/sprintf/#% and what is actually +passed as arguments.

    +
    +
    +

    In addition it checks whether different formats are used in the same +format string. Do not mix numbered, unnumbered, and named formats in +the same format string.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +format('A value: %s and another: %i', a_value)
    +
    +# good
    +format('A value: %s and another: %i', a_value, another)
    +
    +# bad
    +format('Unnumbered format: %s and numbered: %2$s', a_value, another)
    +
    +# good
    +format('Numbered format: %1$s and numbered %2$s', a_value, another)
    +
    +
    +
    +
    +
    +
    +

    Lint/HashCompareByIdentity

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.93

    -

    +
    +

    Prefer using Hash#compare_by_identity rather than using object_id +for hash keys.

    +
    +
    +

    This cop looks for hashes being keyed by objects' object_id, using +one of these methods: key?, has_key?, fetch, [] and []=.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe. Although unlikely, the hash could store both object +ids and other values that need be compared by value, and thus +could be a false positive.

    +
    +
    +

    Furthermore, this cop cannot guarantee that the receiver of one of the +methods (key?, etc.) is actually a hash.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +hash = {}
    +hash[foo.object_id] = :bar
    +hash.key?(baz.object_id)
    +
    +# good
    +hash = {}.compare_by_identity
    +hash[foo] = :bar
    +hash.key?(baz)
    +
    +
    +
    + +
    +
    +
    +

    Lint/HashNewWithKeywordArgumentsAsDefault

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.69

    -

    +
    +

    Checks for the deprecated use of keyword arguments as a default in Hash.new.

    +
    +
    +

    This usage raises a warning in Ruby 3.3 and results in an error in Ruby 3.4. +In Ruby 3.4, keyword arguments will instead be used to change the behavior of a hash. +For example, the capacity option can be passed to create a hash with a certain size +if you know it in advance, for better performance.

    +
    +
    + + + + + +
    + + +The following corner case may result in a false negative when upgrading from Ruby 3.3 +or earlier, but it is intentionally not detected to respect the expected usage in Ruby 3.4. +
    +
    +
    +
    +
    Hash.new(capacity: 42)
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Hash.new(key: :value)
    +
    +# good
    +Hash.new({key: :value})
    +
    +
    +
    +
    +
    +
    +

    Lint/HeredocMethodCallPosition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.68

    -

    +
    +

    Checks for the ordering of a method call where +the receiver of the call is a HEREDOC.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +<<-SQL
    +  bar
    +SQL
    +.strip_indent
    +
    +<<-SQL
    +  bar
    +SQL
    +.strip_indent
    +.trim
    +
    +# good
    +<<~SQL
    +  bar
    +SQL
    +
    +<<~SQL.trim
    +  bar
    +SQL
    +
    +
    +
    + +
    +
    +
    +

    Lint/IdentityComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.91

    -

    +
    +

    Prefer equal? over == when comparing object_id.

    +
    +
    +

    Object#equal? is provided to compare objects for identity, and in contrast +Object#== is provided for the purpose of doing value comparison.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo.object_id == bar.object_id
    +
    +# good
    +foo.equal?(bar)
    +
    +
    +
    + +
    +
    +
    +

    Lint/ImplicitStringConcatenation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    -

    +
    +

    Checks for implicit string concatenation of string literals +which are on the same line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array = ['Item 1' 'Item 2']
    +
    +# good
    +array = ['Item 1Item 2']
    +array = ['Item 1' + 'Item 2']
    +array = [
    +  'Item 1' \
    +  'Item 2'
    +]
    +
    +
    +
    +
    +
    +
    +

    Lint/IncompatibleIoSelectWithFiberScheduler

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.21

    1.24

    +
    +

    Checks for IO.select that is incompatible with Fiber Scheduler since Ruby 3.0.

    +
    +
    +

    When an array of IO objects waiting for an exception (the third argument of IO.select) +is used as an argument, there is no alternative API, so offenses are not registered.

    +
    +
    + + + + + +
    + + +When the method is successful the return value of IO.select is , +and the return value of io.wait_readable and io.wait_writable are self. +They are not autocorrected when assigning a return value because these types are different. +It’s up to user how to handle the return value. +
    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because NoMethodError occurs +if require 'io/wait' is not called.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +IO.select([io], [], [], timeout)
    +
    +# good
    +io.wait_readable(timeout)
    +
    +# bad
    +IO.select([], [io], [], timeout)
    +
    +# good
    +io.wait_writable(timeout)
    +
    +
    +
    +
    +
    +
    +

    Lint/IneffectiveAccessModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.36

    -

    +
    +

    Checks for private or protected access modifiers which are +applied to a singleton method. These access modifiers do not make +singleton methods private/protected. private_class_method can be +used for that.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class C
    +  private
    +
    +  def self.method
    +    puts 'hi'
    +  end
    +end
    +
    +# good
    +class C
    +  def self.method
    +    puts 'hi'
    +  end
    +
    +  private_class_method :method
    +end
    +
    +# good
    +class C
    +  class << self
    +    private
    +
    +    def method
    +      puts 'hi'
    +    end
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/InheritException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.41

    1.26

    +
    +

    Looks for error classes inheriting from Exception. +It is configurable to suggest using either StandardError (default) or +RuntimeError instead.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because rescue that omit +exception class handle StandardError and its subclasses, +but not Exception and its subclasses.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: standard_error (default)

    +
    +
    +
    # bad
    +
    +class C < Exception; end
    +
    +C = Class.new(Exception)
    +
    +# good
    +
    +class C < StandardError; end
    +
    +C = Class.new(StandardError)
    +
    +
    +
    +
    +

    EnforcedStyle: runtime_error

    +
    +
    +
    # bad
    +
    +class C < Exception; end
    +
    +C = Class.new(Exception)
    +
    +# good
    +
    +class C < RuntimeError; end
    +
    +C = Class.new(RuntimeError)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    standard_error

    standard_error, runtime_error

    +
    +
    +
    +
    +

    Lint/InterpolationCheck

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.50

    1.40

    +
    +

    Checks for interpolation in a single quoted string.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because although it always replaces single quotes as +if it were miswritten double quotes, it is not always the case. For example, +'{foo} bar' would be replaced by "{foo} bar", so the replaced code would evaluate +the expression foo.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo = 'something with #{interpolation} inside'
    +
    +# good
    +foo = "something with #{interpolation} inside"
    +
    +
    +
    +
    +
    +
    +

    Lint/ItWithoutArgumentsInBlock

    +
    +
    + + + + + +
    + + +Requires Ruby version ⇐ 3.3 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.59

    -

    +
    +

    Emulates the following Ruby warning in Ruby 3.3.

    +
    +
    +
    +
    $ ruby -e '0.times { it }'
    +-e:1: warning: `it` calls without arguments will refer to the first block param in Ruby 3.4;
    +use it() or self.it
    +
    +
    +
    +

    it calls without arguments will refer to the first block param in Ruby 3.4. +So use it() or self.it to ensure compatibility.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something { it }
    +
    +# good
    +do_something { it() }
    +do_something { self.it }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/LambdaWithoutLiteralBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.8

    -

    +
    +

    Checks uses of lambda without a literal block. +It emulates the following warning in Ruby 3.0:

    +
    +
    +
    +
    $ ruby -vwe 'lambda(&proc {})'
    +ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
    +-e:1: warning: lambda without a literal block is deprecated; use the proc without
    +lambda instead
    +
    +
    +
    +

    This way, proc object is never converted to lambda. +Autocorrection replaces with compatible proc argument.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +lambda(&proc { do_something })
    +lambda(&Proc.new { do_something })
    +
    +# good
    +proc { do_something }
    +Proc.new { do_something }
    +lambda { do_something } # If you use lambda.
    +
    +
    +
    +
    +
    +
    +

    Lint/LiteralAsCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.51

    -

    +
    +

    Checks for literals used as the conditions or as +operands in and/or expressions serving as the conditions of +if/while/until/case-when/case-in.

    +
    +
    + + + + + +
    + + +Literals in case-in condition where the match variable is used in +in are accepted as a pattern matching. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if 20
    +  do_something
    +end
    +
    +# bad
    +if some_var && true
    +  do_something
    +end
    +
    +# good
    +if some_var && some_condition
    +  do_something
    +end
    +
    +# good
    +# When using a boolean value for an infinite loop.
    +while true
    +  break if condition
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/LiteralAssignmentInCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.58

    -

    +
    +

    Checks for literal assignments in the conditions of if, while, and until. +It emulates the following Ruby warning:

    +
    +
    +
    +
    $ ruby -we 'if x = true; end'
    +-e:1: warning: found `= literal' in conditional, should be ==
    +
    +
    +
    +

    As a lint cop, it cannot be determined if == is appropriate as intended, +therefore this cop does not provide autocorrection.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if x = 42
    +  do_something
    +end
    +
    +# good
    +if x == 42
    +  do_something
    +end
    +
    +# good
    +if x = y
    +  do_something
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/LiteralInInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    0.32

    +
    +

    Checks for interpolated literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"result is #{10}"
    +
    +# good
    +"result is 10"
    +
    +
    +
    +
    +
    +
    +

    Lint/Loop

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.9

    1.3

    +
    +

    Checks for uses of begin…​end while/until something.

    +
    +
    +

    Safety

    +
    +

    The cop is unsafe because behavior can change in some cases, including +if a local variable inside the loop body is accessed outside of it, or if the +loop body raises a StopIteration exception (which Kernel#loop rescues).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# using while
    +begin
    +  do_something
    +end while some_condition
    +
    +# good
    +
    +# while replacement
    +loop do
    +  do_something
    +  break unless some_condition
    +end
    +
    +# bad
    +
    +# using until
    +begin
    +  do_something
    +end until some_condition
    +
    +# good
    +
    +# until replacement
    +loop do
    +  do_something
    +  break if some_condition
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/MissingCopEnableDirective

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.52

    -

    +
    +

    Checks that there is an # rubocop:enable …​ statement +after a # rubocop:disable …​ statement. This will prevent leaving +cop disables on wide ranges of code, that latter contributors to +a file wouldn’t be aware of.

    +
    +
    +

    Examples

    +
    +
    +
    # Lint/MissingCopEnableDirective:
    +#   MaximumRangeSize: .inf
    +
    +# good
    +# rubocop:disable Layout/SpaceAroundOperators
    +x= 0
    +# rubocop:enable Layout/SpaceAroundOperators
    +# y = 1
    +# EOF
    +
    +# bad
    +# rubocop:disable Layout/SpaceAroundOperators
    +x= 0
    +# EOF
    +
    +
    +
    +
    +
    # Lint/MissingCopEnableDirective:
    +#   MaximumRangeSize: 2
    +
    +# good
    +# rubocop:disable Layout/SpaceAroundOperators
    +x= 0
    +# With the previous, there are 2 lines on which cop is disabled.
    +# rubocop:enable Layout/SpaceAroundOperators
    +
    +# bad
    +# rubocop:disable Layout/SpaceAroundOperators
    +x= 0
    +x += 1
    +# Including this, that's 3 lines on which the cop is disabled.
    +# rubocop:enable Layout/SpaceAroundOperators
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MaximumRangeSize

    Infinity

    Float

    +
    +
    +
    +
    +

    Lint/MissingSuper

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.89

    1.4

    +
    +

    Checks for the presence of constructors and lifecycle callbacks +without calls to super.

    +
    +
    +

    This cop does not consider method_missing (and respond_to_missing?) +because in some cases it makes sense to overtake what is considered a +missing method. In other cases, the theoretical ideal handling could be +challenging or verbose for no actual gain.

    +
    +
    +

    Autocorrection is not supported because the position of super cannot be +determined automatically.

    +
    +
    +

    Object and BasicObject are allowed by this cop because of their +stateless nature. However, sometimes you might want to allow other parent +classes from this cop, for example in the case of an abstract class that is +not meant to be called with super. In those cases, you can use the +AllowedParentClasses option to specify which classes should be allowed +in addition to Object and BasicObject.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Employee < Person
    +  def initialize(name, salary)
    +    @salary = salary
    +  end
    +end
    +
    +# good
    +class Employee < Person
    +  def initialize(name, salary)
    +    super(name)
    +    @salary = salary
    +  end
    +end
    +
    +# bad
    +Employee = Class.new(Person) do
    +  def initialize(name, salary)
    +    @salary = salary
    +  end
    +end
    +
    +# good
    +Employee = Class.new(Person) do
    +  def initialize(name, salary)
    +    super(name)
    +    @salary = salary
    +  end
    +end
    +
    +# bad
    +class Parent
    +  def self.inherited(base)
    +    do_something
    +  end
    +end
    +
    +# good
    +class Parent
    +  def self.inherited(base)
    +    super
    +    do_something
    +  end
    +end
    +
    +# good
    +class ClassWithNoParent
    +  def initialize
    +    do_something
    +  end
    +end
    +
    +
    +
    +

    AllowedParentClasses: [MyAbstractClass]

    +
    +
    +
    # good
    +class MyConcreteClass < MyAbstractClass
    +  def initialize
    +    do_something
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedParentClasses

    []

    Array

    +
    +
    +
    +
    +

    Lint/MixedCaseRange

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.53

    -

    +
    +

    Checks for mixed-case character ranges since they include likely unintended characters.

    +
    +
    +

    Offenses are registered for regexp character classes like /[A-z]/ +as well as range objects like ('A'..'z').

    +
    +
    + + + + + +
    + + +Range objects cannot be autocorrected. +
    +
    +
    +

    Safety

    +
    +

    The cop autocorrects regexp character classes +by replacing one character range with two: A-z becomes A-Za-z. +In most cases this is probably what was originally intended +but it changes the regexp to no longer match symbols it used to include. +For this reason, this cop’s autocorrect is unsafe (it will +change the behavior of the code).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +r = /[A-z]/
    +
    +# good
    +r = /[A-Za-z]/
    +
    +
    +
    +
    +
    +
    +

    Lint/MixedRegexpCaptureTypes

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.85

    -

    +
    +

    Do not mix named captures and numbered captures in a Regexp literal +because numbered capture is ignored if they’re mixed. +Replace numbered captures with non-capturing groupings or +named captures.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +/(?<foo>FOO)(BAR)/
    +
    +# good
    +/(?<foo>FOO)(?<bar>BAR)/
    +
    +# good
    +/(?<foo>FOO)(?:BAR)/
    +
    +# good
    +/(FOO)(BAR)/
    +
    +
    +
    +
    +
    +
    +

    Lint/MultipleComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.47

    1.1

    +
    +

    In math and Python, we can use x < y < z style comparison to compare +multiple value. However, we can’t use the comparison in Ruby. However, +the comparison is not syntax error. This cop checks the bad usage of +comparison operators.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x < y < z
    +10 <= x <= 20
    +
    +# good
    +x < y && y < z
    +10 <= x && x <= 20
    +
    +
    +
    +
    +
    +
    +

    Lint/NestedMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.32

    -

    +
    +

    Checks for nested method definitions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# `bar` definition actually produces methods in the same scope
    +# as the outer `foo` method. Furthermore, the `bar` method
    +# will be redefined every time `foo` is invoked.
    +def foo
    +  def bar
    +  end
    +end
    +
    +# good
    +
    +def foo
    +  bar = -> { puts 'hello' }
    +  bar.call
    +end
    +
    +# good
    +
    +# `class_eval`, `instance_eval`, `module_eval`, `class_exec`, `instance_exec`, and
    +# `module_exec` blocks are allowed by default.
    +
    +def foo
    +  self.class.class_eval do
    +    def bar
    +    end
    +  end
    +end
    +
    +def foo
    +  self.class.module_exec do
    +    def bar
    +    end
    +  end
    +end
    +
    +# good
    +
    +def foo
    +  class << self
    +    def bar
    +    end
    +  end
    +end
    +
    +
    +
    +

    AllowedMethods: [] (default)

    +
    +
    +
    # bad
    +def do_something
    +  has_many :articles do
    +    def find_or_create_by_name(name)
    +    end
    +  end
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['has_many']

    +
    +
    +
    # bad
    +def do_something
    +  has_many :articles do
    +    def find_or_create_by_name(name)
    +    end
    +  end
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +def foo(obj)
    +  obj.do_baz do
    +    def bar
    +    end
    +  end
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: ['baz']

    +
    +
    +
    # good
    +def foo(obj)
    +  obj.do_baz do
    +    def bar
    +    end
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/NestedPercentLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.52

    -

    +
    +

    Checks for nested percent literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# The percent literal for nested_attributes is parsed as four tokens,
    +# yielding the array [:name, :content, :"%i[incorrectly", :"nested]"].
    +attributes = {
    +  valid_attributes: %i[name content],
    +  nested_attributes: %i[name content %i[incorrectly nested]]
    +}
    +
    +# good
    +
    +# Neither is incompatible with the bad case, but probably the intended code.
    +attributes = {
    +  valid_attributes: %i[name content],
    +  nested_attributes: [:name, :content, %i[incorrectly nested]]
    +}
    +
    +attributes = {
    +  valid_attributes: %i[name content],
    +  nested_attributes: [:name, :content, [:incorrectly, :nested]]
    +}
    +
    +
    +
    +
    +
    +
    +

    Lint/NextWithoutAccumulator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.36

    -

    +
    +

    Don’t omit the accumulator when calling next in a reduce block.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +result = (1..4).reduce(0) do |acc, i|
    +  next if i.odd?
    +  acc + i
    +end
    +
    +# good
    +result = (1..4).reduce(0) do |acc, i|
    +  next acc if i.odd?
    +  acc + i
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/NoReturnInBeginEndBlocks

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.2

    -

    +
    +

    Checks for the presence of a return inside a begin..end block +in assignment contexts. +In this situation, the return will result in an exit from the current +method, possibly leading to unexpected behavior.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +@some_variable ||= begin
    +  return some_value if some_condition_is_met
    +
    +  do_something
    +end
    +
    +# good
    +@some_variable ||= begin
    +  if some_condition_is_met
    +    some_value
    +  else
    +    do_something
    +  end
    +end
    +
    +# good
    +some_variable = if some_condition_is_met
    +                  return if another_condition_is_met
    +
    +                  some_value
    +                else
    +                  do_something
    +                end
    +
    +
    +
    +
    +
    +
    +

    Lint/NonAtomicFileOperation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.31

    -

    +
    +

    Checks for non-atomic file operation. +And then replace it with a nearly equivalent and atomic method.

    +
    +
    +

    These can cause problems that are difficult to reproduce, +especially in cases of frequent file operations in parallel, +such as test runs with parallel_rspec.

    +
    +
    +

    For examples: creating a directory if there is none, has the following problems

    +
    +
    +

    An exception occurs when the directory didn’t exist at the time of exist?, +but someone else created it before mkdir was executed.

    +
    +
    +

    Subsequent processes are executed without the directory that should be there +when the directory existed at the time of exist?, +but someone else deleted it shortly afterwards.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because autocorrection change to atomic processing. +The atomic processing of the replacement destination is not guaranteed +to be strictly equivalent to that before the replacement.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad - race condition with another process may result in an error in `mkdir`
    +unless Dir.exist?(path)
    +  FileUtils.mkdir(path)
    +end
    +
    +# good - atomic and idempotent creation
    +FileUtils.mkdir_p(path)
    +
    +# bad - race condition with another process may result in an error in `remove`
    +if File.exist?(path)
    +  FileUtils.remove(path)
    +end
    +
    +# good - atomic and idempotent removal
    +FileUtils.rm_f(path)
    +
    +
    +
    + +
    +
    +
    +

    Lint/NonDeterministicRequireOrder

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.78

    -

    +
    +

    Dir[…​] and Dir.glob(…​) do not make any guarantees about +the order in which files are returned. The final order is +determined by the operating system and file system. +This means that using them in cases where the order matters, +such as requiring files, can lead to intermittent failures +that are hard to debug. To ensure this doesn’t happen, +always sort the list.

    +
    +
    +

    Dir.glob and Dir[] sort globbed results by default in Ruby 3.0. +So all bad cases are acceptable when Ruby 3.0 or higher are used.

    +
    +
    + + + + + +
    + + +This cop will be deprecated and removed when supporting only Ruby 3.0 and higher. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe in the case where sorting files changes existing +expected behavior.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Dir["./lib/**/*.rb"].each do |file|
    +  require file
    +end
    +
    +# good
    +Dir["./lib/**/*.rb"].sort.each do |file|
    +  require file
    +end
    +
    +# bad
    +Dir.glob(Rails.root.join(__dir__, 'test', '*.rb')) do |file|
    +  require file
    +end
    +
    +# good
    +Dir.glob(Rails.root.join(__dir__, 'test', '*.rb')).sort.each do |file|
    +  require file
    +end
    +
    +# bad
    +Dir['./lib/**/*.rb'].each(&method(:require))
    +
    +# good
    +Dir['./lib/**/*.rb'].sort.each(&method(:require))
    +
    +# bad
    +Dir.glob(Rails.root.join('test', '*.rb'), &method(:require))
    +
    +# good
    +Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require))
    +
    +# good - Respect intent if `sort` keyword option is specified in Ruby 3.0 or higher.
    +Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), sort: false).each(&method(:require))
    +
    +
    +
    +
    +
    +
    +

    Lint/NonLocalExitFromIterator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.30

    -

    +
    +

    Checks for non-local exits from iterators without a return +value. It registers an offense under these conditions:

    +
    +
    +
      +
    • +

      No value is returned,

      +
    • +
    • +

      the block is preceded by a method chain,

      +
    • +
    • +

      the block has arguments,

      +
    • +
    • +

      the method which receives the block is not define_method +or define_singleton_method,

      +
    • +
    • +

      the return is not contained in an inner scope, e.g. a lambda or a +method definition.

      +
    • +
    +
    +
    +

    Examples

    +
    +
    +
    class ItemApi
    +  rescue_from ValidationError do |e| # non-iteration block with arg
    +    return { message: 'validation error' } unless e.errors # allowed
    +    error_array = e.errors.map do |error| # block with method chain
    +      return if error.suppress? # warned
    +      return "#{error.param}: invalid" unless error.message # allowed
    +      "#{error.param}: #{error.message}"
    +    end
    +    { message: 'validation error', errors: error_array }
    +  end
    +
    +  def update_items
    +    transaction do # block without arguments
    +      return unless update_necessary? # allowed
    +      find_each do |item| # block without method chain
    +        return if item.stock == 0 # false-negative...
    +        item.update!(foobar: true)
    +      end
    +    end
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/NumberConversion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always (Unsafe)

    0.53

    1.1

    +
    +

    Warns the usage of unsafe number conversions. Unsafe +number conversion can cause unexpected error if auto type conversion +fails. Cop prefer parsing with number class instead.

    +
    +
    +

    Conversion with Integer, Float, etc. will raise an ArgumentError +if given input that is not numeric (eg. an empty string), whereas +to_i, etc. will try to convert regardless of input (''.to_i ⇒ 0). +As such, this cop is disabled by default because it’s not necessarily +always correct to raise if a value is not numeric.

    +
    +
    + + + + + +
    + + +Some values cannot be converted properly using one of the Kernel +method (for instance, Time and DateTime values are allowed by this +cop by default). Similarly, Rails' duration methods do not work well +with Integer() and can be allowed with AllowedMethods. By default, +there are no methods to allowed. +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because it is not guaranteed that the +replacement Kernel methods are able to properly handle the +input if it is not a standard class.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +'10'.to_i
    +'10.2'.to_f
    +'10'.to_c
    +'1/3'.to_r
    +['1', '2', '3'].map(&:to_i)
    +foo.try(:to_f)
    +bar.send(:to_c)
    +
    +# good
    +
    +Integer('10', 10)
    +Float('10.2')
    +Complex('10')
    +Rational('1/3')
    +['1', '2', '3'].map { |i| Integer(i, 10) }
    +foo.try { |i| Float(i) }
    +bar.send { |i| Complex(i) }
    +
    +
    +
    +

    AllowedMethods: [] (default)

    +
    +
    +
    # bad
    +10.minutes.to_i
    +
    +
    +
    +
    +

    AllowedMethods: [minutes]

    +
    +
    +
    # good
    +10.minutes.to_i
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +10.minutes.to_i
    +
    +
    +
    +
    +

    AllowedPatterns: ['min*']

    +
    +
    +
    # good
    +10.minutes.to_i
    +
    +
    +
    +
    +

    IgnoredClasses: [Time, DateTime] (default)

    +
    +
    +
    # good
    +Time.now.to_datetime.to_i
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    IgnoredClasses

    Time, DateTime

    Array

    +
    +
    +
    +
    +

    Lint/NumberedParameterAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.9

    -

    +
    +

    Checks for uses of numbered parameter assignment. +It emulates the following warning in Ruby 2.7:

    +
    +
    +
    +
    $ ruby -ve '_1 = :value'
    +ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
    +-e:1: warning: `_1' is reserved for numbered parameter; consider another name
    +
    +
    +
    +

    Assigning to a numbered parameter (from _1 to _9) causes an error in Ruby 3.0.

    +
    +
    +
    +
    $ ruby -ve '_1 = :value'
    +ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
    +-e:1: _1 is reserved for numbered parameter
    +
    +
    +
    + + + + + +
    + + +The parametered parameters are from _1 to _9. This cop checks _0, and over _10 +as well to prevent confusion. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +_1 = :value
    +
    +# good
    +non_numbered_parameter_name = :value
    +
    +
    +
    +
    +
    +
    +

    Lint/NumericOperationWithConstantResult

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.69

    -

    +
    +

    Certain numeric operations have a constant result, usually 0 or 1. +Subtracting a number from itself or multiplying it by 0 will always return 0. +Additionally, a variable modulo 0 or itself will always return 0. +Dividing a number by itself or raising it to the power of 0 will always return 1. +As such, they can be replaced with that result. +These are probably leftover from debugging, or are mistakes. +Other numeric operations that are similarly leftover from debugging or mistakes +are handled by Lint/UselessNumericOperation.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x - x
    +x * 0
    +x % 1
    +x % x
    +
    +# good
    +0
    +
    +# bad
    +x -= x
    +x *= 0
    +x %= 1
    +x %= x
    +
    +# good
    +x = 0
    +
    +# bad
    +x / x
    +x ** 0
    +
    +# good
    +1
    +
    +# bad
    +x /= x
    +x **= 0
    +
    +# good
    +x = 1
    +
    +
    +
    +
    +
    +
    +

    Lint/OrAssignmentToConstant

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.9

    -

    +
    +

    Checks for unintended or-assignment to a constant.

    +
    +
    +

    Constants should always be assigned in the same location. And its value +should always be the same. If constants are assigned in multiple +locations, the result may vary depending on the order of require.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because code that is already conditionally +assigning a constant may have its behavior changed by autocorrection.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +CONST ||= 1
    +
    +# good
    +CONST = 1
    +
    +
    +
    +
    +
    +
    +

    Lint/OrderedMagicComments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.53

    1.37

    +
    +

    Checks the proper ordering of magic comments and whether +a magic comment is not placed before a shebang.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because file encoding may change.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# frozen_string_literal: true
    +# encoding: ascii
    +p [''.frozen?, ''.encoding] #=> [true, #<Encoding:UTF-8>]
    +
    +# good
    +
    +# encoding: ascii
    +# frozen_string_literal: true
    +p [''.frozen?, ''.encoding] #=> [true, #<Encoding:US-ASCII>]
    +
    +# good
    +
    +#!/usr/bin/env ruby
    +# encoding: ascii
    +# frozen_string_literal: true
    +p [''.frozen?, ''.encoding] #=> [true, #<Encoding:US-ASCII>]
    +
    +
    +
    +
    +
    +
    +

    Lint/OutOfRangeRegexpRef

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.89

    -

    +
    +

    Looks for references of Regexp captures that are out of range +and thus always returns nil.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it is naive in how it determines what +references are available based on the last encountered regexp, but +it cannot handle some cases, such as conditional regexp matches, which +leads to false positives, such as:

    +
    +
    +
    +
    foo ? /(c)(b)/ =~ str : /(b)/ =~ str
    +do_something if $2
    +# $2 is defined for the first condition but not the second, however
    +# the cop will mark this as an offense.
    +
    +
    +
    +

    This might be a good indication of code that should be refactored, +however.

    +
    +
    +
    +

    Examples

    +
    +
    +
    /(foo)bar/ =~ 'foobar'
    +
    +# bad - always returns nil
    +
    +puts $2 # => nil
    +
    +# good
    +
    +puts $1 # => foo
    +
    +
    +
    +
    +
    +
    +

    Lint/ParenthesesAsGroupedExpression

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.12

    0.83

    +
    +

    Checks for space between the name of a called method and a left +parenthesis.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something (foo)
    +
    +# good
    +do_something(foo)
    +do_something (2 + 3) * 4
    +do_something (foo * bar).baz
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/PercentStringArray

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.41

    -

    +
    +

    Checks for quotes and commas in %w, e.g. %w('foo', "bar")

    +
    +
    +

    It is more likely that the additional characters are unintended (for +example, mistranslating an array of literals to percent string notation) +rather than meant to be part of the resulting strings.

    +
    +
    +

    Safety

    +
    +

    The cop is unsafe because the correction changes the values in the array +and that might have been done purposely.

    +
    +
    +
    +
    %w('foo', "bar") #=> ["'foo',", '"bar"']
    +%w(foo bar)      #=> ['foo', 'bar']
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%w('foo', "bar")
    +
    +# good
    +%w(foo bar)
    +
    +
    +
    +
    +
    +
    +

    Lint/PercentSymbolArray

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.41

    -

    +
    +

    Checks for colons and commas in %i, e.g. %i(:foo, :bar)

    +
    +
    +

    It is more likely that the additional characters are unintended (for +example, mistranslating an array of literals to percent string notation) +rather than meant to be part of the resulting symbols.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%i(:foo, :bar)
    +
    +# good
    +%i(foo bar)
    +
    +
    +
    +
    +
    +
    +

    Lint/RaiseException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.81

    0.86

    +
    +

    Checks for raise or fail statements which are +raising Exception class.

    +
    +
    +

    You can specify a module name that will be an implicit namespace +using AllowedImplicitNamespaces option. The cop cause a false positive +for namespaced Exception when a namespace is omitted. This option can +prevent the false positive by specifying a namespace to be omitted for +Exception. Alternatively, make Exception a fully qualified class +name with an explicit namespace.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it will change the exception class being +raised, which is a change in behavior.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +raise Exception, 'Error message here'
    +
    +# good
    +raise StandardError, 'Error message here'
    +
    +
    +
    +

    AllowedImplicitNamespaces: ['Gem']

    +
    +
    +
    # good
    +module Gem
    +  def self.foo
    +    raise Exception # This exception means `Gem::Exception`.
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedImplicitNamespaces

    Gem

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/RandOne

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.36

    -

    +
    +

    Checks for rand(1) calls. +Such calls always return 0.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +rand 1
    +Kernel.rand(-1)
    +rand 1.0
    +rand(-1.0)
    +
    +# good
    +0 # just use 0 instead
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantCopDisableDirective

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    -

    +
    +

    Detects instances of rubocop:disable comments that can be +removed without causing any offenses to be reported. It’s implemented +as a cop in that it inherits from the Cop base class and calls +add_offense. The unusual part of its implementation is that it doesn’t +have any on_* methods or an investigate method. This means that it +doesn’t take part in the investigation phase when the other cops do +their work. Instead, it waits until it’s called in a later stage of the +execution. The reason it can’t be implemented as a normal cop is that +it depends on the results of all other cops to do its work.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# rubocop:disable Layout/LineLength
    +x += 1
    +# rubocop:enable Layout/LineLength
    +
    +# good
    +x += 1
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantCopEnableDirective

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    -

    +
    +

    Detects instances of rubocop:enable comments that can be +removed.

    +
    +
    +

    When comment enables all cops at once rubocop:enable all +that cop checks whether any cop was actually enabled.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo = 1
    +# rubocop:enable Layout/LineLength
    +
    +# good
    +foo = 1
    +
    +# bad
    +# rubocop:disable Style/StringLiterals
    +foo = "1"
    +# rubocop:enable Style/StringLiterals
    +baz
    +# rubocop:enable all
    +
    +# good
    +# rubocop:disable Style/StringLiterals
    +foo = "1"
    +# rubocop:enable all
    +baz
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantDirGlobSort

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.8

    1.26

    +
    +

    Sort globbed results by default in Ruby 3.0. +This cop checks for redundant sort method to Dir.glob and Dir[].

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, in case of having a file and a directory with +identical names, since directory will be loaded before the file, which +will break exe/files.rb that rely on exe.rb file.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Dir.glob('./lib/**/*.rb').sort.each do |file|
    +end
    +
    +Dir['./lib/**/*.rb'].sort.each do |file|
    +end
    +
    +# good
    +Dir.glob('./lib/**/*.rb').each do |file|
    +end
    +
    +Dir['./lib/**/*.rb'].each do |file|
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantRegexpQuantifiers

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.53

    -

    +
    +

    Checks for redundant quantifiers inside Regexp literals.

    +
    +
    +

    It is always allowed when interpolation is used in a regexp literal, +because it’s unknown what kind of string will be expanded as a result:

    +
    +
    +
    +
    /(?:a*#{interpolation})?/x
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +/(?:x+)+/
    +
    +# good
    +/(?:x)+/
    +
    +# good
    +/(?:x+)/
    +
    +# bad
    +/(?:x+)?/
    +
    +# good
    +/(?:x)*/
    +
    +# good
    +/(?:x*)/
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantRequireStatement

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.76

    1.57

    +
    +

    Checks for unnecessary require statement.

    +
    +
    +

    The following features are unnecessary require statement because +they are already loaded. e.g. Ruby 2.2:

    +
    +
    +
    +
    ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
    +ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
    +["enumerator.so", "rational.so", "complex.so", "thread.rb"]
    +
    +
    +
    +

    Below are the features that each TargetRubyVersion targets.

    +
    +
    +
      +
    • +

      2.0+ …​ enumerator

      +
    • +
    • +

      2.1+ …​ thread

      +
    • +
    • +

      2.2+ …​ Add rational and complex above

      +
    • +
    • +

      2.5+ …​ Add pp above

      +
    • +
    • +

      2.7+ …​ Add ruby2_keywords above

      +
    • +
    • +

      3.1+ …​ Add fiber above

      +
    • +
    • +

      3.2+ …​ set

      +
    • +
    +
    +
    +

    This cop target those features.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because if require 'pp' is removed from one file, +NameError can be encountered when another file uses PP.pp.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +require 'unloaded_feature'
    +require 'thread'
    +
    +# good
    +require 'unloaded_feature'
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantSafeNavigation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.93

    -

    +
    +

    Checks for redundant safe navigation calls. +Use cases where a constant, named in camel case for classes and modules is nil are rare, +and an offense is not detected when the receiver is a constant. The detection also applies +to self, and to literal receivers, except for nil.

    +
    +
    +

    For all receivers, the instance_of?, kind_of?, is_a?, eql?, respond_to?, +and equal? methods are checked by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    The AllowedMethods option specifies nil-safe methods, +in other words, it is a method that is allowed to skip safe navigation. +Note that the AllowedMethod option is not an option that specifies methods +for which to suppress (allow) this cop’s check.

    +
    +
    +

    In the example below, the safe navigation operator (&.) is unnecessary +because NilClass has methods like respond_to? and is_a?.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because autocorrection can change the return type of +the expression. An offending expression that previously could return nil +will be autocorrected to never return nil.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +CamelCaseConst&.do_something
    +
    +# good
    +CamelCaseConst.do_something
    +
    +# bad
    +do_something if attrs&.respond_to?(:[])
    +
    +# good
    +do_something if attrs.respond_to?(:[])
    +
    +# bad
    +while node&.is_a?(BeginNode)
    +  node = node.parent
    +end
    +
    +# good
    +while node.is_a?(BeginNode)
    +  node = node.parent
    +end
    +
    +# good - without `&.` this will always return `true`
    +foo&.respond_to?(:to_a)
    +
    +# bad - for `nil`s conversion methods return default values for the type
    +foo&.to_h || {}
    +foo&.to_h { |k, v| [k, v] } || {}
    +foo&.to_a || []
    +foo&.to_i || 0
    +foo&.to_f || 0.0
    +foo&.to_s || ''
    +
    +# good
    +foo.to_h
    +foo.to_h { |k, v| [k, v] }
    +foo.to_a
    +foo.to_i
    +foo.to_f
    +foo.to_s
    +
    +# bad
    +self&.foo
    +
    +# good
    +self.foo
    +
    +
    +
    +

    AllowedMethods: [nil_safe_method]

    +
    +
    +
    # bad
    +do_something if attrs&.nil_safe_method(:[])
    +
    +# good
    +do_something if attrs.nil_safe_method(:[])
    +do_something if attrs&.not_nil_safe_method(:[])
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    instance_of?, kind_of?, is_a?, eql?, respond_to?, equal?

    Array

    +
    +
    +
    +
    +

    Lint/RedundantSplatExpansion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    1.7

    +
    +

    Checks for unneeded usages of splat expansion.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a = *[1, 2, 3]
    +a = *'a'
    +a = *1
    +['a', 'b', *%w(c d e), 'f', 'g']
    +
    +# good
    +c = [1, 2, 3]
    +a = *c
    +a, b = *c
    +a, *b = *c
    +a = *1..10
    +a = ['a']
    +['a', 'b', 'c', 'd', 'e', 'f', 'g']
    +
    +# bad
    +do_something(*['foo', 'bar', 'baz'])
    +
    +# good
    +do_something('foo', 'bar', 'baz')
    +
    +# bad
    +begin
    +  foo
    +rescue *[StandardError, ApplicationError]
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue StandardError, ApplicationError
    +  bar
    +end
    +
    +# bad
    +case foo
    +when *[1, 2, 3]
    +  bar
    +else
    +  baz
    +end
    +
    +# good
    +case foo
    +when 1, 2, 3
    +  bar
    +else
    +  baz
    +end
    +
    +
    +
    +

    AllowPercentLiteralArrayArgument: true (default)

    +
    +
    +
    # good
    +do_something(*%w[foo bar baz])
    +
    +
    +
    +
    +

    AllowPercentLiteralArrayArgument: false

    +
    +
    +
    # bad
    +do_something(*%w[foo bar baz])
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowPercentLiteralArrayArgument

    true

    Boolean

    +
    +
    +
    +
    +

    Lint/RedundantStringCoercion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    0.77

    +
    +

    Checks for string conversion in string interpolation, print, puts, and warn arguments, +which is redundant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"result is #{something.to_s}"
    +print something.to_s
    +puts something.to_s
    +warn something.to_s
    +
    +# good
    +"result is #{something}"
    +print something
    +puts something
    +warn something
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/RedundantWithIndex

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Checks for redundant with_index.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +ary.each_with_index do |v|
    +  v
    +end
    +
    +# good
    +ary.each do |v|
    +  v
    +end
    +
    +# bad
    +ary.each.with_index do |v|
    +  v
    +end
    +
    +# good
    +ary.each do |v|
    +  v
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RedundantWithObject

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.51

    -

    +
    +

    Checks for redundant with_object.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +ary.each_with_object([]) do |v|
    +  v
    +end
    +
    +# good
    +ary.each do |v|
    +  v
    +end
    +
    +# bad
    +ary.each.with_object([]) do |v|
    +  v
    +end
    +
    +# good
    +ary.each do |v|
    +  v
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RefinementImportMethods

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.1 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.27

    -

    +
    +

    Checks if include or prepend is called in refine block. +These methods are deprecated and should be replaced with Refinement#import_methods.

    +
    +
    +

    It emulates deprecation warnings in Ruby 3.1.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because include M will affect the included class +if any changes are made to module M. +On the other hand, import_methods M uses a snapshot of method definitions, +thus it will not be affected if module M changes.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +refine Foo do
    +  include Bar
    +end
    +
    +# bad
    +refine Foo do
    +  prepend Bar
    +end
    +
    +# good
    +refine Foo do
    +  import_methods Bar
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RegexpAsCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.51

    0.86

    +
    +

    Checks for regexp literals used as match-current-line. +If a regexp literal is in condition, the regexp matches $_ implicitly.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if /foo/
    +  do_something
    +end
    +
    +# good
    +if /foo/ =~ $_
    +  do_something
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RequireParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.18

    -

    +
    +

    Checks for expressions where there is a call to a predicate +method with at least one argument, where no parentheses are used around +the parameter list, and a boolean operator, && or ||, is used in the +last argument.

    +
    +
    +

    The idea behind warning for these constructs is that the user might +be under the impression that the return value from the method call is +an operand of &&/||.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if day.is? :tuesday && month == :jan
    +  # ...
    +end
    +
    +# good
    +if day.is?(:tuesday) && month == :jan
    +  # ...
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/RequireRangeParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.32

    -

    +
    +

    Checks that a range literal is enclosed in parentheses when the end of the range is +at a line break.

    +
    +
    + + + + + +
    + + +The following is maybe intended for (42..). But, compatible is 42..do_something. +So, this cop does not provide autocorrection because it is left to user. +
    +
    +
    +
    +
    case condition
    +when 42..
    +  do_something
    +end
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad - Represents `(1..42)`, not endless range.
    +1..
    +42
    +
    +# good - It's incompatible, but your intentions when using endless range may be:
    +(1..)
    +42
    +
    +# good
    +1..42
    +
    +# good
    +(1..42)
    +
    +# good
    +(1..
    +42)
    +
    +
    +
    +
    +
    +
    +

    Lint/RequireRelativeSelfPath

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.22

    -

    +
    +

    Checks for uses a file requiring itself with require_relative.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# foo.rb
    +require_relative 'foo'
    +require_relative 'bar'
    +
    +# good
    +
    +# foo.rb
    +require_relative 'bar'
    +
    +
    +
    +
    +
    +
    +

    Lint/RescueException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    0.27

    +
    +

    Checks for rescue blocks targeting the Exception class.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  do_something
    +rescue Exception
    +  handle_exception
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue ArgumentError
    +  handle_exception
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Lint/RescueType

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    -

    +
    +

    Check for arguments to rescue that will result in a TypeError +if an exception is raised.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  bar
    +rescue nil
    +  baz
    +end
    +
    +# bad
    +def foo
    +  bar
    +rescue 1, 'a', "#{b}", 0.0, [], {}
    +  baz
    +end
    +
    +# good
    +begin
    +  bar
    +rescue
    +  baz
    +end
    +
    +# good
    +def foo
    +  bar
    +rescue NameError
    +  baz
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/ReturnInVoidContext

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Checks for the use of a return with a value in a context +where the value will be ignored. (initialize and setter methods)

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def initialize
    +  foo
    +  return :qux if bar?
    +  baz
    +end
    +
    +def foo=(bar)
    +  return 42
    +end
    +
    +# good
    +def initialize
    +  foo
    +  return if bar?
    +  baz
    +end
    +
    +def foo=(bar)
    +  return
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/SafeNavigationChain

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.3 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.47

    0.77

    +
    +

    The safe navigation operator returns nil if the receiver is +nil. If you chain an ordinary method call after a safe +navigation operator, it raises NoMethodError. We should use a +safe navigation operator after a safe navigation operator. +This cop checks for the problem outlined above.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x&.foo.bar
    +x&.foo + bar
    +x&.foo[bar]
    +
    +# good
    +x&.foo&.bar
    +x&.foo || bar
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    present?, blank?, presence, try, try!, in?

    Array

    +
    +
    +
    +
    +

    Lint/SafeNavigationConsistency

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.55

    0.77

    +
    +

    Check to make sure that if safe navigation is used in an && or || condition, +consistent and appropriate safe navigation, without excess or deficiency, +is used for all method calls on the same object.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo&.bar && foo&.baz
    +
    +# good
    +foo&.bar && foo.baz
    +
    +# bad
    +foo.bar && foo&.baz
    +
    +# good
    +foo.bar && foo.baz
    +
    +# bad
    +foo&.bar || foo.baz
    +
    +# good
    +foo&.bar || foo&.baz
    +
    +# bad
    +foo.bar || foo&.baz
    +
    +# good
    +foo.bar || foo.baz
    +
    +# bad
    +foo&.bar && (foobar.baz || foo&.baz)
    +
    +# good
    +foo&.bar && (foobar.baz || foo.baz)
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    present?, blank?, presence, try, try!

    Array

    +
    +
    +
    +
    +

    Lint/SafeNavigationWithEmpty

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.62

    0.87

    +
    +

    Checks to make sure safe navigation isn’t used with empty? in +a conditional.

    +
    +
    +

    While the safe navigation operator is generally a good idea, when +checking foo&.empty? in a conditional, foo being nil will actually +do the opposite of what the author intends.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +return if foo&.empty?
    +return unless foo&.empty?
    +
    +# good
    +return if foo && foo.empty?
    +return unless foo && foo.empty?
    +
    +
    +
    +
    +
    +
    +

    Lint/ScriptPermission

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    0.50

    +
    +

    Checks if a file which has a shebang line as +its first line is granted execute permission.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +# A file which has a shebang line as its first line is not
    +# granted execute permission.
    +
    +#!/usr/bin/env ruby
    +puts 'hello, world'
    +
    +# good
    +
    +# A file which has a shebang line as its first line is
    +# granted execute permission.
    +
    +#!/usr/bin/env ruby
    +puts 'hello, world'
    +
    +# good
    +
    +# A file which has not a shebang line as its first line is not
    +# granted execute permission.
    +
    +puts 'hello, world'
    +
    +
    +
    +
    +
    +
    +

    Lint/SelfAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.89

    -

    +
    +

    Checks for self-assignments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo = foo
    +foo, bar = foo, bar
    +Foo = Foo
    +hash['foo'] = hash['foo']
    +obj.attr = obj.attr
    +
    +# good
    +foo = bar
    +foo, bar = bar, foo
    +Foo = Bar
    +hash['foo'] = hash['bar']
    +obj.attr = obj.attr2
    +
    +# good (method calls possibly can return different results)
    +hash[foo] = hash[foo]
    +
    +
    +
    +
    +
    +
    +

    Lint/SendWithMixinArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.75

    -

    +
    +

    Checks for send, public_send, and send methods +when using mix-in.

    +
    +
    +

    include and prepend methods were private methods until Ruby 2.0, +they were mixed-in via send method. This cop uses Ruby 2.1 or +higher style that can be called by public methods. +And extend method that was originally a public method is also targeted +for style unification.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Foo.send(:include, Bar)
    +Foo.send(:prepend, Bar)
    +Foo.send(:extend, Bar)
    +
    +# bad
    +Foo.public_send(:include, Bar)
    +Foo.public_send(:prepend, Bar)
    +Foo.public_send(:extend, Bar)
    +
    +# bad
    +Foo.__send__(:include, Bar)
    +Foo.__send__(:prepend, Bar)
    +Foo.__send__(:extend, Bar)
    +
    +# good
    +Foo.include Bar
    +Foo.prepend Bar
    +Foo.extend Bar
    +
    +
    +
    +
    +
    +
    +

    Lint/ShadowedArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.52

    -

    +
    +

    Checks for shadowed arguments.

    +
    +
    +

    This cop has IgnoreImplicitReferences configuration option. +It means argument shadowing is used in order to pass parameters +to zero arity super when IgnoreImplicitReferences is true.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something do |foo|
    +  foo = 42
    +  puts foo
    +end
    +
    +def do_something(foo)
    +  foo = 42
    +  puts foo
    +end
    +
    +# good
    +do_something do |foo|
    +  foo = foo + 42
    +  puts foo
    +end
    +
    +def do_something(foo)
    +  foo = foo + 42
    +  puts foo
    +end
    +
    +def do_something(foo)
    +  puts foo
    +end
    +
    +
    +
    +

    IgnoreImplicitReferences: false (default)

    +
    +
    +
    # bad
    +def do_something(foo)
    +  foo = 42
    +  super
    +end
    +
    +def do_something(foo)
    +  foo = super
    +  bar
    +end
    +
    +
    +
    +
    +

    IgnoreImplicitReferences: true

    +
    +
    +
    # good
    +def do_something(foo)
    +  foo = 42
    +  super
    +end
    +
    +def do_something(foo)
    +  foo = super
    +  bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IgnoreImplicitReferences

    false

    Boolean

    +
    +
    +
    +
    +

    Lint/ShadowedException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.41

    -

    +
    +

    Checks for a rescued exception that get shadowed by a +less specific exception being rescued before a more specific +exception is rescued.

    +
    +
    +

    An exception is considered shadowed if it is rescued after its +ancestor is, or if it and its ancestor are both rescued in the +same rescue statement. In both cases, the more specific rescue is +unnecessary because it is covered by rescuing the less specific +exception. (ie. rescue Exception, StandardError has the same behavior +whether StandardError is included or not, because all StandardErrors +are rescued by rescue Exception).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +begin
    +  something
    +rescue Exception
    +  handle_exception
    +rescue StandardError
    +  handle_standard_error
    +end
    +
    +# bad
    +begin
    +  something
    +rescue Exception, StandardError
    +  handle_error
    +end
    +
    +# good
    +
    +begin
    +  something
    +rescue StandardError
    +  handle_standard_error
    +rescue Exception
    +  handle_exception
    +end
    +
    +# good, however depending on runtime environment.
    +#
    +# This is a special case for system call errors.
    +# System dependent error code depends on runtime environment.
    +# For example, whether `Errno::EAGAIN` and `Errno::EWOULDBLOCK` are
    +# the same error code or different error code depends on environment.
    +# This good case is for `Errno::EAGAIN` and `Errno::EWOULDBLOCK` with
    +# the same error code.
    +begin
    +  something
    +rescue Errno::EAGAIN, Errno::EWOULDBLOCK
    +  handle_standard_error
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/ShadowingOuterLocalVariable

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    -

    +
    +

    Checks for the use of local variable names from an outer scope +in block arguments or block-local variables. This mirrors the warning +given by ruby -cw prior to Ruby 2.6: +"shadowing outer local variable - foo".

    +
    +
    + + + + + +
    + + +Shadowing of variables in block passed to Ractor.new is allowed +because Ractor should not access outer variables. +eg. following style is encouraged: +
    +
    +
    +
    +
    worker_id, pipe = env
    +Ractor.new(worker_id, pipe) do |worker_id, pipe|
    +end
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  foo = 1
    +
    +  2.times do |foo| # shadowing outer `foo`
    +    do_something(foo)
    +  end
    +end
    +
    +# good
    +def some_method
    +  foo = 1
    +
    +  2.times do |bar|
    +    do_something(bar)
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/StructNewOverride

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.81

    -

    +
    +

    Checks unexpected overrides of the Struct built-in methods +via Struct.new.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Bad = Struct.new(:members, :clone, :count)
    +b = Bad.new([], true, 1)
    +b.members #=> [] (overriding `Struct#members`)
    +b.clone #=> true (overriding `Object#clone`)
    +b.count #=> 1 (overriding `Enumerable#count`)
    +
    +# good
    +Good = Struct.new(:id, :name)
    +g = Good.new(1, "foo")
    +g.members #=> [:id, :name]
    +g.clone #=> #<struct Good id=1, name="foo">
    +g.count #=> 2
    +
    +
    +
    +
    +
    +
    +

    Lint/SuppressedException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    1.12

    +
    +

    Checks for rescue blocks with no body.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  do_something
    +rescue
    +end
    +
    +# bad
    +begin
    +  do_something
    +rescue
    +end
    +
    +# good
    +def some_method
    +  do_something
    +rescue
    +  handle_exception
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue
    +  handle_exception
    +end
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +def some_method
    +  do_something
    +rescue
    +  # do nothing
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue
    +  # do nothing
    +end
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +def some_method
    +  do_something
    +rescue
    +  # do nothing
    +end
    +
    +# bad
    +begin
    +  do_something
    +rescue
    +  # do nothing
    +end
    +
    +
    +
    +
    +

    AllowNil: true (default)

    +
    +
    +
    # good
    +def some_method
    +  do_something
    +rescue
    +  nil
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue
    +  # do nothing
    +end
    +
    +# good
    +do_something rescue nil
    +
    +
    +
    +
    +

    AllowNil: false

    +
    +
    +
    # bad
    +def some_method
    +  do_something
    +rescue
    +  nil
    +end
    +
    +# bad
    +begin
    +  do_something
    +rescue
    +  nil
    +end
    +
    +# bad
    +do_something rescue nil
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    AllowNil

    true

    Boolean

    +
    + +
    +
    +
    +

    Lint/SymbolConversion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.9

    1.16

    +
    +

    Checks for uses of literal strings converted to +a symbol where a literal symbol could be used instead.

    +
    +
    +

    There are two possible styles for this cop. +strict (default) will register an offense for any incorrect usage. +consistent additionally requires hashes to use the same style for +every symbol key (ie. if any symbol key needs to be quoted it requires +all keys to be quoted).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'string'.to_sym
    +:symbol.to_sym
    +'underscored_string'.to_sym
    +:'underscored_symbol'
    +'hyphenated-string'.to_sym
    +"string_#{interpolation}".to_sym
    +
    +# good
    +:string
    +:symbol
    +:underscored_string
    +:underscored_symbol
    +:'hyphenated-string'
    +:"string_#{interpolation}"
    +
    +
    +
    +

    EnforcedStyle: strict (default)

    +
    +
    +
    # bad
    +{
    +  'a': 1,
    +  "b": 2,
    +  'c-d': 3
    +}
    +
    +# good (don't quote keys that don't require quoting)
    +{
    +  a: 1,
    +  b: 2,
    +  'c-d': 3
    +}
    +
    +
    +
    +
    +

    EnforcedStyle: consistent

    +
    +
    +
    # bad
    +{
    +  a: 1,
    +  'b-c': 2
    +}
    +
    +# good (quote all keys if any need quoting)
    +{
    +  'a': 1,
    +  'b-c': 2
    +}
    +
    +# good (no quoting required)
    +{
    +  a: 1,
    +  b: 2
    +}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    strict

    strict, consistent

    +
    +
    +
    +
    +

    Lint/Syntax

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    -

    +
    +

    Repacks Parser’s diagnostics/errors +into RuboCop’s offenses.

    +
    +
    +
    +
    +

    Lint/ToEnumArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.1

    -

    +
    +

    Ensures that to_enum/enum_for, called for the current method, +has correct arguments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo(x, y = 1)
    +  return to_enum(__callee__, x) # `y` is missing
    +end
    +
    +# good
    +def foo(x, y = 1)
    +  # Alternatives to `__callee__` are `__method__` and `:foo`.
    +  return to_enum(__callee__, x, y)
    +end
    +
    +# good
    +def foo(x, y = 1)
    +  # It is also allowed if it is wrapped in some method like Sorbet.
    +  return to_enum(T.must(__callee__), x, y)
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/ToJSON

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.66

    -

    +
    +

    Checks to make sure #to_json includes an optional argument. +When overriding #to_json, callers may invoke JSON +generation via JSON.generate(your_obj). Since JSON#generate allows +for an optional argument, your method should too.

    +
    +
    +

    Examples

    +
    +
    +
    class Point
    +  attr_reader :x, :y
    +
    +  # bad, incorrect arity
    +  def to_json
    +    JSON.generate([x, y])
    +  end
    +
    +  # good, preserving args
    +  def to_json(*args)
    +    JSON.generate([x, y], *args)
    +  end
    +
    +  # good, discarding args
    +  def to_json(*_args)
    +    JSON.generate([x, y])
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/TopLevelReturnWithArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.89

    -

    +
    +

    Checks for top level return with arguments. If there is a +top-level return statement with an argument, then the argument is +always ignored. This is detected automatically since Ruby 2.7.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +return 1
    +
    +# good
    +return
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Exclude

    **/*.jb

    Array

    +
    +
    +
    +
    +

    Lint/TrailingCommaInAttributeDeclaration

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.90

    1.61

    +
    +

    Checks for trailing commas in attribute declarations, such as +#attr_reader. Leaving a trailing comma will nullify the next method +definition by overriding it with a getter method.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  attr_reader :foo,
    +
    +  def bar
    +    puts "Unreachable."
    +  end
    +end
    +
    +# good
    +class Foo
    +  attr_reader :foo
    +
    +  def bar
    +    puts "No problem!"
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/TripleQuotes

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.9

    -

    +
    +

    Checks for "triple quotes" (strings delimited by any odd number +of quotes greater than 1).

    +
    +
    +

    Ruby allows multiple strings to be implicitly concatenated by just +being adjacent in a statement (ie. "foo""bar" == "foobar"). This sometimes +gives the impression that there is something special about triple quotes, but +in fact it is just extra unnecessary quotes and produces the same string. Each +pair of quotes produces an additional concatenated empty string, so the result +is still only the "actual" string within the delimiters.

    +
    +
    + + + + + +
    + + +Although this cop is called triple quotes, the same behavior is present +for strings delimited by 5, 7, etc. quotation marks. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"""
    +  A string
    +"""
    +
    +# bad
    +'''
    +  A string
    +'''
    +
    +# good
    +"
    +  A string
    +"
    +
    +# good
    +<<STRING
    +  A string
    +STRING
    +
    +# good (but not the same spacing as the bad case)
    +'A string'
    +
    +
    +
    +
    +
    +
    +

    Lint/UnderscorePrefixedVariableName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.21

    -

    +
    +

    Checks for underscore-prefixed variables that are actually +used.

    +
    +
    +

    Since block keyword arguments cannot be arbitrarily named at call +sites, the AllowKeywordBlockArguments will allow use of underscore- +prefixed block keyword arguments.

    +
    +
    +

    Examples

    +
    +

    AllowKeywordBlockArguments: false (default)

    +
    +
    +
    # bad
    +
    +[1, 2, 3].each do |_num|
    +  do_something(_num)
    +end
    +
    +query(:sales) do |_id:, revenue:, cost:|
    +  {_id: _id, profit: revenue - cost}
    +end
    +
    +# good
    +
    +[1, 2, 3].each do |num|
    +  do_something(num)
    +end
    +
    +[1, 2, 3].each do |_num|
    +  do_something # not using `_num`
    +end
    +
    +
    +
    +
    +

    AllowKeywordBlockArguments: true

    +
    +
    +
    # good
    +
    +query(:sales) do |_id:, revenue:, cost:|
    +  {_id: _id, profit: revenue - cost}
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowKeywordBlockArguments

    false

    Boolean

    +
    +
    +
    +
    +

    Lint/UnescapedBracketInRegexp

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.68

    -

    +
    +

    Checks for Regexpes (both literals and via Regexp.new / Regexp.compile) +that contain unescaped ] characters.

    +
    +
    +

    It emulates the following Ruby warning:

    +
    +
    +
    +
    $ ruby -e '/abc]123/'
    +-e:1: warning: regular expression has ']' without escape: /abc]123/
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +/abc]123/
    +%r{abc]123}
    +Regexp.new('abc]123')
    +Regexp.compile('abc]123')
    +
    +# good
    +/abc\]123/
    +%r{abc\]123}
    +Regexp.new('abc\]123')
    +Regexp.compile('abc\]123')
    +
    +
    +
    +
    +
    +
    +

    Lint/UnexpectedBlockArity

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    No

    1.5

    -

    +
    +

    Checks for a block that is known to need more positional +block arguments than are given (by default this is configured for +Enumerable methods needing 2 arguments). Optional arguments are allowed, +although they don’t generally make sense as the default value will +be used. Blocks that have no receiver, or take splatted arguments +(ie. *args) are always accepted.

    +
    +
    +

    Keyword arguments (including **kwargs) do not get counted towards +this, as they are not used by the methods in question.

    +
    +
    +

    Method names and their expected arity can be configured like this:

    +
    +
    +
    +
    Methods:
    +  inject: 2
    +  reduce: 2
    +
    +
    +
    +

    Safety

    +
    +

    This cop matches for method names only and hence cannot tell apart +methods with same name in different classes, which may lead to a +false positive.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +values.reduce {}
    +values.min { |a| a }
    +values.sort { |a; b| a + b }
    +
    +# good
    +values.reduce { |memo, obj| memo << obj }
    +values.min { |a, b| a <=> b }
    +values.sort { |*x| x[0] <=> x[1] }
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Methods

    {"chunk_while"⇒2, "each_with_index"⇒2, "each_with_object"⇒2, "inject"⇒2, "max"⇒2, "min"⇒2, "minmax"⇒2, "reduce"⇒2, "slice_when"⇒2, "sort"⇒2}

    +
    +
    +
    +
    +

    Lint/UnifiedInteger

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.43

    -

    +
    +

    Checks for using Fixnum or Bignum constant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +1.is_a?(Fixnum)
    +1.is_a?(Bignum)
    +
    +# good
    +1.is_a?(Integer)
    +
    +
    +
    +
    +
    +
    +

    Lint/UnmodifiedReduceAccumulator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.1

    1.5

    +
    +

    Looks for reduce or inject blocks where the value returned (implicitly or +explicitly) does not include the accumulator. A block is considered valid as +long as at least one return value includes the accumulator.

    +
    +
    +

    If the accumulator is not included in the return value, then the entire +block will just return a transformation of the last element value, and +could be rewritten as such without a loop.

    +
    +
    +

    Also catches instances where an index of the accumulator is returned, as +this may change the type of object being retained.

    +
    +
    + + + + + +
    + + +For the purpose of reducing false positives, this cop only flags +returns in reduce blocks where the element is the only variable in +the expression (since we will not be able to tell what other variables +relate to via static analysis). +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(1..4).reduce(0) do |acc, el|
    +  el * 2
    +end
    +
    +# bad, may raise a NoMethodError after the first iteration
    +%w(a b c).reduce({}) do |acc, letter|
    +  acc[letter] = true
    +end
    +
    +# good
    +(1..4).reduce(0) do |acc, el|
    +  acc + el * 2
    +end
    +
    +# good, element is returned but modified using the accumulator
    +values.reduce do |acc, el|
    +  el << acc
    +  el
    +end
    +
    +# good, returns the accumulator instead of the index
    +%w(a b c).reduce({}) do |acc, letter|
    +  acc[letter] = true
    +  acc
    +end
    +
    +# good, at least one branch returns the accumulator
    +values.reduce(nil) do |result, value|
    +  break result if something?
    +  value
    +end
    +
    +# good, recursive
    +keys.reduce(self) { |result, key| result[key] }
    +
    +# ignored as the return value cannot be determined
    +enum.reduce do |acc, el|
    +  x = foo(acc, el)
    +  bar(x)
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UnreachableCode

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    -

    +
    +

    Checks for unreachable code. +The check are based on the presence of flow of control +statement in non-final position in begin (implicit) blocks.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  return
    +  do_something
    +end
    +
    +# bad
    +def some_method
    +  if cond
    +    return
    +  else
    +    return
    +  end
    +  do_something
    +end
    +
    +# good
    +def some_method
    +  do_something
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UnreachableLoop

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.89

    1.7

    +
    +

    Checks for loops that will have at most one iteration.

    +
    +
    +

    A loop that can never reach the second iteration is a possible error in the code. +In rare cases where only one iteration (or at most one iteration) is intended behavior, +the code should be refactored to use if conditionals.

    +
    +
    + + + + + +
    + + +Block methods that are used with Enumerables are considered to be loops. +
    +
    +
    +

    AllowedPatterns can be used to match against the block receiver in order to allow +code that would otherwise be registered as an offense (eg. times used not in an +Enumerable context).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +while node
    +  do_something(node)
    +  node = node.parent
    +  break
    +end
    +
    +# good
    +while node
    +  do_something(node)
    +  node = node.parent
    +end
    +
    +# bad
    +def verify_list(head)
    +  item = head
    +  begin
    +    if verify(item)
    +      return true
    +    else
    +      return false
    +    end
    +  end while(item)
    +end
    +
    +# good
    +def verify_list(head)
    +  item = head
    +  begin
    +    if verify(item)
    +      item = item.next
    +    else
    +      return false
    +    end
    +  end while(item)
    +
    +  true
    +end
    +
    +# bad
    +def find_something(items)
    +  items.each do |item|
    +    if something?(item)
    +      return item
    +    else
    +      raise NotFoundError
    +    end
    +  end
    +end
    +
    +# good
    +def find_something(items)
    +  items.each do |item|
    +    if something?(item)
    +      return item
    +    end
    +  end
    +  raise NotFoundError
    +end
    +
    +# bad
    +2.times { raise ArgumentError }
    +
    +
    +
    +

    AllowedPatterns: ['(exactly|at_least|at_most)\(\d+\)\.times'] (default)

    +
    +
    +
    # good
    +exactly(2).times { raise StandardError }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedPatterns

    (?-mix:(exactly|at_least|at_most)\(\d+\)\.times)

    Array

    +
    +
    +
    +
    +

    Lint/UnusedBlockArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.21

    1.61

    +
    +

    Checks for unused block arguments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something do |used, unused|
    +  puts used
    +end
    +
    +do_something do |bar|
    +  puts :foo
    +end
    +
    +define_method(:foo) do |bar|
    +  puts :baz
    +end
    +
    +# good
    +do_something do |used, _unused|
    +  puts used
    +end
    +
    +do_something do
    +  puts :foo
    +end
    +
    +define_method(:foo) do |_bar|
    +  puts :baz
    +end
    +
    +
    +
    +

    IgnoreEmptyBlocks: true (default)

    +
    +
    +
    # good
    +do_something { |unused| }
    +
    +
    +
    +
    +

    IgnoreEmptyBlocks: false

    +
    +
    +
    # bad
    +do_something { |unused| }
    +
    +
    +
    +
    +

    AllowUnusedKeywordArguments: false (default)

    +
    +
    +
    # bad
    +do_something do |unused: 42|
    +  foo
    +end
    +
    +
    +
    +
    +

    AllowUnusedKeywordArguments: true

    +
    +
    +
    # good
    +do_something do |unused: 42|
    +  foo
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IgnoreEmptyBlocks

    true

    Boolean

    AllowUnusedKeywordArguments

    false

    Boolean

    +
    + +
    +
    +
    +

    Lint/UnusedMethodArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.21

    1.69

    +
    +

    Checks for unused method arguments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method(used, unused, _unused_but_allowed)
    +  puts used
    +end
    +
    +# good
    +def some_method(used, _unused, _unused_but_allowed)
    +  puts used
    +end
    +
    +
    +
    +

    AllowUnusedKeywordArguments: false (default)

    +
    +
    +
    # bad
    +def do_something(used, unused: 42)
    +  used
    +end
    +
    +
    +
    +
    +

    AllowUnusedKeywordArguments: true

    +
    +
    +
    # good
    +def do_something(used, unused: 42)
    +  used
    +end
    +
    +
    +
    +
    +

    IgnoreEmptyMethods: true (default)

    +
    +
    +
    # good
    +def do_something(unused)
    +end
    +
    +
    +
    +
    +

    IgnoreEmptyMethods: false

    +
    +
    +
    # bad
    +def do_something(unused)
    +end
    +
    +
    +
    +
    +

    IgnoreNotImplementedMethods: true (default)

    +
    +
    +
    # with default value of `NotImplementedExceptions: ['NotImplementedError']`
    +
    +# good
    +def do_something(unused)
    +  raise NotImplementedError
    +end
    +
    +def do_something_else(unused)
    +  fail "TODO"
    +end
    +
    +
    +
    +
    +

    IgnoreNotImplementedMethods: true

    +
    +
    +
    # with `NotImplementedExceptions: ['AbstractMethodError']`
    +
    +# good
    +def do_something(unused)
    +  raise AbstractMethodError
    +end
    +
    +
    +
    +
    +

    IgnoreNotImplementedMethods: false

    +
    +
    +
    # bad
    +def do_something(unused)
    +  raise NotImplementedError
    +end
    +
    +def do_something_else(unused)
    +  fail "TODO"
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowUnusedKeywordArguments

    false

    Boolean

    IgnoreEmptyMethods

    true

    Boolean

    IgnoreNotImplementedMethods

    true

    Boolean

    NotImplementedExceptions

    NotImplementedError

    Array

    +
    + +
    +
    +
    +

    Lint/UriEscapeUnescape

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Identifies places where URI.escape can be replaced by +CGI.escape, URI.encode_www_form, or URI.encode_www_form_component +depending on your specific use case. +Also this cop identifies places where URI.unescape can be replaced by +CGI.unescape, URI.decode_www_form, +or URI.decode_www_form_component depending on your specific use case.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +URI.escape('http://example.com')
    +URI.encode('http://example.com')
    +
    +# good
    +CGI.escape('http://example.com')
    +URI.encode_www_form([['example', 'param'], ['lang', 'en']])
    +URI.encode_www_form(page: 10, locale: 'en')
    +URI.encode_www_form_component('http://example.com')
    +
    +# bad
    +URI.unescape(enc_uri)
    +URI.decode(enc_uri)
    +
    +# good
    +CGI.unescape(enc_uri)
    +URI.decode_www_form(enc_uri)
    +URI.decode_www_form_component(enc_uri)
    +
    +
    +
    +
    +
    +
    +

    Lint/UriRegexp

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Identifies places where URI.regexp is obsolete and should not be used.

    +
    +
    +

    For Ruby 3.3 or lower, use URI::DEFAULT_PARSER.make_regexp. +For Ruby 3.4 or higher, use URI::RFC2396_PARSER.make_regexp.

    +
    +
    + + + + + +
    + + +If you need to support both Ruby 3.3 and lower as well as Ruby 3.4 and higher, +consider manually changing the code as follows: +
    +
    +
    +
    +
    defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +URI.regexp('http://example.com')
    +
    +# good - Ruby 3.3 or lower
    +URI::DEFAULT_PARSER.make_regexp('http://example.com')
    +
    +# good - Ruby 3.4 or higher
    +URI::RFC2396_PARSER.make_regexp('http://example.com')
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessAccessModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.20

    1.61

    +
    +

    Checks for redundant access modifiers, including those with no +code, those which are repeated, and leading public modifiers in a +class or module body. Conditionally-defined methods are considered as +always being defined, and thus access modifiers guarding such methods +are not redundant.

    +
    +
    +

    This cop has ContextCreatingMethods option. The default setting value +is an empty array that means no method is specified. +This setting is an array of methods which, when called, are known to +create its own context in the module’s current access context.

    +
    +
    +

    It also has MethodCreatingMethods option. The default setting value +is an empty array that means no method is specified. +This setting is an array of methods which, when called, are known to +create other methods in the module’s current access context.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  public # this is redundant (default access is public)
    +
    +  def method
    +  end
    +end
    +
    +# bad
    +class Foo
    +  # The following is redundant (methods defined on the class'
    +  # singleton class are not affected by the private modifier)
    +  private
    +
    +  def self.method3
    +  end
    +end
    +
    +# bad
    +class Foo
    +  protected
    +
    +  define_method(:method2) do
    +  end
    +
    +  protected # this is redundant (repeated from previous modifier)
    +
    +  [1,2,3].each do |i|
    +    define_method("foo#{i}") do
    +    end
    +  end
    +end
    +
    +# bad
    +class Foo
    +  private # this is redundant (no following methods are defined)
    +end
    +
    +# good
    +class Foo
    +  private # this is not redundant (a method is defined)
    +
    +  def method2
    +  end
    +end
    +
    +# good
    +class Foo
    +  # The following is not redundant (conditionally defined methods are
    +  # considered as always defining a method)
    +  private
    +
    +  if condition?
    +    def method
    +    end
    +  end
    +end
    +
    +# good
    +class Foo
    +  protected # this is not redundant (a method is defined)
    +
    +  define_method(:method2) do
    +  end
    +end
    +
    +
    +
    +

    ContextCreatingMethods: concerning

    +
    +
    +
    # Lint/UselessAccessModifier:
    +#   ContextCreatingMethods:
    +#     - concerning
    +
    +# good
    +require 'active_support/concern'
    +class Foo
    +  concerning :Bar do
    +    def some_public_method
    +    end
    +
    +    private
    +
    +    def some_private_method
    +    end
    +  end
    +
    +  # this is not redundant because `concerning` created its own context
    +  private
    +
    +  def some_other_private_method
    +  end
    +end
    +
    +
    +
    +
    +

    MethodCreatingMethods: delegate

    +
    +
    +
    # Lint/UselessAccessModifier:
    +#   MethodCreatingMethods:
    +#     - delegate
    +
    +# good
    +require 'active_support/core_ext/module/delegation'
    +class Foo
    +  # this is not redundant because `delegate` creates methods
    +  private
    +
    +  delegate :method_a, to: :method_b
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    ContextCreatingMethods

    []

    Array

    MethodCreatingMethods

    []

    Array

    +
    +
    +
    +
    +

    Lint/UselessAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.11

    1.66

    +
    +

    Checks for every useless assignment to local variable in every +scope. +The basic idea for this cop was from the warning of ruby -cw:

    +
    +
    +
    +
    assigned but unused variable - foo
    +
    +
    +
    +

    Currently this cop has advanced logic that detects unreferenced +reassignments and properly handles varied cases such as branch, loop, +rescue, ensure, etc.

    +
    +
    +

    This cop’s autocorrection avoids cases like a ||= 1 because removing assignment from +operator assignment can cause NameError if this assignment has been used to declare +a local variable. For example, replacing a ||= 1 with a || 1 may cause +"undefined local variable or method `a' for main:Object (NameError)".

    +
    +
    + + + + + +
    + + +Given the assignment foo = 1, bar = 2, removing unused variables +can lead to a syntax error, so this case is not autocorrected. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +  some_var = 1
    +  do_something
    +end
    +
    +# good
    +def some_method
    +  some_var = 1
    +  do_something(some_var)
    +end
    +
    +
    +
    + +
    +
    +
    +

    Lint/UselessDefined

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.69

    -

    +
    +

    Checks for calls to defined? with strings or symbols as the argument. +Such calls will always return 'expression', you probably meant to +check for the existence of a constant, method, or variable instead.

    +
    +
    +

    defined? is part of the Ruby syntax and doesn’t behave like normal methods. +You can safely pass in what you are checking for directly, without encountering +a NameError.

    +
    +
    +

    When interpolation is used, oftentimes it is not possible to write the +code with defined?. In these cases, switch to one of the more specific methods:

    +
    +
    +
      +
    • +

      class_variable_defined?

      +
    • +
    • +

      const_defined?

      +
    • +
    • +

      method_defined?

      +
    • +
    • +

      instance_variable_defined?

      +
    • +
    • +

      binding.local_variable_defined?

      +
    • +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +defined?('FooBar')
    +defined?(:FooBar)
    +defined?(:foo_bar)
    +defined?('foo_bar')
    +
    +# good
    +defined?(FooBar)
    +defined?(foo_bar)
    +
    +# bad - interpolation
    +bar = 'Bar'
    +defined?("Foo::#{bar}::Baz")
    +
    +# good
    +bar = 'Bar'
    +defined?(Foo) && Foo.const_defined?(bar) && Foo.const_get(bar).const_defined?(:Baz)
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessElseWithoutRescue

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.17

    1.31

    +
    +

    Checks for useless else in begin..end without rescue.

    +
    +
    + + + + + +
    + + +This syntax is no longer valid on Ruby 2.6 or higher. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +begin
    +  do_something
    +else
    +  do_something_else # This will never be run.
    +end
    +
    +# good
    +begin
    +  do_something
    +rescue
    +  handle_errors
    +else
    +  do_something_else
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Command-line only (Unsafe)

    0.90

    1.61

    +
    +

    Checks for useless method definitions, specifically: empty constructors +and methods just delegating to super.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe as it can register false positives for cases when an empty +constructor just overrides the parent constructor, which is bad anyway.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def initialize
    +  super
    +end
    +
    +def method
    +  super
    +end
    +
    +# good - with default arguments
    +def initialize(x = Object.new)
    +  super
    +end
    +
    +# good
    +def initialize
    +  super
    +  initialize_internals
    +end
    +
    +def method(*args)
    +  super(:extra_arg, *args)
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessNumericOperation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.66

    -

    +
    +

    Certain numeric operations have no impact, being: +Adding or subtracting 0, multiplying or dividing by 1 or raising to the power of 1. +These are probably leftover from debugging, or are mistakes.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x + 0
    +x - 0
    +x * 1
    +x / 1
    +x ** 1
    +
    +# good
    +x
    +
    +# bad
    +x += 0
    +x -= 0
    +x *= 1
    +x /= 1
    +x **= 1
    +
    +# good
    +x = x
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessRescue

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.43

    -

    +
    +

    Checks for useless `rescue`s, which only reraise rescued exceptions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo
    +  do_something
    +rescue
    +  raise
    +end
    +
    +# bad
    +def foo
    +  do_something
    +rescue => e
    +  raise # or 'raise e', or 'raise $!', or 'raise $ERROR_INFO'
    +end
    +
    +# good
    +def foo
    +  do_something
    +rescue
    +  do_cleanup
    +  raise
    +end
    +
    +# bad (latest rescue)
    +def foo
    +  do_something
    +rescue ArgumentError
    +  # noop
    +rescue
    +  raise
    +end
    +
    +# good (not the latest rescue)
    +def foo
    +  do_something
    +rescue ArgumentError
    +  raise
    +rescue
    +  # noop
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessRuby2Keywords

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.23

    -

    +
    +

    Looks for ruby2_keywords calls for methods that do not need it.

    +
    +
    +

    ruby2_keywords should only be called on methods that accept an argument splat +(*args) but do not explicit keyword arguments (k: or k: true) or +a keyword splat (**kwargs).

    +
    +
    +

    Examples

    +
    +
    +
    # good (splat argument without keyword arguments)
    +ruby2_keywords def foo(*args); end
    +
    +# bad (no arguments)
    +ruby2_keywords def foo; end
    +
    +# good
    +def foo; end
    +
    +# bad (positional argument)
    +ruby2_keywords def foo(arg); end
    +
    +# good
    +def foo(arg); end
    +
    +# bad (double splatted argument)
    +ruby2_keywords def foo(**args); end
    +
    +# good
    +def foo(**args); end
    +
    +# bad (keyword arguments)
    +ruby2_keywords def foo(i:, j:); end
    +
    +# good
    +def foo(i:, j:); end
    +
    +# bad (splat argument with keyword arguments)
    +ruby2_keywords def foo(*args, i:, j:); end
    +
    +# good
    +def foo(*args, i:, j:); end
    +
    +# bad (splat argument with double splat)
    +ruby2_keywords def foo(*args, **kwargs); end
    +
    +# good
    +def foo(*args, **kwargs); end
    +
    +# bad (ruby2_keywords given a symbol)
    +def foo; end
    +ruby2_keywords :foo
    +
    +# good
    +def foo; end
    +
    +# bad (ruby2_keywords with dynamic method)
    +define_method(:foo) { |arg| }
    +ruby2_keywords :foo
    +
    +# good
    +define_method(:foo) { |arg| }
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessSetterCall

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.13

    1.2

    +
    +

    Checks for setter call to local variable as the final +expression of a function definition.

    +
    +
    +

    Safety

    +
    +

    There are edge cases in which the local variable references a +value that is also accessible outside the local scope. This is not +detected by the cop, and it can yield a false positive.

    +
    +
    +

    As well, autocorrection is unsafe because the method’s +return value will be changed.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def something
    +  x = Something.new
    +  x.attr = 5
    +end
    +
    +# good
    +def something
    +  x = Something.new
    +  x.attr = 5
    +  x
    +end
    +
    +
    +
    +
    +
    +
    +

    Lint/UselessTimes

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Command-line only (Unsafe)

    0.91

    1.61

    +
    +

    Checks for uses of Integer#times that will never yield +(when the integer ⇐ 0) or that will only ever yield once +(1.times).

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe as times returns its receiver, which is +usually OK, but might change behavior.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +-5.times { do_something }
    +0.times { do_something }
    +1.times { do_something  }
    +1.times { |i| do_something(i) }
    +
    +# good
    +do_something
    +do_something(1)
    +
    +
    +
    +
    +
    +
    +

    Lint/Void

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.9

    1.61

    +
    +

    Checks for operators, variables, literals, lambda, proc and nonmutating +methods used in void context.

    +
    +
    +

    each blocks are allowed to prevent false positives. +For example, the expression inside the each block below. +It’s not void, especially when the receiver is an Enumerator:

    +
    +
    +
    +
    enumerator = [1, 2, 3].filter
    +enumerator.each { |item| item >= 2 } #=> [2, 3]
    +
    +
    +
    +

    Examples

    +
    +

    CheckForMethodsWithNoSideEffects: false (default)

    +
    +
    +
    # bad
    +def some_method
    +  some_num * 10
    +  do_something
    +end
    +
    +def some_method(some_var)
    +  some_var
    +  do_something
    +end
    +
    +
    +
    +
    +

    CheckForMethodsWithNoSideEffects: true

    +
    +
    +
    # bad
    +def some_method(some_array)
    +  some_array.sort
    +  do_something(some_array)
    +end
    +
    +# good
    +def some_method
    +  do_something
    +  some_num * 10
    +end
    +
    +def some_method(some_var)
    +  do_something
    +  some_var
    +end
    +
    +def some_method(some_array)
    +  some_array.sort!
    +  do_something(some_array)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CheckForMethodsWithNoSideEffects

    false

    Boolean

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_metrics.html b/docs/rubocop/1.69/cops_metrics.html new file mode 100644 index 000000000..d38d1fc76 --- /dev/null +++ b/docs/rubocop/1.69/cops_metrics.html @@ -0,0 +1,1897 @@ + + + + + + Metrics :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Metrics

    +
    +

    Metrics/AbcSize

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.27

    1.5

    +
    +

    Checks that the ABC size of methods is not higher than the +configured maximum. The ABC size is based on assignments, branches +(method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric +and https://en.wikipedia.org/wiki/ABC_Software_Metric.

    +
    +
    +

    Interpreting ABC size:

    +
    +
    +
      +
    • +

      ⇐ 17 satisfactory

      +
    • +
    • +

      18..30 unsatisfactory

      +
    • +
    • +

      > 30 dangerous

      +
    • +
    +
    +
    +

    You can have repeated "attributes" calls count as a single "branch". +For this purpose, attributes are any method with no argument; no attempt +is meant to distinguish actual attr_reader from other methods.

    +
    +
    +

    This cop also takes into account AllowedMethods (defaults to []) +And AllowedPatterns (defaults to [])

    +
    +
    +

    Examples

    +
    +

    CountRepeatedAttributes: false (default is true)

    +
    +
    +
    # `model` and `current_user`, referenced 3 times each,
    +# are each counted as only 1 branch each if
    +# `CountRepeatedAttributes` is set to 'false'
    +
    +def search
    +  @posts = model.active.visible_by(current_user)
    +            .search(params[:q])
    +  @posts = model.some_process(@posts, current_user)
    +  @posts = model.another_process(@posts, current_user)
    +
    +  render 'pages/search/page'
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    CountRepeatedAttributes

    true

    Boolean

    Max

    17

    Integer

    +
    + +
    +
    +
    +

    Metrics/BlockLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.44

    1.5

    +
    +

    Checks if the length of a block exceeds some maximum value. +Comment lines can optionally be ignored. +The maximum allowed length is configurable. +The cop can be configured to ignore blocks passed to certain methods.

    +
    +
    +

    You can set constructs you want to fold with CountAsOne.

    +
    +
    +

    Available are: 'array', 'hash', 'heredoc', and 'method_call'. +Each construct will be counted as one line regardless of its actual size.

    +
    +
    + + + + + +
    + + +This cop does not apply for Struct definitions. +
    +
    +
    + + + + + +
    + + +The ExcludedMethods configuration is deprecated and only kept +for backwards compatibility. Please use AllowedMethods and AllowedPatterns +instead. By default, there are no methods to allowed. +
    +
    +
    +

    Examples

    +
    +

    CountAsOne: ['array', 'hash', 'heredoc', 'method_call']

    +
    +
    +
    something do
    +  array = [         # +1
    +    1,
    +    2
    +  ]
    +
    +  hash = {          # +1
    +    key: 'value'
    +  }
    +
    +  msg = <<~HEREDOC  # +1
    +    Heredoc
    +    content.
    +  HEREDOC
    +
    +  foo(              # +1
    +    1,
    +    2
    +  )
    +end                 # 4 points
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CountComments

    false

    Boolean

    Max

    25

    Integer

    CountAsOne

    []

    Array

    AllowedMethods

    refine

    Array

    AllowedPatterns

    []

    Array

    Exclude

    **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Metrics/BlockNesting

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    1.65

    +
    +

    Checks for excessive nesting of conditional and looping constructs.

    +
    +
    +

    You can configure if blocks are considered using the CountBlocks and CountModifierForms +options. When both are set to false (the default) blocks and modifier forms are not +counted towards the nesting level. Set them to true to include these in the nesting level +calculation as well.

    +
    +
    +

    The maximum level of nesting allowed is configurable.

    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CountBlocks

    false

    Boolean

    CountModifierForms

    false

    Boolean

    Max

    3

    Integer

    +
    + +
    +
    +
    +

    Metrics/ClassLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    0.87

    +
    +

    Checks if the length of a class exceeds some maximum value. +Comment lines can optionally be ignored. +The maximum allowed length is configurable.

    +
    +
    +

    You can set constructs you want to fold with CountAsOne.

    +
    +
    +

    Available are: 'array', 'hash', 'heredoc', and 'method_call'. +Each construct will be counted as one line regardless of its actual size.

    +
    +
    + + + + + +
    + + +This cop also applies for Struct definitions. +
    +
    +
    +

    Examples

    +
    +

    CountAsOne: ['array', 'hash', 'heredoc', 'method_call']

    +
    +
    +
    class Foo
    +  ARRAY = [         # +1
    +    1,
    +    2
    +  ]
    +
    +  HASH = {          # +1
    +    key: 'value'
    +  }
    +
    +  MSG = <<~HEREDOC  # +1
    +    Heredoc
    +    content.
    +  HEREDOC
    +
    +  foo(              # +1
    +    1,
    +    2
    +  )
    +end                 # 4 points
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CountComments

    false

    Boolean

    Max

    100

    Integer

    CountAsOne

    []

    Array

    +
    +
    +
    +
    +

    Metrics/CollectionLiteralLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.47

    -

    +
    +

    Checks for literals with extremely many entries. This is indicative of +configuration or data that may be better extracted somewhere else, like +a database, fetched from an API, or read from a non-code file (CSV, +JSON, YAML, etc.).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# Huge Array literal
    +[1, 2, '...', 999_999_999]
    +
    +# bad
    +# Huge Hash literal
    +{ 1 => 1, 2 => 2, '...' => '...', 999_999_999 => 999_999_999}
    +
    +# bad
    +# Huge Set "literal"
    +Set[1, 2, '...', 999_999_999]
    +
    +# good
    +# Reasonably sized Array literal
    +[1, 2, '...', 10]
    +
    +# good
    +# Reading huge Array from external data source
    +# File.readlines('numbers.txt', chomp: true).map!(&:to_i)
    +
    +# good
    +# Reasonably sized Hash literal
    +{ 1 => 1, 2 => 2, '...' => '...', 10 => 10}
    +
    +# good
    +# Reading huge Hash from external data source
    +CSV.foreach('numbers.csv', headers: true).each_with_object({}) do |row, hash|
    +  hash[row["key"].to_i] = row["value"].to_i
    +end
    +
    +# good
    +# Reasonably sized Set "literal"
    +Set[1, 2, '...', 10]
    +
    +# good
    +# Reading huge Set from external data source
    +SomeFramework.config_for(:something)[:numbers].to_set
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    LengthThreshold

    250

    Integer

    +
    +
    +
    +
    +

    Metrics/CyclomaticComplexity

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    0.81

    +
    +

    Checks that the cyclomatic complexity of methods is not higher +than the configured maximum. The cyclomatic complexity is the number of +linearly independent paths through a method. The algorithm counts +decision points and adds one.

    +
    +
    +

    An if statement (or unless or ?:) increases the complexity by one. An +else branch does not, since it doesn’t add a decision point. The && +operator (or keyword and) can be converted to a nested if statement, +and ||/or is shorthand for a sequence of ifs, so they also add one. +Loops can be said to have an exit condition, so they add one. +Blocks that are calls to builtin iteration methods +(e.g. ary.map{…​}) also add one, others are ignored.

    +
    +
    +

    Examples

    +
    +
    +
    def each_child_node(*types)               # count begins: 1
    +  unless block_given?                     # unless: +1
    +    return to_enum(__method__, *types)
    +  end
    +
    +  children.each do |child|                # each{}: +1
    +    next unless child.is_a?(Node)         # unless: +1
    +
    +    yield child if types.empty? ||        # if: +1, ||: +1
    +                   types.include?(child.type)
    +  end
    +
    +  self
    +end                                       # total: 6
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    Max

    7

    Integer

    +
    +
    +
    +
    +

    Metrics/MethodLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    1.5

    +
    +

    Checks if the length of a method exceeds some maximum value. +Comment lines can optionally be allowed. +The maximum allowed length is configurable.

    +
    +
    +

    You can set constructs you want to fold with CountAsOne.

    +
    +
    +

    Available are: 'array', 'hash', 'heredoc', and 'method_call'. +Each construct will be counted as one line regardless of its actual size.

    +
    +
    + + + + + +
    + + +The ExcludedMethods and IgnoredMethods configuration is +deprecated and only kept for backwards compatibility. +Please use AllowedMethods and AllowedPatterns instead. +By default, there are no methods to allowed. +
    +
    +
    +

    Examples

    +
    +

    CountAsOne: ['array', 'hash', 'heredoc', 'method_call']

    +
    +
    +
    def m
    +  array = [       # +1
    +    1,
    +    2
    +  ]
    +
    +  hash = {        # +1
    +    key: 'value'
    +  }
    +
    +  <<~HEREDOC      # +1
    +    Heredoc
    +    content.
    +  HEREDOC
    +
    +  foo(            # +1
    +    1,
    +    2
    +  )
    +end               # 4 points
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CountComments

    false

    Boolean

    Max

    10

    Integer

    CountAsOne

    []

    Array

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Metrics/ModuleLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.31

    0.87

    +
    +

    Checks if the length of a module exceeds some maximum value. +Comment lines can optionally be ignored. +The maximum allowed length is configurable.

    +
    +
    +

    You can set constructs you want to fold with CountAsOne.

    +
    +
    +

    Available are: 'array', 'hash', 'heredoc', and 'method_call'. +Each construct will be counted as one line regardless of its actual size.

    +
    +
    +

    Examples

    +
    +

    CountAsOne: ['array', 'hash', 'heredoc', 'method_call']

    +
    +
    +
    module M
    +  ARRAY = [         # +1
    +    1,
    +    2
    +  ]
    +
    +  HASH = {          # +1
    +    key: 'value'
    +  }
    +
    +  MSG = <<~HEREDOC  # +1
    +    Heredoc
    +    content.
    +  HEREDOC
    +
    +  foo(              # +1
    +    1,
    +    2
    +  )
    +end                 # 4 points
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CountComments

    false

    Boolean

    Max

    100

    Integer

    CountAsOne

    []

    Array

    +
    +
    +
    +
    +

    Metrics/ParameterLists

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    1.5

    +
    +

    Checks for methods with too many parameters.

    +
    +
    +

    The maximum number of parameters is configurable. +Keyword arguments can optionally be excluded from the total count, +as they add less complexity than positional or optional parameters.

    +
    +
    +

    Any number of arguments for initialize method inside a block of +Struct.new and Data.define like this is always allowed:

    +
    +
    +
    +
    Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
    +  def initialize(one:, two:, three:, four:, five:)
    +  end
    +end
    +
    +
    +
    +

    This is because checking the number of arguments of the initialize method +does not make sense.

    +
    +
    + + + + + +
    + + +Explicit block argument &block is not counted to prevent +erroneous change that is avoided by making block argument implicit. +
    +
    +
    +

    This cop also checks for the maximum number of optional parameters. +This can be configured using the MaxOptionalParameters config option.

    +
    +
    +

    Examples

    +
    +

    Max: 3

    +
    +
    +
    # good
    +def foo(a, b, c = 1)
    +end
    +
    +
    +
    +
    +

    Max: 2

    +
    +
    +
    # bad
    +def foo(a, b, c = 1)
    +end
    +
    +
    +
    +
    +

    CountKeywordArgs: true (default)

    +
    +
    +
    # counts keyword args towards the maximum
    +
    +# bad (assuming Max is 3)
    +def foo(a, b, c, d: 1)
    +end
    +
    +# good (assuming Max is 3)
    +def foo(a, b, c: 1)
    +end
    +
    +
    +
    +
    +

    CountKeywordArgs: false

    +
    +
    +
    # don't count keyword args towards the maximum
    +
    +# good (assuming Max is 3)
    +def foo(a, b, c, d: 1)
    +end
    +
    +
    +
    +
    +

    MaxOptionalParameters: 3 (default)

    +
    +
    +
    # good
    +def foo(a = 1, b = 2, c = 3)
    +end
    +
    +
    +
    +
    +

    MaxOptionalParameters: 2

    +
    +
    +
    # bad
    +def foo(a = 1, b = 2, c = 3)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Max

    5

    Integer

    CountKeywordArgs

    true

    Boolean

    MaxOptionalParameters

    3

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Metrics/PerceivedComplexity

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.25

    0.81

    +
    +

    Tries to produce a complexity score that’s a measure of the +complexity the reader experiences when looking at a method. For that +reason it considers when nodes as something that doesn’t add as much +complexity as an if or a &&. Except if it’s one of those special +case/when constructs where there’s no expression after case. Then +the cop treats it as an if/elsif/elsif…​ and lets all the when +nodes count. In contrast to the CyclomaticComplexity cop, this cop +considers else nodes as adding complexity.

    +
    +
    +

    Examples

    +
    +
    +
    def my_method                   # 1
    +  if cond                       # 1
    +    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    +    when 1 then func_one
    +    when 2 then func_two
    +    when 3 then func_three
    +    when 4..10 then func_other
    +    end
    +  else                          # 1
    +    do_something until a && b   # 2
    +  end                           # ===
    +end                             # 7 complexity points
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    Max

    8

    Integer

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_migration.html b/docs/rubocop/1.69/cops_migration.html new file mode 100644 index 000000000..a98e0110b --- /dev/null +++ b/docs/rubocop/1.69/cops_migration.html @@ -0,0 +1,696 @@ + + + + + + Migration :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Migration

    +
    +

    Migration/DepartmentName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.75

    -

    +
    +

    Check that cop names in rubocop:disable comments are given with +department name.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_naming.html b/docs/rubocop/1.69/cops_naming.html new file mode 100644 index 000000000..ea8b5b441 --- /dev/null +++ b/docs/rubocop/1.69/cops_naming.html @@ -0,0 +1,3028 @@ + + + + + + Naming :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Naming

    +
    +

    Naming/AccessorMethodName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Makes sure that accessor methods are named properly. Applies +to both instance and class methods.

    +
    +
    + + + + + +
    + + +Offenses are only registered for methods with the expected +arity. Getters (get_attribute) must have no arguments to be +registered, and setters (set_attribute(value)) must have exactly +one. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def set_attribute(value)
    +end
    +
    +# good
    +def attribute=(value)
    +end
    +
    +# bad
    +def get_attribute
    +end
    +
    +# good
    +def attribute
    +end
    +
    +# accepted, incorrect arity for getter
    +def get_value(attr)
    +end
    +
    +# accepted, incorrect arity for setter
    +def set_value
    +end
    +
    +
    +
    + +
    +
    +
    +

    Naming/AsciiIdentifiers

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    0.87

    +
    +

    Checks for non-ascii characters in identifier and constant names. +Identifiers are always checked and whether constants are checked +can be controlled using AsciiConstants config.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def καλημερα # Greek alphabet (non-ascii)
    +end
    +
    +# bad
    +def こんにちはと言う # Japanese character (non-ascii)
    +end
    +
    +# bad
    +def hello_🍣 # Emoji (non-ascii)
    +end
    +
    +# good
    +def say_hello
    +end
    +
    +# bad
    +신장 = 10 # Hangul character (non-ascii)
    +
    +# good
    +height = 10
    +
    +# bad
    +params[:عرض_gteq] # Arabic character (non-ascii)
    +
    +# good
    +params[:width_gteq]
    +
    +
    +
    +

    AsciiConstants: true (default)

    +
    +
    +
    # bad
    +class Foö
    +end
    +
    +FOÖ = "foo"
    +
    +
    +
    +
    +

    AsciiConstants: false

    +
    +
    +
    # good
    +class Foö
    +end
    +
    +FOÖ = "foo"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AsciiConstants

    true

    Boolean

    +
    + +
    +
    +
    +

    Naming/BinaryOperatorParameterName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    1.2

    +
    +

    Makes sure that certain binary operator methods have their +sole parameter named other.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def +(amount); end
    +
    +# good
    +def +(other); end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/BlockForwarding

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.1 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.24

    -

    +
    +

    In Ruby 3.1, anonymous block forwarding has been added.

    +
    +
    +

    This cop identifies places where do_something(&block) can be replaced +by do_something(&).

    +
    +
    +

    It also supports the opposite style by alternative explicit option. +You can specify the block variable name for autocorrection with BlockForwardingName. +The default variable name is block. If the name is already in use, it will not be +autocorrected.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: anonymous (default)

    +
    +
    +
    # bad
    +def foo(&block)
    +  bar(&block)
    +end
    +
    +# good
    +def foo(&)
    +  bar(&)
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: explicit

    +
    +
    +
    # bad
    +def foo(&)
    +  bar(&)
    +end
    +
    +# good
    +def foo(&block)
    +  bar(&block)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    anonymous

    anonymous, explicit

    BlockForwardingName

    block

    String

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/BlockParameterName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.53

    0.77

    +
    +

    Checks block parameter names for how descriptive they +are. It is highly configurable.

    +
    +
    +

    The MinNameLength config option takes an integer. It represents +the minimum amount of characters the name must be. Its default is 1. +The AllowNamesEndingInNumbers config option takes a boolean. When +set to false, this cop will register offenses for names ending with +numbers. Its default is false. The AllowedNames config option +takes an array of permitted names that will never register an +offense. The ForbiddenNames config option takes an array of +restricted names that will always register an offense.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +bar do |varOne, varTwo|
    +  varOne + varTwo
    +end
    +
    +# With `AllowNamesEndingInNumbers` set to false
    +foo { |num1, num2| num1 * num2 }
    +
    +# With `MinNameLength` set to number greater than 1
    +baz { |a, b, c| do_stuff(a, b, c) }
    +
    +# good
    +bar do |thud, fred|
    +  thud + fred
    +end
    +
    +foo { |speed, distance| speed * distance }
    +
    +baz { |age, height, gender| do_stuff(age, height, gender) }
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinNameLength

    1

    Integer

    AllowNamesEndingInNumbers

    true

    Boolean

    AllowedNames

    []

    Array

    ForbiddenNames

    []

    Array

    +
    +
    +
    +
    +

    Naming/ClassAndModuleCamelCase

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    0.85

    +
    +

    Checks for class and module names with +an underscore in them.

    +
    +
    +

    AllowedNames config takes an array of permitted names. +Its default value is ['module_parent']. +These names can be full class/module names or part of the name. +eg. Adding my_class to the AllowedNames config will allow names like +my_class, my_class::User, App::my_class, App::my_class::User, etc.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class My_Class
    +end
    +module My_Module
    +end
    +
    +# good
    +class MyClass
    +end
    +module MyModule
    +end
    +class module_parent::MyModule
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedNames

    module_parent

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/ConstantName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Checks whether constant names are written using +SCREAMING_SNAKE_CASE.

    +
    +
    +

    To avoid false positives, it ignores cases in which we cannot know +for certain the type of value that would be assigned to a constant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +InchInCm = 2.54
    +INCHinCM = 2.54
    +Inch_In_Cm = 2.54
    +
    +# good
    +INCH_IN_CM = 2.54
    +
    +
    +
    + +
    +
    +
    +

    Naming/FileName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    1.23

    +
    +

    Makes sure that Ruby source files have snake_case +names. Ruby scripts (i.e. source files with a shebang in the +first line) are ignored.

    +
    +
    +

    The cop also ignores .gemspec files, because Bundler +recommends using dashes to separate namespaces in nested gems +(i.e. bundler-console becomes Bundler::Console). As such, the +gemspec is supposed to be named bundler-console.gemspec.

    +
    +
    +

    When ExpectMatchingDefinition (default: false) is true, the cop requires +each file to have a class, module or Struct defined in it that matches +the filename. This can be further configured using +CheckDefinitionPathHierarchy (default: true) to determine whether the +path should match the namespace of the above definition.

    +
    +
    +

    When IgnoreExecutableScripts (default: true) is true, files that start +with a shebang line are not considered by the cop.

    +
    +
    +

    When Regex is set, the cop will flag any filename that does not match +the regular expression.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +lib/layoutManager.rb
    +
    +anything/usingCamelCase
    +
    +# good
    +lib/layout_manager.rb
    +
    +anything/using_snake_case.rake
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Exclude

    Rakefile.rb

    Array

    ExpectMatchingDefinition

    false

    Boolean

    CheckDefinitionPathHierarchy

    true

    Boolean

    CheckDefinitionPathHierarchyRoots

    lib, spec, test, src

    Array

    Regex

    <none>

    IgnoreExecutableScripts

    true

    Boolean

    AllowedAcronyms

    CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/HeredocDelimiterCase

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    1.2

    +
    +

    Checks that your heredocs are using the configured case. +By default it is configured to enforce uppercase heredocs.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: uppercase (default)

    +
    +
    +
    # bad
    +<<-sql
    +  SELECT * FROM foo
    +sql
    +
    +# good
    +<<-SQL
    +  SELECT * FROM foo
    +SQL
    +
    +
    +
    +
    +

    EnforcedStyle: lowercase

    +
    +
    +
    # bad
    +<<-SQL
    +  SELECT * FROM foo
    +SQL
    +
    +# good
    +<<-sql
    +  SELECT * FROM foo
    +sql
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    uppercase

    lowercase, uppercase

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/HeredocDelimiterNaming

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Checks that your heredocs are using meaningful delimiters. +By default it disallows END and EO*, and can be configured through +forbidden listing additional delimiters.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +<<-SQL
    +  SELECT * FROM foo
    +SQL
    +
    +# bad
    +<<-END
    +  SELECT * FROM foo
    +END
    +
    +# bad
    +<<-EOS
    +  SELECT * FROM foo
    +EOS
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    ForbiddenDelimiters

    (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$))

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/InclusiveLanguage

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    1.18

    1.49

    +
    +

    Recommends the use of inclusive language instead of problematic terms. +The cop can check the following locations for offenses:

    +
    +
    +
      +
    • +

      identifiers

      +
    • +
    • +

      constants

      +
    • +
    • +

      variables

      +
    • +
    • +

      strings

      +
    • +
    • +

      symbols

      +
    • +
    • +

      comments

      +
    • +
    • +

      file paths

      +
    • +
    +
    +
    +

    Each of these locations can be individually enabled/disabled via configuration, +for example CheckIdentifiers = true/false.

    +
    +
    +

    Flagged terms are configurable for the cop. For each flagged term an optional +Regex can be specified to identify offenses. Suggestions for replacing a flagged term can +be configured and will be displayed as part of the offense message. +An AllowedRegex can be specified for a flagged term to exempt allowed uses of the term. +WholeWord: true can be set on a flagged term to indicate the cop should only match when +a term matches the whole word (partial matches will not be offenses).

    +
    +
    +

    The cop supports autocorrection when there is only one suggestion. When there are multiple +suggestions, the best suggestion cannot be identified and will not be autocorrected.

    +
    +
    +

    Examples

    +
    +

    FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }

    +
    +
    +
    # Suggest replacing identifier whitelist with allowlist
    +
    +# bad
    +whitelist_users = %w(user1 user1)
    +
    +# good
    +allowlist_users = %w(user1 user2)
    +
    +
    +
    +
    +

    FlaggedTerms: { master: { Suggestions: ['main', 'primary', 'leader'] } }

    +
    +
    +
    # Suggest replacing master in an instance variable name with main, primary, or leader
    +
    +# bad
    +@master_node = 'node1.example.com'
    +
    +# good
    +@primary_node = 'node1.example.com'
    +
    +
    +
    +
    +

    FlaggedTerms: { whitelist: { Regex: !ruby/regexp '/white[-_\s]?list' } }

    +
    +
    +
    # Identify problematic terms using a Regexp
    +
    +# bad
    +white_list = %w(user1 user2)
    +
    +# good
    +allow_list = %w(user1 user2)
    +
    +
    +
    +
    +

    FlaggedTerms: { master: { AllowedRegex: 'master\'?s degree' } }

    +
    +
    +
    # Specify allowed uses of the flagged term as a string or regexp.
    +
    +# bad
    +# They had a masters
    +
    +# good
    +# They had a master's degree
    +
    +
    +
    +
    +

    FlaggedTerms: { slave: { WholeWord: true } }

    +
    +
    +
    # Specify that only terms that are full matches will be flagged.
    +
    +# bad
    +Slave
    +
    +# good (won't be flagged despite containing `slave`)
    +TeslaVehicle
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    CheckIdentifiers

    true

    Boolean

    CheckConstants

    true

    Boolean

    CheckVariables

    true

    Boolean

    CheckStrings

    false

    Boolean

    CheckSymbols

    true

    Boolean

    CheckComments

    true

    Boolean

    CheckFilepaths

    true

    Boolean

    FlaggedTerms

    {"whitelist"⇒{"Regex"⇒/white[-\s]?list/, "Suggestions"⇒["allowlist", "permit"]}, "blacklist"⇒{"Regex"⇒/black[-\s]?list/, "Suggestions"⇒["denylist", "block"]}, "slave"⇒{"WholeWord"⇒true, "Suggestions"⇒["replica", "secondary", "follower"]}}

    +
    +
    +
    +
    +

    Naming/MemoizedInstanceVariableName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.53

    1.2

    +
    +

    Checks for memoized methods whose instance variable name +does not match the method name. Applies to both regular methods +(defined with def) and dynamic methods (defined with +define_method or define_singleton_method).

    +
    +
    +

    This cop can be configured with the EnforcedStyleForLeadingUnderscores +directive. It can be configured to allow for memoized instance variables +prefixed with an underscore. Prefixing ivars with an underscore is a +convention that is used to implicitly indicate that an ivar should not +be set or referenced outside of the memoization method.

    +
    +
    +

    Safety

    +
    +

    This cop relies on the pattern @instance_var ||= …​, +but this is sometimes used for other purposes than memoization +so this cop is considered unsafe. Also, its autocorrection is unsafe +because it may conflict with instance variable names already in use.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyleForLeadingUnderscores: disallowed (default)

    +
    +
    +
    # bad
    +# Method foo is memoized using an instance variable that is
    +# not `@foo`. This can cause confusion and bugs.
    +def foo
    +  @something ||= calculate_expensive_thing
    +end
    +
    +def foo
    +  return @something if defined?(@something)
    +  @something = calculate_expensive_thing
    +end
    +
    +# good
    +def _foo
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  @foo ||= begin
    +    calculate_expensive_thing
    +  end
    +end
    +
    +# good
    +def foo
    +  helper_variable = something_we_need_to_calculate_foo
    +  @foo ||= calculate_expensive_thing(helper_variable)
    +end
    +
    +# good
    +define_method(:foo) do
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +define_method(:foo) do
    +  return @foo if defined?(@foo)
    +  @foo = calculate_expensive_thing
    +end
    +
    +
    +
    +
    +

    EnforcedStyleForLeadingUnderscores: required

    +
    +
    +
    # bad
    +def foo
    +  @something ||= calculate_expensive_thing
    +end
    +
    +# bad
    +def foo
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +def foo
    +  return @foo if defined?(@foo)
    +  @foo = calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def _foo
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +def foo
    +  return @_foo if defined?(@_foo)
    +  @_foo = calculate_expensive_thing
    +end
    +
    +# good
    +define_method(:foo) do
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +define_method(:foo) do
    +  return @_foo if defined?(@_foo)
    +  @_foo = calculate_expensive_thing
    +end
    +
    +
    +
    +
    +

    EnforcedStyleForLeadingUnderscores :optional

    +
    +
    +
    # bad
    +def foo
    +  @something ||= calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def _foo
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +def foo
    +  return @_foo if defined?(@_foo)
    +  @_foo = calculate_expensive_thing
    +end
    +
    +# good
    +define_method(:foo) do
    +  @foo ||= calculate_expensive_thing
    +end
    +
    +# good
    +define_method(:foo) do
    +  @_foo ||= calculate_expensive_thing
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleForLeadingUnderscores

    disallowed

    disallowed, required, optional

    +
    +
    +
    +
    +

    Naming/MethodName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    -

    +
    +

    Makes sure that all methods use the configured style, +snake_case or camelCase, for their names.

    +
    +
    +

    This cop has AllowedPatterns configuration option.

    +
    +
    +
    +
    Naming/MethodName:
    +  AllowedPatterns:
    +    - '\AonSelectionBulkChange\z'
    +    - '\AonSelectionCleared\z'
    +
    +
    +
    +

    Method names matching patterns are always allowed.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: snake_case (default)

    +
    +
    +
    # bad
    +def fooBar; end
    +
    +# good
    +def foo_bar; end
    +
    +
    +
    +
    +

    EnforcedStyle: camelCase

    +
    +
    +
    # bad
    +def foo_bar; end
    +
    +# good
    +def fooBar; end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    snake_case

    snake_case, camelCase

    AllowedPatterns

    []

    Array

    +
    + +
    +
    +
    +

    Naming/MethodParameterName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.53

    0.77

    +
    +

    Checks method parameter names for how descriptive they +are. It is highly configurable.

    +
    +
    +

    The MinNameLength config option takes an integer. It represents +the minimum amount of characters the name must be. Its default is 3. +The AllowNamesEndingInNumbers config option takes a boolean. When +set to false, this cop will register offenses for names ending with +numbers. Its default is false. The AllowedNames config option +takes an array of permitted names that will never register an +offense. The ForbiddenNames config option takes an array of +restricted names that will always register an offense.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def bar(varOne, varTwo)
    +  varOne + varTwo
    +end
    +
    +# With `AllowNamesEndingInNumbers` set to false
    +def foo(num1, num2)
    +  num1 * num2
    +end
    +
    +# With `MinNameLength` set to number greater than 1
    +def baz(a, b, c)
    +  do_stuff(a, b, c)
    +end
    +
    +# good
    +def bar(thud, fred)
    +  thud + fred
    +end
    +
    +def foo(speed, distance)
    +  speed * distance
    +end
    +
    +def baz(age_a, height_b, gender_c)
    +  do_stuff(age_a, height_b, gender_c)
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinNameLength

    3

    Integer

    AllowNamesEndingInNumbers

    true

    Boolean

    AllowedNames

    as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to

    Array

    ForbiddenNames

    []

    Array

    +
    +
    +
    +
    +

    Naming/PredicateName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    0.77

    +
    +

    Checks that predicate method names end with a question mark and +do not start with a forbidden prefix.

    +
    +
    +

    A method is determined to be a predicate method if its name starts with +one of the prefixes listed in the NamePrefix configuration. The list +defaults to is_, has_, and have_ but may be overridden.

    +
    +
    +

    Predicate methods must end with a question mark.

    +
    +
    +

    When ForbiddenPrefixes is also set (as it is by default), predicate +methods which begin with a forbidden prefix are not allowed, even if +they end with a ?. These methods should be changed to remove the +prefix.

    +
    +
    +

    Examples

    +
    +

    NamePrefix: ['is_', 'has_', 'have_'] (default)

    +
    +
    +
    # bad
    +def is_even(value)
    +end
    +
    +# When ForbiddenPrefixes: ['is_', 'has_', 'have_'] (default)
    +# good
    +def even?(value)
    +end
    +
    +# When ForbiddenPrefixes: []
    +# good
    +def is_even?(value)
    +end
    +
    +
    +
    +
    +

    NamePrefix: ['seems_to_be_']

    +
    +
    +
    # bad
    +def seems_to_be_even(value)
    +end
    +
    +# When ForbiddenPrefixes: ['seems_to_be_']
    +# good
    +def even?(value)
    +end
    +
    +# When ForbiddenPrefixes: []
    +# good
    +def seems_to_be_even?(value)
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['is_a?'] (default)

    +
    +
    +
    # Despite starting with the `is_` prefix, this method is allowed
    +# good
    +def is_a?(value)
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['is_even?']

    +
    +
    +
    # good
    +def is_even?(value)
    +end
    +
    +
    +
    +
    +

    MethodDefinitionMacros: ['define_method', 'define_singleton_method'] (default)

    +
    +
    +
    # bad
    +define_method(:is_even) { |value| }
    +
    +# good
    +define_method(:even?) { |value| }
    +
    +
    +
    +
    +

    MethodDefinitionMacros: ['def_node_matcher']

    +
    +
    +
    # bad
    +def_node_matcher(:is_even) { |value| }
    +
    +# good
    +def_node_matcher(:even?) { |value| }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    NamePrefix

    is_, has_, have_

    Array

    ForbiddenPrefixes

    is_, has_, have_

    Array

    AllowedMethods

    is_a?

    Array

    MethodDefinitionMacros

    define_method, define_singleton_method

    Array

    Exclude

    spec/**/*

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Naming/RescuedExceptionsVariableName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.67

    0.68

    +
    +

    Makes sure that rescued exceptions variables are named as +expected.

    +
    +
    +

    The PreferredName config option takes a String. It represents +the required name of the variable. Its default is e.

    +
    +
    + + + + + +
    + + +This cop does not consider nested rescues because it cannot +guarantee that the variable from the outer rescue is not used within +the inner rescue (in which case, changing the inner variable would +shadow the outer variable). +
    +
    +
    +

    Examples

    +
    +

    PreferredName: e (default)

    +
    +
    +
    # bad
    +begin
    +  # do something
    +rescue MyException => exception
    +  # do something
    +end
    +
    +# good
    +begin
    +  # do something
    +rescue MyException => e
    +  # do something
    +end
    +
    +# good
    +begin
    +  # do something
    +rescue MyException => _e
    +  # do something
    +end
    +
    +
    +
    +
    +

    PreferredName: exception

    +
    +
    +
    # bad
    +begin
    +  # do something
    +rescue MyException => e
    +  # do something
    +end
    +
    +# good
    +begin
    +  # do something
    +rescue MyException => exception
    +  # do something
    +end
    +
    +# good
    +begin
    +  # do something
    +rescue MyException => _exception
    +  # do something
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    PreferredName

    e

    String

    +
    +
    +
    +
    +

    Naming/VariableName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    1.8

    +
    +

    Makes sure that all variables use the configured style, +snake_case or camelCase, for their names.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: snake_case (default)

    +
    +
    +
    # bad
    +fooBar = 1
    +
    +# good
    +foo_bar = 1
    +
    +
    +
    +
    +

    EnforcedStyle: camelCase

    +
    +
    +
    # bad
    +foo_bar = 1
    +
    +# good
    +fooBar = 1
    +
    +
    +
    +
    +

    AllowedIdentifiers: ['fooBar']

    +
    +
    +
    # good (with EnforcedStyle: snake_case)
    +fooBar = 1
    +
    +
    +
    +
    +

    AllowedPatterns: ['_v\d+\z']

    +
    +
    +
    # good (with EnforcedStyle: camelCase)
    +:release_v1
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    snake_case

    snake_case, camelCase

    AllowedIdentifiers

    []

    Array

    AllowedPatterns

    []

    Array

    +
    + +
    +
    +
    +

    Naming/VariableNumber

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.50

    1.4

    +
    +

    Makes sure that all numbered variables use the +configured style, snake_case, normalcase, or non_integer, +for their numbering.

    +
    +
    +

    Additionally, CheckMethodNames and CheckSymbols configuration options +can be used to specify whether method names and symbols should be checked. +Both are enabled by default.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: normalcase (default)

    +
    +
    +
    # bad
    +:some_sym_1
    +variable_1 = 1
    +
    +def some_method_1; end
    +
    +def some_method1(arg_1); end
    +
    +# good
    +:some_sym1
    +variable1 = 1
    +
    +def some_method1; end
    +
    +def some_method1(arg1); end
    +
    +
    +
    +
    +

    EnforcedStyle: snake_case

    +
    +
    +
    # bad
    +:some_sym1
    +variable1 = 1
    +
    +def some_method1; end
    +
    +def some_method_1(arg1); end
    +
    +# good
    +:some_sym_1
    +variable_1 = 1
    +
    +def some_method_1; end
    +
    +def some_method_1(arg_1); end
    +
    +
    +
    +
    +

    EnforcedStyle: non_integer

    +
    +
    +
    # bad
    +:some_sym1
    +:some_sym_1
    +
    +variable1 = 1
    +variable_1 = 1
    +
    +def some_method1; end
    +
    +def some_method_1; end
    +
    +def some_methodone(arg1); end
    +def some_methodone(arg_1); end
    +
    +# good
    +:some_symone
    +:some_sym_one
    +
    +variableone = 1
    +variable_one = 1
    +
    +def some_methodone; end
    +
    +def some_method_one; end
    +
    +def some_methodone(argone); end
    +def some_methodone(arg_one); end
    +
    +# In the following examples, we assume `EnforcedStyle: normalcase` (default).
    +
    +
    +
    +
    +

    CheckMethodNames: true (default)

    +
    +
    +
    # bad
    +def some_method_1; end
    +
    +
    +
    +
    +

    CheckMethodNames: false

    +
    +
    +
    # good
    +def some_method_1; end
    +
    +
    +
    +
    +

    CheckSymbols: true (default)

    +
    +
    +
    # bad
    +:some_sym_1
    +
    +
    +
    +
    +

    CheckSymbols: false

    +
    +
    +
    # good
    +:some_sym_1
    +
    +
    +
    +
    +

    AllowedIdentifiers: [capture3]

    +
    +
    +
    # good
    +expect(Open3).to receive(:capture3)
    +
    +
    +
    +
    +

    AllowedPatterns: ['_v\d+\z']

    +
    +
    +
    # good
    +:some_sym_v1
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    normalcase

    snake_case, normalcase, non_integer

    CheckMethodNames

    true

    Boolean

    CheckSymbols

    true

    Boolean

    AllowedIdentifiers

    capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64

    Array

    AllowedPatterns

    []

    Array

    +
    + +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_security.html b/docs/rubocop/1.69/cops_security.html new file mode 100644 index 000000000..7c9747c5d --- /dev/null +++ b/docs/rubocop/1.69/cops_security.html @@ -0,0 +1,1136 @@ + + + + + + Security :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Security

    +
    +

    Security/CompoundHash

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    No

    1.28

    1.51

    +
    +

    Checks for implementations of the hash method which combine +values using custom logic instead of delegating to Array#hash.

    +
    +
    +

    Manually combining hashes is error prone and hard to follow, especially +when there are many values. Poor implementations may also introduce +performance or security concerns if they are prone to collisions. +Delegating to Array#hash is clearer and safer, although it might be slower +depending on the use case.

    +
    +
    +

    Safety

    +
    +

    This cop may be unsafe if the application logic depends on the hash +value, however this is inadvisable anyway.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def hash
    +  @foo ^ @bar
    +end
    +
    +# good
    +def hash
    +  [@foo, @bar].hash
    +end
    +
    +
    +
    +
    +
    +
    +

    Security/Eval

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.47

    -

    +
    +

    Checks for the use of Kernel#eval and Binding#eval.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +eval(something)
    +binding.eval(something)
    +
    +
    +
    +
    +
    +
    +

    Security/IoMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.22

    -

    +
    +

    Checks for the first argument to IO.read, IO.binread, IO.write, IO.binwrite, +IO.foreach, and IO.readlines.

    +
    +
    +

    If argument starts with a pipe character ('|') and the receiver is the IO class, +a subprocess is created in the same way as Kernel#open, and its output is returned. +Kernel#open may allow unintentional command injection, which is the reason these +IO methods are a security risk. +Consider to use File.read to disable the behavior of subprocess invocation.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because false positive will occur if the variable passed as +the first argument is a command that is not a file path.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +IO.read(path)
    +IO.read('path')
    +
    +# good
    +File.read(path)
    +File.read('path')
    +IO.read('| command') # Allow intentional command invocation.
    +
    +
    +
    +
    +
    +
    +

    Security/JSONLoad

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.43

    1.22

    +
    +

    Checks for the use of JSON class methods which have potential +security issues.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because it’s potentially dangerous. +If using a stream, like JSON.load(open('file')), it will need to call +#read manually, like JSON.parse(open('file').read). +If reading single values (rather than proper JSON objects), like +JSON.load('false'), it will need to pass the quirks_mode: true +option, like JSON.parse('false', quirks_mode: true). +Other similar issues may apply.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +JSON.load("{}")
    +JSON.restore("{}")
    +
    +# good
    +JSON.parse("{}")
    +
    +
    +
    + +
    +
    +
    +

    Security/MarshalLoad

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.47

    -

    +
    +

    Checks for the use of Marshal class methods which have +potential security issues leading to remote code execution when +loading from an untrusted source.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Marshal.load("{}")
    +Marshal.restore("{}")
    +
    +# good
    +Marshal.dump("{}")
    +
    +# okish - deep copy hack
    +Marshal.load(Marshal.dump({}))
    +
    +
    +
    + +
    +
    +
    +

    Security/Open

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.53

    1.0

    +
    +

    Checks for the use of Kernel#open and URI.open with dynamic +data.

    +
    +
    +

    Kernel#open and URI.open enable not only file access but also process +invocation by prefixing a pipe symbol (e.g., open("| ls")). +So, it may lead to a serious security risk by using variable input to +the argument of Kernel#open and URI.open. It would be better to use +File.open, IO.popen or URI.parse#open explicitly.

    +
    +
    + + + + + +
    + + +open and URI.open with literal strings are not flagged by this +cop. +
    +
    +
    +

    Safety

    +
    +

    This cop could register false positives if open is redefined +in a class and then used without a receiver in that class.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +open(something)
    +open("| #{something}")
    +open("| foo")
    +URI.open(something)
    +
    +# good
    +File.open(something)
    +IO.popen(something)
    +URI.parse(something).open
    +
    +# good (literal strings)
    +open("foo.text")
    +URI.open("http://example.com")
    +
    +
    +
    +
    +
    +
    +

    Security/YAMLLoad

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.47

    -

    +
    +

    Checks for the use of YAML class methods which have +potential security issues leading to remote code execution when +loading from an untrusted source.

    +
    +
    + + + + + +
    + + +Ruby 3.1+ (Psych 4) uses Psych.load as Psych.safe_load by default. +
    +
    +
    +

    Safety

    +
    +

    The behavior of the code might change depending on what was +in the YAML payload, since YAML.safe_load is more restrictive.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +YAML.load("--- !ruby/object:Foo {}") # Psych 3 is unsafe by default
    +
    +# good
    +YAML.safe_load("--- !ruby/object:Foo {}", [Foo])                    # Ruby 2.5  (Psych 3)
    +YAML.safe_load("--- !ruby/object:Foo {}", permitted_classes: [Foo]) # Ruby 3.0- (Psych 3)
    +YAML.load("--- !ruby/object:Foo {}", permitted_classes: [Foo])      # Ruby 3.1+ (Psych 4)
    +YAML.dump(foo)
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/cops_style.html b/docs/rubocop/1.69/cops_style.html new file mode 100644 index 000000000..19dc60d17 --- /dev/null +++ b/docs/rubocop/1.69/cops_style.html @@ -0,0 +1,28189 @@ + + + + + + Style :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Style

    +
    +

    Style/AccessModifierDeclarations

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.57

    0.81

    +
    +

    Access modifiers should be declared to apply to a group of methods +or inline before each method, depending on configuration. +EnforcedStyle config covers only method definitions. +Applications of visibility methods to symbols can be controlled +using AllowModifiersOnSymbols config. +Also, the visibility of attr* methods can be controlled using +AllowModifiersOnAttrs config.

    +
    +
    +

    In Ruby 3.0, attr* methods now return an array of defined method names +as symbols. So we can write the modifier and attr* in inline style. +AllowModifiersOnAttrs config allows attr* methods to be written in +inline style without modifying applications that have been maintained +for a long time in group style. Furthermore, developers who are not very +familiar with Ruby may know that the modifier applies to def, but they +may not know that it also applies to attr* methods. It would be easier +to understand if we could write attr* methods in inline style.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is not safe, because the visibility of dynamically +defined methods can vary depending on the state determined by +the group access modifier.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: group (default)

    +
    +
    +
    # bad
    +class Foo
    +
    +  private def bar; end
    +  private def baz; end
    +
    +end
    +
    +# good
    +class Foo
    +
    +  private
    +
    +  def bar; end
    +  def baz; end
    +
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: inline

    +
    +
    +
    # bad
    +class Foo
    +
    +  private
    +
    +  def bar; end
    +  def baz; end
    +
    +end
    +
    +# good
    +class Foo
    +
    +  private def bar; end
    +  private def baz; end
    +
    +end
    +
    +
    +
    +
    +

    AllowModifiersOnSymbols: true (default)

    +
    +
    +
    # good
    +class Foo
    +
    +  private :bar, :baz
    +  private *%i[qux quux]
    +  private *METHOD_NAMES
    +  private *private_methods
    +
    +end
    +
    +
    +
    +
    +

    AllowModifiersOnSymbols: false

    +
    +
    +
    # bad
    +class Foo
    +
    +  private :bar, :baz
    +  private *%i[qux quux]
    +  private *METHOD_NAMES
    +  private *private_methods
    +
    +end
    +
    +
    +
    +
    +

    AllowModifiersOnAttrs: true (default)

    +
    +
    +
    # good
    +class Foo
    +
    +  public attr_reader :bar
    +  protected attr_writer :baz
    +  private attr_accessor :qux
    +  private attr :quux
    +
    +  def public_method; end
    +
    +  private
    +
    +  def private_method; end
    +
    +end
    +
    +
    +
    +
    +

    AllowModifiersOnAttrs: false

    +
    +
    +
    # bad
    +class Foo
    +
    +  public attr_reader :bar
    +  protected attr_writer :baz
    +  private attr_accessor :qux
    +  private attr :quux
    +
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    group

    inline, group

    AllowModifiersOnSymbols

    true

    Boolean

    AllowModifiersOnAttrs

    true

    Boolean

    +
    +
    +
    +
    +

    Style/AccessorGrouping

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.87

    -

    +
    +

    Checks for grouping of accessors in class and module bodies. +By default it enforces accessors to be placed in grouped declarations, +but it can be configured to enforce separating them in multiple declarations.

    +
    +
    + + + + + +
    + + +If there is a method call before the accessor method it is always allowed +as it might be intended like Sorbet. +
    +
    +
    + + + + + +
    + + +If there is a RBS::Inline annotation comment just after the accessor method +it is always allowed. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: grouped (default)

    +
    +
    +
    # bad
    +class Foo
    +  attr_reader :bar
    +  attr_reader :bax
    +  attr_reader :baz
    +end
    +
    +# good
    +class Foo
    +  attr_reader :bar, :bax, :baz
    +end
    +
    +# good
    +class Foo
    +  # may be intended comment for bar.
    +  attr_reader :bar
    +
    +  sig { returns(String) }
    +  attr_reader :bax
    +
    +  may_be_intended_annotation :baz
    +  attr_reader :baz
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: separated

    +
    +
    +
    # bad
    +class Foo
    +  attr_reader :bar, :baz
    +end
    +
    +# good
    +class Foo
    +  attr_reader :bar
    +  attr_reader :baz
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    grouped

    separated, grouped

    +
    +
    +
    +
    +

    Style/Alias

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.36

    +
    +

    Enforces the use of either #alias or #alias_method +depending on configuration. +It also flags uses of alias :symbol rather than alias bareword.

    +
    +
    +

    However, it will always enforce method_alias when used alias +in an instance method definition and in a singleton method definition. +If used in a block, always enforce alias_method +unless it is an instance_eval block.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: prefer_alias (default)

    +
    +
    +
    # bad
    +alias_method :bar, :foo
    +alias :bar :foo
    +
    +# good
    +alias bar foo
    +
    +
    +
    +
    +

    EnforcedStyle: prefer_alias_method

    +
    +
    +
    # bad
    +alias :bar :foo
    +alias bar foo
    +
    +# good
    +alias_method :bar, :foo
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    prefer_alias

    prefer_alias, prefer_alias_method

    +
    + +
    +
    +
    +

    Style/AmbiguousEndlessMethodDefinition

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.68

    -

    +
    +

    Looks for endless methods inside operations of lower precedence (and, or, and +modifier forms of if, unless, while, until) that are ambiguous due to +lack of parentheses. This may lead to unexpected behavior as the code may appear +to use these keywords as part of the method but in fact they modify +the method definition itself.

    +
    +
    +

    In these cases, using a normal method definition is more clear.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo = true if bar
    +
    +# good - using a non-endless method is more explicit
    +def foo
    +  true
    +end if bar
    +
    +# ok - method body is explicit
    +def foo = (true if bar)
    +
    +# ok - method definition is explicit
    +(def foo = true) if bar
    +
    +
    +
    + +
    +
    +
    +

    Style/AndOr

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.9

    1.21

    +
    +

    Checks for uses of and and or, and suggests using && and +|| instead. It can be configured to check only in conditions or in +all contexts.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because there is a different operator precedence +between logical operators (&& and ||) and semantic operators (and and or), +and that might change the behavior.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: conditionals (default)

    +
    +
    +
    # bad
    +if foo and bar
    +end
    +
    +# good
    +foo.save && return
    +
    +# good
    +foo.save and return
    +
    +# good
    +if foo && bar
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: always

    +
    +
    +
    # bad
    +foo.save and return
    +
    +# bad
    +if foo and bar
    +end
    +
    +# good
    +foo.save && return
    +
    +# good
    +if foo && bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    conditionals

    always, conditionals

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ArgumentsForwarding

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.1

    1.58

    +
    +

    In Ruby 2.7, arguments forwarding has been added.

    +
    +
    +

    This cop identifies places where do_something(*args, &block) +can be replaced by do_something(…​).

    +
    +
    +

    In Ruby 3.1, anonymous block forwarding has been added.

    +
    +
    +

    This cop identifies places where do_something(&block) can be replaced +by do_something(&); if desired, this functionality can be disabled +by setting UseAnonymousForwarding: false.

    +
    +
    +

    In Ruby 3.2, anonymous args/kwargs forwarding has been added.

    +
    +
    +

    This cop also identifies places where use_args(args)/use_kwargs(kwargs) can be +replaced by use_args()/use_kwargs(); if desired, this functionality can be disabled +by setting UseAnonymousForwarding: false.

    +
    +
    +

    And this cop has RedundantRestArgumentNames, RedundantKeywordRestArgumentNames, +and RedundantBlockArgumentNames options. This configuration is a list of redundant names +that are sufficient for anonymizing meaningless naming.

    +
    +
    +

    Meaningless names that are commonly used can be anonymized by default: +e.g., args, *options, &block, and so on.

    +
    +
    +

    Names not on this list are likely to be meaningful and are allowed by default.

    +
    +
    +

    This cop handles not only method forwarding but also forwarding to super.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo(*args, &block)
    +  bar(*args, &block)
    +end
    +
    +# bad
    +def foo(*args, **kwargs, &block)
    +  bar(*args, **kwargs, &block)
    +end
    +
    +# good
    +def foo(...)
    +  bar(...)
    +end
    +
    +
    +
    +

    UseAnonymousForwarding: true (default, only relevant for Ruby >= 3.2)

    +
    +
    +
    # bad
    +def foo(*args, **kwargs, &block)
    +  args_only(*args)
    +  kwargs_only(**kwargs)
    +  block_only(&block)
    +end
    +
    +# good
    +def foo(*, **, &)
    +  args_only(*)
    +  kwargs_only(**)
    +  block_only(&)
    +end
    +
    +
    +
    +
    +

    UseAnonymousForwarding: false (only relevant for Ruby >= 3.2)

    +
    +
    +
    # good
    +def foo(*args, **kwargs, &block)
    +  args_only(*args)
    +  kwargs_only(**kwargs)
    +  block_only(&block)
    +end
    +
    +
    +
    +
    +

    AllowOnlyRestArgument: true (default, only relevant for Ruby < 3.2)

    +
    +
    +
    # good
    +def foo(*args)
    +  bar(*args)
    +end
    +
    +def foo(**kwargs)
    +  bar(**kwargs)
    +end
    +
    +
    +
    +
    +

    AllowOnlyRestArgument: false (only relevant for Ruby < 3.2)

    +
    +
    +
    # bad
    +# The following code can replace the arguments with `...`,
    +# but it will change the behavior. Because `...` forwards block also.
    +def foo(*args)
    +  bar(*args)
    +end
    +
    +def foo(**kwargs)
    +  bar(**kwargs)
    +end
    +
    +
    +
    +
    +

    RedundantRestArgumentNames: ['args', 'arguments'] (default)

    +
    +
    +
    # bad
    +def foo(*args)
    +  bar(*args)
    +end
    +
    +# good
    +def foo(*)
    +  bar(*)
    +end
    +
    +
    +
    +
    +

    RedundantKeywordRestArgumentNames: ['kwargs', 'options', 'opts'] (default)

    +
    +
    +
    # bad
    +def foo(**kwargs)
    +  bar(**kwargs)
    +end
    +
    +# good
    +def foo(**)
    +  bar(**)
    +end
    +
    +
    +
    +
    +

    RedundantBlockArgumentNames: ['blk', 'block', 'proc'] (default)

    +
    +
    +
    # bad - But it is good with `EnforcedStyle: explicit` set for `Naming/BlockForwarding`.
    +def foo(&block)
    +  bar(&block)
    +end
    +
    +# good
    +def foo(&)
    +  bar(&)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowOnlyRestArgument

    true

    Boolean

    UseAnonymousForwarding

    true

    Boolean

    RedundantRestArgumentNames

    args, arguments

    Array

    RedundantKeywordRestArgumentNames

    kwargs, options, opts

    Array

    RedundantBlockArgumentNames

    blk, block, proc

    Array

    +
    + +
    +
    +
    +

    Style/ArrayCoercion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    0.88

    -

    +
    +

    Enforces the use of Array() instead of explicit Array check or [*var].

    +
    +
    +

    The cop is disabled by default due to safety concerns.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because a false positive may occur if +the argument of Array() is (or could be) nil or depending +on how the argument is handled by Array() (which can be +different than just wrapping the argument in an array).

    +
    +
    +

    For example:

    +
    +
    +
    +
    [nil]             #=> [nil]
    +Array(nil)        #=> []
    +
    +[{a: 'b'}]        #= [{a: 'b'}]
    +Array({a: 'b'})   #=> [[:a, 'b']]
    +
    +[Time.now]        #=> [#<Time ...>]
    +Array(Time.now)   #=> [14, 16, 14, 16, 9, 2021, 4, 259, true, "EDT"]
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +paths = [paths] unless paths.is_a?(Array)
    +paths.each { |path| do_something(path) }
    +
    +# bad (always creates a new Array instance)
    +[*paths].each { |path| do_something(path) }
    +
    +# good (and a bit more readable)
    +Array(paths).each { |path| do_something(path) }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ArrayFirstLast

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    1.58

    -

    +
    +

    Identifies usages of arr[0] and arr[-1] and suggests to change +them to use arr.first and arr.last instead.

    +
    +
    +

    The cop is disabled by default due to safety concerns.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because [0] or [-1] can be called on a Hash, +which returns a value for 0 or -1 key, but changing these to use +.first or .last will return first/last tuple instead. Also, String +does not implement first/last methods.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +arr[0]
    +arr[-1]
    +
    +# good
    +arr.first
    +arr.last
    +arr[0] = 2
    +arr[0][-2]
    +
    +
    +
    +
    +

    References

    +
    +
      +
    • +

      #first-and-last

      +
    • +
    +
    +
    +
    +
    +
    +

    Style/ArrayIntersect

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.1 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.40

    -

    +
    +

    In Ruby 3.1, Array#intersect? has been added.

    +
    +
    +

    This cop identifies places where (array1 & array2).any? +can be replaced by array1.intersect?(array2).

    +
    +
    +

    The array1.intersect?(array2) method is faster than +(array1 & array2).any? and is more readable.

    +
    +
    +

    In cases like the following, compatibility is not ensured, +so it will not be detected when using block argument.

    +
    +
    +
    +
    ([1] & [1,2]).any? { |x| false }    # => false
    +[1].intersect?([1,2]) { |x| false } # => true
    +
    +
    +
    +

    Safety

    +
    +

    This cop cannot guarantee that array1 and array2 are +actually arrays while method intersect? is for arrays only.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(array1 & array2).any?
    +(array1 & array2).empty?
    +(array1 & array2).none?
    +
    +# good
    +array1.intersect?(array2)
    +!array1.intersect?(array2)
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: false (default)

    +
    +
    +
    # good
    +(array1 & array2).present?
    +(array1 & array2).blank?
    +
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: true

    +
    +
    +
    # bad
    +(array1 & array2).present?
    +(array1 & array2).blank?
    +
    +# good
    +array1.intersect?(array2)
    +!array1.intersect?(array2)
    +
    +
    +
    +
    +
    +
    +
    +

    Style/ArrayJoin

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.20

    0.31

    +
    +

    Checks for uses of "*" as a substitute for join.

    +
    +
    +

    Not all cases can reliably checked, due to Ruby’s dynamic +types, so we consider only cases when the first argument is an +array literal or the second is a string literal.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%w(foo bar baz) * ","
    +
    +# good
    +%w(foo bar baz).join(",")
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/AsciiComments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.9

    1.21

    +
    +

    Checks for non-ascii (non-English) characters +in comments. You could set an array of allowed non-ascii chars in +AllowedChars attribute (copyright notice "©" by default).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# Translates from English to 日本語。
    +
    +# good
    +# Translates from English to Japanese
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedChars

    ©

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Attr

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.12

    +
    +

    Checks for uses of Module#attr.

    +
    +
    +

    Examples

    +
    +
    +
    # bad - creates a single attribute accessor (deprecated in Ruby 1.9)
    +attr :something, true
    +attr :one, :two, :three # behaves as attr_reader
    +
    +# good
    +attr_accessor :something
    +attr_reader :one, :two, :three
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/AutoResourceCleanup

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.30

    -

    +
    +

    Checks for cases when you could use a block +accepting version of a method that does automatic +resource cleanup.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +f = File.open('file')
    +
    +# good
    +File.open('file') do |f|
    +  # ...
    +end
    +
    +# bad
    +f = Tempfile.open('temp')
    +
    +# good
    +Tempfile.open('temp') do |f|
    +  # ...
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/BarePercentLiterals

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.25

    -

    +
    +

    Checks if usage of %() or %Q() matches configuration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: bare_percent (default)

    +
    +
    +
    # bad
    +%Q(He said: "#{greeting}")
    +%q{She said: 'Hi'}
    +
    +# good
    +%(He said: "#{greeting}")
    +%{She said: 'Hi'}
    +
    +
    +
    +
    +

    EnforcedStyle: percent_q

    +
    +
    +
    # bad
    +%|He said: "#{greeting}"|
    +%/She said: 'Hi'/
    +
    +# good
    +%Q|He said: "#{greeting}"|
    +%q/She said: 'Hi'/
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    bare_percent

    percent_q, bare_percent

    +
    + +
    +
    +
    +

    Style/BeginBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    -

    +
    +

    Checks for BEGIN blocks.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +BEGIN { test }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/BisectedAttrAccessor

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.87

    -

    +
    +

    Checks for places where attr_reader and attr_writer +for the same method can be combined into single attr_accessor.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  attr_reader :bar
    +  attr_writer :bar
    +end
    +
    +# good
    +class Foo
    +  attr_accessor :bar
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/BitwisePredicate

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.5 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.68

    -

    +
    +

    Prefer bitwise predicate methods over direct comparison operations.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if the receiver +is not an Integer object.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad - checks any set bits
    +(variable & flags).positive?
    +
    +# good
    +variable.anybits?(flags)
    +
    +# bad - checks all set bits
    +(variable & flags) == flags
    +
    +# good
    +variable.allbits?(flags)
    +
    +# bad - checks no set bits
    +(variable & flags).zero?
    +
    +# good
    +variable.nobits?(flags)
    +
    +
    +
    + +
    +
    +
    +

    Style/BlockComments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.23

    +
    +

    Looks for uses of block comments (=begin…​=end).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +=begin
    +Multiple lines
    +of comments...
    +=end
    +
    +# good
    +# Multiple lines
    +# of comments...
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/BlockDelimiters

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.30

    0.35

    +
    +

    Check for uses of braces or do/end around single line or +multi-line blocks.

    +
    +
    +

    Methods that can be either procedural or functional and cannot be +categorised from their usage alone is ignored. +lambda, proc, and it are their defaults. +Additional methods can be added to the AllowedMethods.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: line_count_based (default)

    +
    +
    +
    # bad - single line block
    +items.each do |item| item / 5 end
    +
    +# good - single line block
    +items.each { |item| item / 5 }
    +
    +# bad - multi-line block
    +things.map { |thing|
    +  something = thing.some_method
    +  process(something)
    +}
    +
    +# good - multi-line block
    +things.map do |thing|
    +  something = thing.some_method
    +  process(something)
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: semantic

    +
    +
    +
    # Prefer `do...end` over `{...}` for procedural blocks.
    +
    +# return value is used/assigned
    +# bad
    +foo = map do |x|
    +  x
    +end
    +puts (map do |x|
    +  x
    +end)
    +
    +# return value is not used out of scope
    +# good
    +map do |x|
    +  x
    +end
    +
    +# Prefer `{...}` over `do...end` for functional blocks.
    +
    +# return value is not used out of scope
    +# bad
    +each { |x|
    +  x
    +}
    +
    +# return value is used/assigned
    +# good
    +foo = map { |x|
    +  x
    +}
    +map { |x|
    +  x
    +}.inspect
    +
    +# The AllowBracesOnProceduralOneLiners option is allowed unless the
    +# EnforcedStyle is set to `semantic`. If so:
    +
    +# If the AllowBracesOnProceduralOneLiners option is unspecified, or
    +# set to `false` or any other falsey value, then semantic purity is
    +# maintained, so one-line procedural blocks must use do-end, not
    +# braces.
    +
    +# bad
    +collection.each { |element| puts element }
    +
    +# good
    +collection.each do |element| puts element end
    +
    +# If the AllowBracesOnProceduralOneLiners option is set to `true`, or
    +# any other truthy value, then one-line procedural blocks may use
    +# either style. (There is no setting for requiring braces on them.)
    +
    +# good
    +collection.each { |element| puts element }
    +
    +# also good
    +collection.each do |element| puts element end
    +
    +
    +
    +
    +

    EnforcedStyle: braces_for_chaining

    +
    +
    +
    # bad
    +words.each do |word|
    +  word.flip.flop
    +end.join("-")
    +
    +# good
    +words.each { |word|
    +  word.flip.flop
    +}.join("-")
    +
    +
    +
    +
    +

    EnforcedStyle: always_braces

    +
    +
    +
    # bad
    +words.each do |word|
    +  word.flip.flop
    +end
    +
    +# good
    +words.each { |word|
    +  word.flip.flop
    +}
    +
    +
    +
    +
    +

    BracesRequiredMethods: ['sig']

    +
    +
    +
    # Methods listed in the BracesRequiredMethods list, such as 'sig'
    +# in this example, will require `{...}` braces. This option takes
    +# precedence over all other configurations except AllowedMethods.
    +
    +# bad
    +sig do
    +  params(
    +    foo: string,
    +  ).void
    +end
    +def bar(foo)
    +  puts foo
    +end
    +
    +# good
    +sig {
    +  params(
    +    foo: string,
    +  ).void
    +}
    +def bar(foo)
    +  puts foo
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['lambda', 'proc', 'it' ] (default)

    +
    +
    +
    # good
    +foo = lambda do |x|
    +  puts "Hello, #{x}"
    +end
    +
    +foo = lambda do |x|
    +  x * 100
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +things.map { |thing|
    +  something = thing.some_method
    +  process(something)
    +}
    +
    +
    +
    +
    +

    AllowedPatterns: ['map']

    +
    +
    +
    # good
    +things.map { |thing|
    +  something = thing.some_method
    +  process(something)
    +}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    line_count_based

    line_count_based, semantic, braces_for_chaining, always_braces

    ProceduralMethods

    benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object

    Array

    FunctionalMethods

    let, let!, subject, watch

    Array

    AllowedMethods

    lambda, proc, it

    Array

    AllowedPatterns

    []

    Array

    AllowBracesOnProceduralOneLiners

    false

    Boolean

    BracesRequiredMethods

    []

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CaseEquality

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.89

    +
    +

    If AllowOnSelfClass option is enabled, the cop will ignore violations when the receiver of +the case equality operator is self.class. Note intermediate variables are not accepted.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(1..100) === 7
    +/something/ === some_string
    +
    +# good
    +something.is_a?(Array)
    +(1..100).include?(7)
    +/something/.match?(some_string)
    +
    +
    +
    +

    AllowOnConstant: false (default)

    +
    +
    +
    # bad
    +Array === something
    +
    +
    +
    +
    +

    AllowOnConstant: true

    +
    +
    +
    # good
    +Array === something
    +
    +
    +
    +
    +

    AllowOnSelfClass: false (default)

    +
    +
    +
    # bad
    +self.class === something
    +
    +
    +
    +
    +

    AllowOnSelfClass: true

    +
    +
    +
    # good
    +self.class === something
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowOnConstant

    false

    Boolean

    AllowOnSelfClass

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CaseLikeIf

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.88

    1.48

    +
    +

    Identifies places where if-elsif constructions +can be replaced with case-when.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe. case statements use === for equality, +so if the original conditional used a different equality operator, the +behavior may be different.

    +
    +
    +
    +

    Examples

    +
    +

    MinBranchesCount: 3 (default)

    +
    +
    +
    # bad
    +if status == :active
    +  perform_action
    +elsif status == :inactive || status == :hibernating
    +  check_timeout
    +elsif status == :invalid
    +  report_invalid
    +else
    +  final_action
    +end
    +
    +# good
    +case status
    +when :active
    +  perform_action
    +when :inactive, :hibernating
    +  check_timeout
    +when :invalid
    +  report_invalid
    +else
    +  final_action
    +end
    +
    +
    +
    +
    +

    MinBranchesCount: 4

    +
    +
    +
    # good
    +if status == :active
    +  perform_action
    +elsif status == :inactive || status == :hibernating
    +  check_timeout
    +elsif status == :invalid
    +  report_invalid
    +else
    +  final_action
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinBranchesCount

    3

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CharacterLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    -

    +
    +

    Checks for uses of the character literal ?x. +Starting with Ruby 1.9 character literals are +essentially one-character strings, so this syntax +is mostly redundant at this point.

    +
    +
    +

    ? character literal can be used to express meta and control character. +That’s a good use case of ? literal so it doesn’t count it as an offense.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +?x
    +
    +# good
    +'x'
    +
    +# good - control & meta escapes
    +?\C-\M-d
    +"\C-\M-d" # same as above
    +
    +
    +
    + +
    +
    +
    +

    Style/ClassAndModuleChildren

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.19

    -

    +
    +

    Checks the style of children definitions at classes and +modules. Basically there are two different styles:

    +
    +
    +

    The compact style is only forced for classes/modules with one child.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe.

    +
    +
    +

    Moving from compact to nested children requires knowledge of whether the +outer parent is a module or a class. Moving from nested to compact requires +verification that the outer parent is defined elsewhere. RuboCop does not +have the knowledge to perform either operation safely and thus requires +manual oversight.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: nested (default)

    +
    +
    +
    # good
    +# have each child on its own line
    +class Foo
    +  class Bar
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: compact

    +
    +
    +
    # good
    +# combine definitions as much as possible
    +class Foo::Bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    nested

    nested, compact

    +
    + +
    +
    +
    +

    Style/ClassCheck

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.24

    -

    +
    +

    Enforces consistent use of Object#is_a? or Object#kind_of?.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: is_a? (default)

    +
    +
    +
    # bad
    +var.kind_of?(Date)
    +var.kind_of?(Integer)
    +
    +# good
    +var.is_a?(Date)
    +var.is_a?(Integer)
    +
    +
    +
    +
    +

    EnforcedStyle: kind_of?

    +
    +
    +
    # bad
    +var.is_a?(Time)
    +var.is_a?(String)
    +
    +# good
    +var.kind_of?(Time)
    +var.kind_of?(String)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    is_a?

    is_a?, kind_of?

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ClassEqualityComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.93

    1.57

    +
    +

    Enforces the use of Object#instance_of? instead of class comparison +for equality. +==, equal?, and eql? custom method definitions are allowed by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because there is no guarantee that +the constant Foo exists when autocorrecting var.class.name == 'Foo' to +var.instance_of?(Foo).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +var.class == Date
    +var.class.equal?(Date)
    +var.class.eql?(Date)
    +var.class.name == 'Date'
    +
    +# good
    +var.instance_of?(Date)
    +
    +
    +
    +

    AllowedMethods: ['==', 'equal?', 'eql?'] (default)

    +
    +
    +
    # good
    +def ==(other)
    +  self.class == other.class && name == other.name
    +end
    +
    +def equal?(other)
    +  self.class.equal?(other.class) && name.equal?(other.name)
    +end
    +
    +def eql?(other)
    +  self.class.eql?(other.class) && name.eql?(other.name)
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +def eq(other)
    +  self.class.eq(other.class) && name.eq(other.name)
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: ['eq']

    +
    +
    +
    # good
    +def eq(other)
    +  self.class.eq(other.class) && name.eq(other.name)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    ==, equal?, eql?

    Array

    AllowedPatterns

    []

    Array

    +
    + +
    +
    +
    +

    Style/ClassMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.20

    +
    +

    Checks for uses of the class/module name instead of +self, when defining class/module methods.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class SomeClass
    +  def SomeClass.class_method
    +    # ...
    +  end
    +end
    +
    +# good
    +class SomeClass
    +  def self.class_method
    +    # ...
    +  end
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/ClassMethodsDefinitions

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.89

    -

    +
    +

    Enforces using def self.method_name or class << self to define class methods.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: def_self (default)

    +
    +
    +
    # bad
    +class SomeClass
    +  class << self
    +    attr_accessor :class_accessor
    +
    +    def class_method
    +      # ...
    +    end
    +  end
    +end
    +
    +# good
    +class SomeClass
    +  def self.class_method
    +    # ...
    +  end
    +
    +  class << self
    +    attr_accessor :class_accessor
    +  end
    +end
    +
    +# good - contains private method
    +class SomeClass
    +  class << self
    +    attr_accessor :class_accessor
    +
    +    private
    +
    +    def private_class_method
    +      # ...
    +    end
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: self_class

    +
    +
    +
    # bad
    +class SomeClass
    +  def self.class_method
    +    # ...
    +  end
    +end
    +
    +# good
    +class SomeClass
    +  class << self
    +    def class_method
    +      # ...
    +    end
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    def_self

    def_self, self_class

    +
    + +
    +
    +
    +

    Style/ClassVars

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.13

    -

    +
    +

    Checks for uses of class variables. Offenses +are signaled only on assignment to class variables to +reduce the number of offenses that would be reported.

    +
    +
    +

    You have to be careful when setting a value for a class +variable; if a class has been inherited, changing the +value of a class variable also affects the inheriting +classes. This means that it’s almost always better to +use a class instance variable instead.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class A
    +  @@test = 10
    +end
    +
    +class A
    +  def self.test(name, value)
    +    class_variable_set("@@#{name}", value)
    +  end
    +end
    +
    +class A; end
    +A.class_variable_set(:@@test, 10)
    +
    +# good
    +class A
    +  @test = 10
    +end
    +
    +class A
    +  def test
    +    @@test # you can access class variable without offense
    +  end
    +end
    +
    +class A
    +  def self.test(name)
    +    class_variable_get("@@#{name}") # you can access without offense
    +  end
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CollectionCompact

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.2

    1.3

    +
    +

    Checks for places where custom logic on rejection nils from arrays +and hashes can be replaced with {Array,Hash}#{compact,compact!}.

    +
    +
    +

    Safety

    +
    +

    It is unsafe by default because false positives may occur in the +nil check of block arguments to the receiver object. Additionally, +we can’t know the type of the receiver object for sure, which may +result in false positives as well.

    +
    +
    +

    For example, [[1, 2], [3, nil]].reject { |first, second| second.nil? } +and [[1, 2], [3, nil]].compact are not compatible. This will work fine +when the receiver is a hash object.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array.reject(&:nil?)
    +array.reject { |e| e.nil? }
    +array.select { |e| !e.nil? }
    +array.filter { |e| !e.nil? }
    +array.grep_v(nil)
    +array.grep_v(NilClass)
    +
    +# good
    +array.compact
    +
    +# bad
    +hash.reject!(&:nil?)
    +hash.reject! { |k, v| v.nil? }
    +hash.select! { |k, v| !v.nil? }
    +hash.filter! { |k, v| !v.nil? }
    +
    +# good
    +hash.compact!
    +
    +
    +
    +

    AllowedReceivers: ['params']

    +
    +
    +
    # good
    +params.reject(&:nil?)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedReceivers

    []

    Array

    +
    +
    +
    +
    +

    Style/CollectionMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    0.9

    1.7

    +
    +

    Enforces the use of consistent method names +from the Enumerable module.

    +
    +
    +

    You can customize the mapping from undesired method to desired method.

    +
    +
    +

    e.g. to use detect over find:

    +
    +
    +
    +
    Style/CollectionMethods:
    +  PreferredMethods:
    +    find: detect
    +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it finds methods by name, without actually +being able to determine if the receiver is an Enumerable or not, so +this cop may register false positives.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # These examples are based on the default mapping for `PreferredMethods`.
    +
    +# bad
    +items.collect
    +items.collect!
    +items.collect_concat
    +items.inject
    +items.detect
    +items.find_all
    +items.member?
    +
    +# good
    +items.map
    +items.map!
    +items.flat_map
    +items.reduce
    +items.find
    +items.select
    +items.include?
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    PreferredMethods

    {"collect"⇒"map", "collect!"⇒"map!", "collect_concat"⇒"flat_map", "inject"⇒"reduce", "detect"⇒"find", "find_all"⇒"select", "member?"⇒"include?"}

    MethodsAcceptingSymbol

    inject, reduce

    Array

    +
    + +
    +
    +
    +

    Style/ColonMethodCall

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    -

    +
    +

    Checks for methods invoked via the :: operator instead +of the . operator (like FileUtils::rmdir instead of FileUtils.rmdir).

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Timeout::timeout(500) { do_something }
    +FileUtils::rmdir(dir)
    +Marshal::dump(obj)
    +
    +# good
    +Timeout.timeout(500) { do_something }
    +FileUtils.rmdir(dir)
    +Marshal.dump(obj)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ColonMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for class methods that are defined using the :: +operator instead of the . operator.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  def self::bar
    +  end
    +end
    +
    +# good
    +class Foo
    +  def self.bar
    +  end
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/CombinableDefined

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.68

    -

    +
    +

    Checks for multiple defined? calls joined by && that can be combined +into a single defined?.

    +
    +
    +

    When checking that a nested constant or chained method is defined, it is +not necessary to check each ancestor or component of the chain.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +defined?(Foo) && defined?(Foo::Bar) && defined?(Foo::Bar::Baz)
    +
    +# good
    +defined?(Foo::Bar::Baz)
    +
    +# bad
    +defined?(foo) && defined?(foo.bar) && defined?(foo.bar.baz)
    +
    +# good
    +defined?(foo.bar.baz)
    +
    +
    +
    +
    +
    +
    +

    Style/CombinableLoops

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.90

    -

    +
    +

    Checks for places where multiple consecutive loops over the same data +can be combined into a single loop. It is very likely that combining them +will make the code more efficient and more concise.

    +
    +
    + + + + + +
    + + +Autocorrection is not applied when the block variable names differ in separate loops, +as it is impossible to determine which variable name should be prioritized. +
    +
    +
    +

    Safety

    +
    +

    The cop is unsafe, because the first loop might modify state that the +second loop depends on; these two aren’t combinable.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def method
    +  items.each do |item|
    +    do_something(item)
    +  end
    +
    +  items.each do |item|
    +    do_something_else(item)
    +  end
    +end
    +
    +# good
    +def method
    +  items.each do |item|
    +    do_something(item)
    +    do_something_else(item)
    +  end
    +end
    +
    +# bad
    +def method
    +  for item in items do
    +    do_something(item)
    +  end
    +
    +  for item in items do
    +    do_something_else(item)
    +  end
    +end
    +
    +# good
    +def method
    +  for item in items do
    +    do_something(item)
    +    do_something_else(item)
    +  end
    +end
    +
    +# good
    +def method
    +  each_slice(2) { |slice| do_something(slice) }
    +  each_slice(3) { |slice| do_something(slice) }
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/CommandLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.30

    -

    +
    +

    Enforces using `` or %x around command literals.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: backticks (default)

    +
    +
    +
    # bad
    +folders = %x(find . -type d).split
    +
    +# bad
    +%x(
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +)
    +
    +# good
    +folders = `find . -type d`.split
    +
    +# good
    +`
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +`
    +
    +
    +
    +
    +

    EnforcedStyle: mixed

    +
    +
    +
    # bad
    +folders = %x(find . -type d).split
    +
    +# bad
    +`
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +`
    +
    +# good
    +folders = `find . -type d`.split
    +
    +# good
    +%x(
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +)
    +
    +
    +
    +
    +

    EnforcedStyle: percent_x

    +
    +
    +
    # bad
    +folders = `find . -type d`.split
    +
    +# bad
    +`
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +`
    +
    +# good
    +folders = %x(find . -type d).split
    +
    +# good
    +%x(
    +  ln -s foo.example.yml foo.example
    +  ln -s bar.example.yml bar.example
    +)
    +
    +
    +
    +
    +

    AllowInnerBackticks: false (default)

    +
    +
    +
    # If `false`, the cop will always recommend using `%x` if one or more
    +# backticks are found in the command string.
    +
    +# bad
    +`echo \`ls\``
    +
    +# good
    +%x(echo `ls`)
    +
    +
    +
    +
    +

    AllowInnerBackticks: true

    +
    +
    +
    # good
    +`echo \`ls\``
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    backticks

    backticks, percent_x, mixed

    AllowInnerBackticks

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CommentAnnotation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.10

    1.20

    +
    +

    Checks that comment annotation keywords are written according +to guidelines.

    +
    +
    +

    Annotation keywords can be specified by overriding the cop’s Keywords +configuration. Keywords are allowed to be single words or phrases.

    +
    +
    + + + + + +
    + + +With a multiline comment block (where each line is only a +comment), only the first line will be able to register an offense, even +if an annotation keyword starts another line. This is done to prevent +incorrect registering of keywords (eg. review) inside a paragraph as an +annotation. +
    +
    +
    +

    Examples

    +
    +

    RequireColon: true (default)

    +
    +
    +
    # bad
    +# TODO make better
    +
    +# good
    +# TODO: make better
    +
    +# bad
    +# TODO:make better
    +
    +# good
    +# TODO: make better
    +
    +# bad
    +# fixme: does not work
    +
    +# good
    +# FIXME: does not work
    +
    +# bad
    +# Optimize does not work
    +
    +# good
    +# OPTIMIZE: does not work
    +
    +
    +
    +
    +

    RequireColon: false

    +
    +
    +
    # bad
    +# TODO: make better
    +
    +# good
    +# TODO make better
    +
    +# bad
    +# fixme does not work
    +
    +# good
    +# FIXME does not work
    +
    +# bad
    +# Optimize does not work
    +
    +# good
    +# OPTIMIZE does not work
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Keywords

    TODO, FIXME, OPTIMIZE, HACK, REVIEW, NOTE

    Array

    RequireColon

    true

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/CommentedKeyword

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.51

    1.19

    +
    +

    Checks for comments put on the same line as some keywords. +These keywords are: class, module, def, begin, end.

    +
    +
    +

    Note that some comments +(:nodoc:, :yields:, rubocop:disable and rubocop:todo) +and RBS::Inline annotation comments are allowed.

    +
    +
    +

    Autocorrection removes comments from end keyword and keeps comments +for class, module, def and begin above the keyword.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because it may remove a comment that is +meaningful.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition
    +  statement
    +end # end if
    +
    +# bad
    +class X # comment
    +  statement
    +end
    +
    +# bad
    +def x; end # comment
    +
    +# good
    +if condition
    +  statement
    +end
    +
    +# good
    +class X # :nodoc:
    +  y
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/ComparableClamp

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.44

    -

    +
    +

    Enforces the use of Comparable#clamp instead of comparison by minimum and maximum.

    +
    +
    +

    This cop supports autocorrection for if/elsif/else bad style only. +Because ArgumentError occurs if the minimum and maximum of clamp arguments are reversed. +When these are variables, it is not possible to determine which is the minimum and maximum:

    +
    +
    +
    +
    [1, [2, 3].max].min # => 1
    +1.clamp(3, 1)       # => min argument must be smaller than max argument (ArgumentError)
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[[x, low].max, high].min
    +
    +# bad
    +if x < low
    +  low
    +elsif high < x
    +  high
    +else
    +  x
    +end
    +
    +# good
    +x.clamp(low, high)
    +
    +
    +
    +
    +
    +
    +

    Style/ConcatArrayLiterals

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.41

    -

    +
    +

    Enforces the use of Array#push(item) instead of Array#concat([item]) +to avoid redundant array literals.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if the receiver +is not an Array object.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +list.concat([foo])
    +list.concat([bar, baz])
    +list.concat([qux, quux], [corge])
    +
    +# good
    +list.push(foo)
    +list.push(bar, baz)
    +list.push(qux, quux, corge)
    +
    +
    +
    +
    +
    +
    +

    Style/ConditionalAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    0.47

    +
    +

    Check for if and case statements where each branch is used for +both the assignment and comparison of the same variable +when using the return of the condition can be used instead.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: assign_to_condition (default)

    +
    +
    +
    # bad
    +if foo
    +  bar = 1
    +else
    +  bar = 2
    +end
    +
    +case foo
    +when 'a'
    +  bar += 1
    +else
    +  bar += 2
    +end
    +
    +if foo
    +  some_method
    +  bar = 1
    +else
    +  some_other_method
    +  bar = 2
    +end
    +
    +# good
    +bar = if foo
    +        1
    +      else
    +        2
    +      end
    +
    +bar += case foo
    +       when 'a'
    +         1
    +       else
    +         2
    +       end
    +
    +bar << if foo
    +         some_method
    +         1
    +       else
    +         some_other_method
    +         2
    +       end
    +
    +
    +
    +
    +

    EnforcedStyle: assign_inside_condition

    +
    +
    +
    # bad
    +bar = if foo
    +        1
    +      else
    +        2
    +      end
    +
    +bar += case foo
    +       when 'a'
    +         1
    +       else
    +         2
    +       end
    +
    +bar << if foo
    +         some_method
    +         1
    +       else
    +         some_other_method
    +         2
    +       end
    +
    +# good
    +if foo
    +  bar = 1
    +else
    +  bar = 2
    +end
    +
    +case foo
    +when 'a'
    +  bar += 1
    +else
    +  bar += 2
    +end
    +
    +if foo
    +  some_method
    +  bar = 1
    +else
    +  some_other_method
    +  bar = 2
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    assign_to_condition

    assign_to_condition, assign_inside_condition

    SingleLineConditionsOnly

    true

    Boolean

    IncludeTernaryExpressions

    true

    Boolean

    +
    +
    +
    +
    +

    Style/ConstantVisibility

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.66

    1.10

    +
    +

    Checks that constants defined in classes and modules have +an explicit visibility declaration. By default, Ruby makes all class- +and module constants public, which litters the public API of the +class or module. Explicitly declaring a visibility makes intent more +clear, and prevents outside actors from touching private state.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo
    +  BAR = 42
    +  BAZ = 43
    +end
    +
    +# good
    +class Foo
    +  BAR = 42
    +  private_constant :BAR
    +
    +  BAZ = 43
    +  public_constant :BAZ
    +end
    +
    +
    +
    +

    IgnoreModules: false (default)

    +
    +
    +
    # bad
    +class Foo
    +  MyClass = Struct.new()
    +end
    +
    +# good
    +class Foo
    +  MyClass = Struct.new()
    +  public_constant :MyClass
    +end
    +
    +
    +
    +
    +

    IgnoreModules: true

    +
    +
    +
    # good
    +class Foo
    +  MyClass = Struct.new()
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IgnoreModules

    false

    Boolean

    +
    +
    +
    +
    +

    Style/Copyright

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.30

    -

    +
    +

    Check that a copyright notice was given in each source file.

    +
    +
    +

    The default regexp for an acceptable copyright notice can be found in +config/default.yml. The default can be changed as follows:

    +
    +
    +
    +
    Style/Copyright:
    +  Notice: '^Copyright (\(c\) )?2\d{3} Acme Inc'
    +
    +
    +
    +

    This regex string is treated as an unanchored regex. For each file +that RuboCop scans, a comment that matches this regex must be found or +an offense is reported.

    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Notice

    ^Copyright (\(c\) )?2[0-9]{3} .+

    String

    AutocorrectNotice

    ``

    String

    +
    +
    +
    +
    +

    Style/DataInheritance

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.2 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.49

    1.51

    +
    +

    Checks for inheritance from Data.define to avoid creating the anonymous parent class.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because it will change the inheritance +tree (e.g. return value of Module#ancestors) of the constant.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Person < Data.define(:first_name, :last_name)
    +  def age
    +    42
    +  end
    +end
    +
    +# good
    +Person = Data.define(:first_name, :last_name) do
    +  def age
    +    42
    +  end
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/DateTime

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always (Unsafe)

    0.51

    0.92

    +
    +

    Checks for consistent usage of the DateTime class over the +Time class. This cop is disabled by default since these classes, +although highly overlapping, have particularities that make them not +replaceable in certain situations when dealing with multiple timezones +and/or DST.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is not safe, because DateTime and Time do not have +exactly the same behavior, although in most cases the autocorrection +will be fine.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad - uses `DateTime` for current time
    +DateTime.now
    +
    +# good - uses `Time` for current time
    +Time.now
    +
    +# bad - uses `DateTime` for modern date
    +DateTime.iso8601('2016-06-29')
    +
    +# good - uses `Time` for modern date
    +Time.iso8601('2016-06-29')
    +
    +# good - uses `DateTime` with start argument for historical date
    +DateTime.iso8601('1751-04-23', Date::ENGLAND)
    +
    +
    +
    +

    AllowCoercion: false (default)

    +
    +
    +
    # bad - coerces to `DateTime`
    +something.to_datetime
    +
    +# good - coerces to `Time`
    +something.to_time
    +
    +
    +
    +
    +

    AllowCoercion: true

    +
    +
    +
    # good
    +something.to_datetime
    +
    +# good
    +something.to_time
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowCoercion

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/DefWithParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.12

    +
    +

    Checks for parentheses in the definition of a method, +that does not take any arguments. Both instance and +class/singleton methods are checked.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo()
    +  do_something
    +end
    +
    +# good
    +def foo
    +  do_something
    +end
    +
    +# bad
    +def foo() = do_something
    +
    +# good
    +def foo = do_something
    +
    +# good (without parentheses it's a syntax error)
    +def foo() do_something end
    +
    +# bad
    +def Baz.foo()
    +  do_something
    +end
    +
    +# good
    +def Baz.foo
    +  do_something
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/DigChain

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.69

    -

    +
    +

    Check for chained dig calls that can be collapsed into a single dig.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is an Enumerable or does not have a nonstandard implementation +of dig.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x.dig(:foo).dig(:bar).dig(:baz)
    +x.dig(:foo, :bar).dig(:baz)
    +x.dig(:foo, :bar)&.dig(:baz)
    +
    +# good
    +x.dig(:foo, :bar, :baz)
    +
    +# good - `dig`s cannot be combined
    +x.dig(:foo).bar.dig(:baz)
    +
    +
    +
    +
    +
    +
    +

    Style/Dir

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Checks for places where the #_\_dir\_\_ method can replace more +complex constructs to retrieve a canonicalized absolute path to the +current file.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +path = File.expand_path(File.dirname(__FILE__))
    +
    +# bad
    +path = File.dirname(File.realpath(__FILE__))
    +
    +# good
    +path = __dir__
    +
    +
    +
    +
    +
    +
    +

    Style/DirEmpty

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.48

    -

    +
    +

    Prefer to use Dir.empty?('path/to/dir') when checking if a directory is empty.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Dir.entries('path/to/dir').size == 2
    +Dir.children('path/to/dir').empty?
    +Dir.children('path/to/dir').size == 0
    +Dir.each_child('path/to/dir').none?
    +
    +# good
    +Dir.empty?('path/to/dir')
    +
    +
    +
    +
    +
    +
    +

    Style/DisableCopsWithinSourceCodeDirective

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.82

    1.9

    +
    +

    Detects comments to enable/disable RuboCop. +This is useful if want to make sure that every RuboCop error gets fixed +and not quickly disabled with a comment.

    +
    +
    +

    Specific cops can be allowed with the AllowedCops configuration. Note that +if this configuration is set, rubocop:disable all is still disallowed.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# rubocop:disable Metrics/AbcSize
    +def foo
    +end
    +# rubocop:enable Metrics/AbcSize
    +
    +# good
    +def foo
    +end
    +
    +
    +
    +

    AllowedCops: [Metrics/AbcSize]

    +
    +
    +
    # good
    +# rubocop:disable Metrics/AbcSize
    +def foo
    +end
    +# rubocop:enable Metrics/AbcSize
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedCops

    []

    Array

    +
    +
    +
    +
    +

    Style/DocumentDynamicEvalDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.1

    1.3

    +
    +

    When using class_eval (or other eval) with string interpolation, +add a comment block showing its appearance if interpolated (a practice used in Rails code).

    +
    +
    +

    Examples

    +
    +
    +
    # from activesupport/lib/active_support/core_ext/string/output_safety.rb
    +
    +# bad
    +UNSAFE_STRING_METHODS.each do |unsafe_method|
    +  if 'String'.respond_to?(unsafe_method)
    +    class_eval <<-EOT, __FILE__, __LINE__ + 1
    +      def #{unsafe_method}(*params, &block)
    +        to_str.#{unsafe_method}(*params, &block)
    +      end
    +
    +      def #{unsafe_method}!(*params)
    +        @dirty = true
    +        super
    +      end
    +    EOT
    +  end
    +end
    +
    +# good, inline comments in heredoc
    +UNSAFE_STRING_METHODS.each do |unsafe_method|
    +  if 'String'.respond_to?(unsafe_method)
    +    class_eval <<-EOT, __FILE__, __LINE__ + 1
    +      def #{unsafe_method}(*params, &block)       # def capitalize(*params, &block)
    +        to_str.#{unsafe_method}(*params, &block)  #   to_str.capitalize(*params, &block)
    +      end                                         # end
    +
    +      def #{unsafe_method}!(*params)              # def capitalize!(*params)
    +        @dirty = true                             #   @dirty = true
    +        super                                     #   super
    +      end                                         # end
    +    EOT
    +  end
    +end
    +
    +# good, block comments in heredoc
    +class_eval <<-EOT, __FILE__, __LINE__ + 1
    +  # def capitalize!(*params)
    +  #   @dirty = true
    +  #   super
    +  # end
    +
    +  def #{unsafe_method}!(*params)
    +    @dirty = true
    +    super
    +  end
    +EOT
    +
    +# good, block comments before heredoc
    +class_eval(
    +  # def capitalize!(*params)
    +  #   @dirty = true
    +  #   super
    +  # end
    +
    +  <<-EOT, __FILE__, __LINE__ + 1
    +    def #{unsafe_method}!(*params)
    +      @dirty = true
    +      super
    +    end
    +  EOT
    +)
    +
    +# bad - interpolated string without comment
    +class_eval("def #{unsafe_method}!(*params); end")
    +
    +# good - with inline comment or replace it with block comment using heredoc
    +class_eval("def #{unsafe_method}!(*params); end # def capitalize!(*params); end")
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Documentation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.9

    -

    +
    +

    Checks for missing top-level documentation of classes and +modules. Classes with no body are exempt from the check and so are +namespace modules - modules that have nothing in their bodies except +classes, other modules, constant definitions or constant visibility +declarations.

    +
    +
    +

    The documentation requirement is annulled if the class or module has +a :nodoc: comment next to it. Likewise, :nodoc: all does the +same for all its children.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Person
    +  # ...
    +end
    +
    +module Math
    +end
    +
    +# good
    +# Description/Explanation of Person class
    +class Person
    +  # ...
    +end
    +
    +# allowed
    +# Class without body
    +class Person
    +end
    +
    +# Namespace - A namespace can be a class or a module
    +# Containing a class
    +module Namespace
    +  # Description/Explanation of Person class
    +  class Person
    +    # ...
    +  end
    +end
    +
    +# Containing constant visibility declaration
    +module Namespace
    +  class Private
    +  end
    +
    +  private_constant :Private
    +end
    +
    +# Containing constant definition
    +module Namespace
    +  Public = Class.new
    +end
    +
    +# Macro calls
    +module Namespace
    +  extend Foo
    +end
    +
    +
    +
    +

    AllowedConstants: ['ClassMethods']

    +
    +
    +
    # good
    +module A
    +  module ClassMethods
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedConstants

    []

    Array

    Exclude

    spec/**/*, test/**/*

    Array

    +
    +
    +
    +
    +

    Style/DocumentationMethod

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.43

    -

    +
    +

    Checks for missing documentation comment for public methods. +It can optionally be configured to also require documentation for +non-public methods.

    +
    +
    + + + + + +
    + + +This cop allows initialize method because initialize is +a special method called from new. In some programming languages +they are called constructor to distinguish it from method. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +
    +class Foo
    +  def bar
    +    puts baz
    +  end
    +end
    +
    +module Foo
    +  def bar
    +    puts baz
    +  end
    +end
    +
    +def foo.bar
    +  puts baz
    +end
    +
    +# good
    +
    +class Foo
    +  # Documentation
    +  def bar
    +    puts baz
    +  end
    +end
    +
    +module Foo
    +  # Documentation
    +  def bar
    +    puts baz
    +  end
    +end
    +
    +# Documentation
    +def foo.bar
    +  puts baz
    +end
    +
    +
    +
    +

    RequireForNonPublicMethods: false (default)

    +
    +
    +
    # good
    +class Foo
    +  protected
    +  def do_something
    +  end
    +end
    +
    +class Foo
    +  private
    +  def do_something
    +  end
    +end
    +
    +
    +
    +
    +

    RequireForNonPublicMethods: true

    +
    +
    +
    # bad
    +class Foo
    +  protected
    +  def do_something
    +  end
    +end
    +
    +class Foo
    +  private
    +  def do_something
    +  end
    +end
    +
    +# good
    +class Foo
    +  protected
    +  # Documentation
    +  def do_something
    +  end
    +end
    +
    +class Foo
    +  private
    +  # Documentation
    +  def do_something
    +  end
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['method_missing', 'respond_to_missing?']

    +
    +
    +
    # good
    +class Foo
    +  def method_missing(name, *args)
    +  end
    +
    +  def respond_to_missing?(symbol, include_private)
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    Exclude

    spec/**/*, test/**/*

    Array

    RequireForNonPublicMethods

    false

    Boolean

    +
    +
    +
    +
    +

    Style/DoubleCopDisableDirective

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.73

    -

    +
    +

    Detects double disable comments on one line. This is mostly to catch +automatically generated comments that need to be regenerated.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def f # rubocop:disable Style/For # rubocop:disable Metrics/AbcSize
    +end
    +
    +# good
    +# rubocop:disable Metrics/AbcSize
    +def f # rubocop:disable Style/For
    +end
    +# rubocop:enable Metrics/AbcSize
    +
    +# if both fit on one line
    +def f # rubocop:disable Style/For, Metrics/AbcSize
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/DoubleNegation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.19

    1.2

    +
    +

    Checks for uses of double negation (!!) to convert something to a boolean value.

    +
    +
    +

    When using EnforcedStyle: allowed_in_returns, allow double negation in contexts +that use boolean as a return value. When using EnforcedStyle: forbidden, double negation +should be forbidden always.

    +
    +
    + + + + + +
    + + +when something is a boolean value +!!something and !something.nil? are not the same thing. +As you’re unlikely to write code that can accept values of any type +this is rarely a problem in practice. +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe when the value is false, because the result +of the expression will change.

    +
    +
    +
    +
    !!false     #=> false
    +!false.nil? #=> true
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +!!something
    +
    +# good
    +!something.nil?
    +
    +
    +
    +

    EnforcedStyle: allowed_in_returns (default)

    +
    +
    +
    # good
    +def foo?
    +  !!return_value
    +end
    +
    +define_method :foo? do
    +  !!return_value
    +end
    +
    +define_singleton_method :foo? do
    +  !!return_value
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: forbidden

    +
    +
    +
    # bad
    +def foo?
    +  !!return_value
    +end
    +
    +define_method :foo? do
    +  !!return_value
    +end
    +
    +define_singleton_method :foo? do
    +  !!return_value
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    allowed_in_returns

    allowed_in_returns, forbidden

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/EachForSimpleLoop

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.41

    -

    +
    +

    Checks for loops which iterate a constant number of times, +using a Range literal and #each. This can be done more readably using +Integer#times.

    +
    +
    +

    This check only applies if the block takes no parameters.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(1..5).each { }
    +
    +# good
    +5.times { }
    +
    +# bad
    +(0...10).each {}
    +
    +# good
    +10.times {}
    +
    +
    +
    +
    +
    +
    +

    Style/EachWithObject

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.22

    0.42

    +
    +

    Looks for inject / reduce calls where the passed in object is +returned at the end and so could be replaced by each_with_object without +the need to return the object at the end.

    +
    +
    +

    However, we can’t replace with each_with_object if the accumulator +parameter is assigned to within the block.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[1, 2].inject({}) { |a, e| a[e] = e; a }
    +
    +# good
    +[1, 2].each_with_object({}) { |e, a| a[e] = e }
    +
    +
    +
    +
    +
    +
    +

    Style/EmptyBlockParameter

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for pipes for empty block parameters. Pipes for empty +block parameters do not cause syntax errors, but they are redundant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a do ||
    +  do_something
    +end
    +
    +# bad
    +a { || do_something }
    +
    +# good
    +a do
    +end
    +
    +# good
    +a { do_something }
    +
    +
    +
    +
    +
    +
    +

    Style/EmptyCaseCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.40

    -

    +
    +

    Checks for case statements with an empty condition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad:
    +case
    +when x == 0
    +  puts 'x is 0'
    +when y == 0
    +  puts 'y is 0'
    +else
    +  puts 'neither is 0'
    +end
    +
    +# good:
    +if x == 0
    +  puts 'x is 0'
    +elsif y == 0
    +  puts 'y is 0'
    +else
    +  puts 'neither is 0'
    +end
    +
    +# good: (the case condition node is not empty)
    +case n
    +when 0
    +  puts 'zero'
    +when 1
    +  puts 'one'
    +else
    +  puts 'more'
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/EmptyElse

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.28

    1.61

    +
    +

    Checks for empty else-clauses, possibly including comments and/or an +explicit nil depending on the EnforcedStyle.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: both (default)

    +
    +
    +
    # warn on empty else and else with nil in it
    +
    +# bad
    +if condition
    +  statement
    +else
    +  nil
    +end
    +
    +# bad
    +if condition
    +  statement
    +else
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: empty

    +
    +
    +
    # warn only on empty else
    +
    +# bad
    +if condition
    +  statement
    +else
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  nil
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: nil

    +
    +
    +
    # warn on else with nil in it
    +
    +# bad
    +if condition
    +  statement
    +else
    +  nil
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +end
    +
    +
    +
    +
    +

    AllowComments: false (default)

    +
    +
    +
    # bad
    +if condition
    +  statement
    +else
    +  # something comment
    +  nil
    +end
    +
    +# bad
    +if condition
    +  statement
    +else
    +  # something comment
    +end
    +
    +
    +
    +
    +

    AllowComments: true

    +
    +
    +
    # good
    +if condition
    +  statement
    +else
    +  # something comment
    +  nil
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  # something comment
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    both

    empty, nil, both

    AllowComments

    false

    Boolean

    +
    +
    +
    +
    +

    Style/EmptyHeredoc

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Command-line only

    1.32

    1.61

    +
    +

    Checks for using empty heredoc to reduce redundancy.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +<<~EOS
    +EOS
    +
    +<<-EOS
    +EOS
    +
    +<<EOS
    +EOS
    +
    +# good
    +''
    +
    +# bad
    +do_something(<<~EOS)
    +EOS
    +
    +do_something(<<-EOS)
    +EOS
    +
    +do_something(<<EOS)
    +EOS
    +
    +# good
    +do_something('')
    +
    +
    +
    +
    +
    +
    +

    Style/EmptyLambdaParameter

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for parentheses for empty lambda parameters. Parentheses +for empty lambda parameters do not cause syntax errors, but they are +redundant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +-> () { do_something }
    +
    +# good
    +-> { do_something }
    +
    +# good
    +-> (arg) { do_something(arg) }
    +
    +
    +
    +
    +
    +
    +

    Style/EmptyLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.12

    +
    +

    Checks for the use of a method, the result of which +would be a literal, like an empty array, hash, or string.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a = Array.new
    +a = Array[]
    +h = Hash.new
    +h = Hash[]
    +s = String.new
    +
    +# good
    +a = []
    +h = {}
    +s = ''
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/EmptyMethod

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Command-line only

    0.46

    1.61

    +
    +

    Checks for the formatting of empty method definitions. +By default it enforces empty method definitions to go on a single +line (compact style), but it can be configured to enforce the end +to go on its own line (expanded style).

    +
    +
    + + + + + +
    + + +A method definition is not considered empty if it contains +comments. +
    +
    +
    + + + + + +
    + + +Autocorrection will not be applied for the compact style +if the resulting code is longer than the Max configuration for +Layout/LineLength, but an offense will still be registered. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: compact (default)

    +
    +
    +
    # bad
    +def foo(bar)
    +end
    +
    +def self.foo(bar)
    +end
    +
    +# good
    +def foo(bar); end
    +
    +def foo(bar)
    +  # baz
    +end
    +
    +def self.foo(bar); end
    +
    +
    +
    +
    +

    EnforcedStyle: expanded

    +
    +
    +
    # bad
    +def foo(bar); end
    +
    +def self.foo(bar); end
    +
    +# good
    +def foo(bar)
    +end
    +
    +def self.foo(bar)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    compact

    compact, expanded

    +
    + +
    +
    +
    +

    Style/Encoding

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.50

    +
    +

    Checks ensures source files have no utf-8 encoding comments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# encoding: UTF-8
    +# coding: UTF-8
    +# -*- coding: UTF-8 -*-
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/EndBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.81

    +
    +

    Checks for END blocks.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +END { puts 'Goodbye!' }
    +
    +# good
    +at_exit { puts 'Goodbye!' }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/EndlessMethod

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.8

    -

    +
    +

    Checks for endless methods.

    +
    +
    +

    It can enforce either the use of endless methods definitions +for single-lined method bodies, or disallow endless methods.

    +
    +
    +

    Other method definition types are not considered by this cop.

    +
    +
    +

    The supported styles are:

    +
    +
    +
      +
    • +

      allow_single_line (default) - only single line endless method definitions are allowed.

      +
    • +
    • +

      allow_always - all endless method definitions are allowed.

      +
    • +
    • +

      disallow - all endless method definitions are disallowed.

      +
    • +
    +
    +
    + + + + + +
    + + +Incorrect endless method definitions will always be +corrected to a multi-line definition. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: allow_single_line (default)

    +
    +
    +
    # good
    +def my_method() = x
    +
    +# bad, multi-line endless method
    +def my_method() = x.foo
    +                   .bar
    +                   .baz
    +
    +
    +
    +
    +

    EnforcedStyle: allow_always

    +
    +
    +
    # good
    +def my_method() = x
    +
    +# good
    +def my_method() = x.foo
    +                   .bar
    +                   .baz
    +
    +
    +
    +
    +

    EnforcedStyle: disallow

    +
    +
    +
    # bad
    +def my_method() = x
    +
    +# bad
    +def my_method() = x.foo
    +                   .bar
    +                   .baz
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    allow_single_line

    allow_single_line, allow_always, disallow

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/EnvHome

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.29

    -

    +
    +

    Checks for consistent usage of ENV['HOME']. If nil is used as +the second argument of ENV.fetch, it is treated as a bad case like ENV[].

    +
    +
    +

    Safety

    +
    +

    The cop is unsafe because the result when nil is assigned to ENV['HOME'] changes:

    +
    +
    +
    +
    ENV['HOME'] = nil
    +ENV['HOME'] # => nil
    +Dir.home    # => '/home/foo'
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +ENV['HOME']
    +ENV.fetch('HOME', nil)
    +
    +# good
    +Dir.home
    +
    +# good
    +ENV.fetch('HOME', default)
    +
    +
    +
    +
    +
    +
    +

    Style/EvalWithLocation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Ensures that eval methods (eval, instance_eval, class_eval +and module_eval) are given filename and line number values (_\_FILE\__ +and \__LINE\_\_). This data is used to ensure that any errors raised +within the evaluated code will be given the correct identification +in a backtrace.

    +
    +
    +

    The cop also checks that the line number given relative to _\_LINE\_\_ is +correct.

    +
    +
    +

    This cop will autocorrect incorrect or missing filename and line number +values. However, if eval is called without a binding argument, the cop +will not attempt to automatically add a binding, or add filename and +line values.

    +
    +
    + + + + + +
    + + +This cop works only when a string literal is given as a code string. +No offense is reported if a string variable is given as below: +
    +
    +
    +
    +
    code = <<-RUBY
    +  def do_something
    +  end
    +RUBY
    +eval code # not checked.
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +eval <<-RUBY
    +  def do_something
    +  end
    +RUBY
    +
    +# bad
    +C.class_eval <<-RUBY
    +  def do_something
    +  end
    +RUBY
    +
    +# good
    +eval <<-RUBY, binding, __FILE__, __LINE__ + 1
    +  def do_something
    +  end
    +RUBY
    +
    +# good
    +C.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    +  def do_something
    +  end
    +RUBY
    +
    +
    +
    +
    +
    +
    +

    Style/EvenOdd

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.12

    0.29

    +
    +

    Checks for places where Integer#even? or Integer#odd? +can be used.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if x % 2 == 0
    +end
    +
    +# good
    +if x.even?
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ExactRegexpMatch

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.51

    -

    +
    +

    Checks for exact regexp match inside Regexp literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +string =~ /\Astring\z/
    +string === /\Astring\z/
    +string.match(/\Astring\z/)
    +string.match?(/\Astring\z/)
    +
    +# good
    +string == 'string'
    +
    +# bad
    +string !~ /\Astring\z/
    +
    +# good
    +string != 'string'
    +
    +
    +
    +
    +
    +
    +

    Style/ExpandPathArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks for use of the File.expand_path arguments. +Likewise, it also checks for the Pathname.new argument.

    +
    +
    +

    Contrastive bad case and good case are alternately shown in +the following examples.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +File.expand_path('..', __FILE__)
    +
    +# good
    +File.expand_path(__dir__)
    +
    +# bad
    +File.expand_path('../..', __FILE__)
    +
    +# good
    +File.expand_path('..', __dir__)
    +
    +# bad
    +File.expand_path('.', __FILE__)
    +
    +# good
    +File.expand_path(__FILE__)
    +
    +# bad
    +Pathname(__FILE__).parent.expand_path
    +
    +# good
    +Pathname(__dir__).expand_path
    +
    +# bad
    +Pathname.new(__FILE__).parent.expand_path
    +
    +# good
    +Pathname.new(__dir__).expand_path
    +
    +
    +
    +
    +
    +
    +

    Style/ExplicitBlockArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.89

    1.8

    +
    +

    Enforces the use of explicit block argument to avoid writing +block literal that just passes its arguments to another block.

    +
    +
    + + + + + +
    + + +This cop only registers an offense if the block args match the +yield args exactly. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def with_tmp_dir
    +  Dir.mktmpdir do |tmp_dir|
    +    Dir.chdir(tmp_dir) { |dir| yield dir } # block just passes arguments
    +  end
    +end
    +
    +# bad
    +def nine_times
    +  9.times { yield }
    +end
    +
    +# good
    +def with_tmp_dir(&block)
    +  Dir.mktmpdir do |tmp_dir|
    +    Dir.chdir(tmp_dir, &block)
    +  end
    +end
    +
    +with_tmp_dir do |dir|
    +  puts "dir is accessible as a parameter and pwd is set: #{dir}"
    +end
    +
    +# good
    +def nine_times(&block)
    +  9.times(&block)
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ExponentialNotation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.82

    -

    +
    +

    Enforces consistency when using exponential notation +for numbers in the code (eg 1.2e4). Different styles are supported:

    +
    +
    +
      +
    • +

      scientific which enforces a mantissa between 1 (inclusive) and 10 (exclusive).

      +
    • +
    • +

      engineering which enforces the exponent to be a multiple of 3 and the mantissa +to be between 0.1 (inclusive) and 10 (exclusive).

      +
    • +
    • +

      integral which enforces the mantissa to always be a whole number without +trailing zeroes.

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: scientific (default)

    +
    +
    +
    # Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
    +
    +# bad
    +10e6
    +0.3e4
    +11.7e5
    +3.14e0
    +
    +# good
    +1e7
    +3e3
    +1.17e6
    +3.14
    +
    +
    +
    +
    +

    EnforcedStyle: engineering

    +
    +
    +
    # Enforces using multiple of 3 exponents,
    +# mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
    +
    +# bad
    +3.2e7
    +0.1e5
    +12e5
    +1232e6
    +
    +# good
    +32e6
    +10e3
    +1.2e6
    +1.232e9
    +
    +
    +
    +
    +

    EnforcedStyle: integral

    +
    +
    +
    # Enforces the mantissa to have no decimal part and no
    +# trailing zeroes.
    +
    +# bad
    +3.2e7
    +0.1e5
    +120e4
    +
    +# good
    +32e6
    +1e4
    +12e5
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    scientific

    scientific, engineering, integral

    +
    + +
    +
    +
    +

    Style/FetchEnvVar

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.28

    -

    +
    +

    Suggests ENV.fetch for the replacement of ENV[]. +ENV[] silently fails and returns nil when the environment variable is unset, +which may cause unexpected behaviors when the developer forgets to set it. +On the other hand, ENV.fetch raises KeyError or returns the explicitly +specified default value.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +ENV['X']
    +x = ENV['X']
    +
    +# good
    +ENV.fetch('X')
    +x = ENV.fetch('X')
    +
    +# also good
    +!ENV['X']
    +ENV['X'].some_method # (e.g. `.nil?`)
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedVars

    []

    Array

    +
    + +
    +
    +
    +

    Style/FileEmpty

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.48

    -

    +
    +

    Prefer to use File.empty?('path/to/file') when checking if a file is empty.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because File.size, File.read, and File.binread +raise ENOENT exception when there is no file corresponding to the path, +while File.empty? does not raise an exception.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +File.zero?('path/to/file')
    +File.size('path/to/file') == 0
    +File.size('path/to/file') >= 0
    +File.size('path/to/file').zero?
    +File.read('path/to/file').empty?
    +File.binread('path/to/file') == ''
    +FileTest.zero?('path/to/file')
    +
    +# good
    +File.empty?('path/to/file')
    +FileTest.empty?('path/to/file')
    +
    +
    +
    +
    +
    +
    +

    Style/FileNull

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.69

    -

    +
    +

    Use File::NULL instead of hardcoding the null device (/dev/null on Unix-like +OSes, NUL or NUL: on Windows), so that code is platform independent. +Only looks for full string matches, substrings within a longer string are not +considered.

    +
    +
    + + + + + +
    + + +Uses inside arrays and hashes are ignored. +
    +
    +
    +

    Safety

    +
    +

    It is possible for a string value to be changed if code is being run +on multiple platforms and was previously hardcoded to a specific null device.

    +
    +
    +

    For example, the following string will change on Windows when changed to +File::NULL:

    +
    +
    +
    +
    path = "/dev/null"
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'/dev/null'
    +'NUL'
    +'NUL:'
    +
    +# good
    +File::NULL
    +
    +# ok - inside an array
    +null_devices = %w[/dev/null nul]
    +
    +# ok - inside a hash
    +{ unix: "/dev/null", windows: "nul" }
    +
    +
    +
    +
    +
    +
    +

    Style/FileRead

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.24

    -

    +
    +

    Favor File.(bin)read convenience methods.

    +
    +
    +

    Examples

    +
    +
    +
    # bad - text mode
    +File.open(filename).read
    +File.open(filename, &:read)
    +File.open(filename) { |f| f.read }
    +File.open(filename) do |f|
    +  f.read
    +end
    +File.open(filename, 'r').read
    +File.open(filename, 'r', &:read)
    +File.open(filename, 'r') do |f|
    +  f.read
    +end
    +
    +# good
    +File.read(filename)
    +
    +# bad - binary mode
    +File.open(filename, 'rb').read
    +File.open(filename, 'rb', &:read)
    +File.open(filename, 'rb') do |f|
    +  f.read
    +end
    +
    +# good
    +File.binread(filename)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/FileTouch

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.69

    -

    +
    +

    Checks for usage of File.open in append mode with empty block.

    +
    +
    +

    Such a usage only creates a new file, but it doesn’t update +timestamps for an existing file, which might have been the intention.

    +
    +
    +

    For example, for an existing file foo.txt:

    +
    +
    +
    +
    ruby -e "puts File.mtime('foo.txt')"
    +# 2024-11-26 12:17:23 +0100
    +
    +
    +
    +
    +
    ruby -e "File.open('foo.txt', 'a') {}"
    +
    +
    +
    +
    +
    ruby -e "puts File.mtime('foo.txt')"
    +# 2024-11-26 12:17:23 +0100 -> unchanged
    +
    +
    +
    +

    If the intention was to update timestamps, FileUtils.touch('foo.txt') +should be used instead.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe for this cop because unlike File.open, +FileUtils.touch updates an existing file’s timestamps.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +File.open(filename, 'a') {}
    +File.open(filename, 'a+') {}
    +
    +# good
    +FileUtils.touch(filename)
    +
    +
    +
    +
    +
    +
    +

    Style/FileWrite

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.24

    -

    +
    +

    Favor File.(bin)write convenience methods.

    +
    +
    + + + + + +
    + + +There are different method signatures between File.write (class method) +and File#write (instance method). The following case will be allowed because +static analysis does not know the contents of the splat argument: +
    +
    +
    +
    +
    File.open(filename, 'w') do |f|
    +  f.write(*objects)
    +end
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad - text mode
    +File.open(filename, 'w').write(content)
    +File.open(filename, 'w') do |f|
    +  f.write(content)
    +end
    +
    +# good
    +File.write(filename, content)
    +
    +# bad - binary mode
    +File.open(filename, 'wb').write(content)
    +File.open(filename, 'wb') do |f|
    +  f.write(content)
    +end
    +
    +# good
    +File.binwrite(filename, content)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/FloatDivision

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.72

    1.9

    +
    +

    Checks for division with integers coerced to floats. +It is recommended to either always use fdiv or coerce one side only. +This cop also provides other options for code consistency.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because if the operand variable is a string object +then .to_f will be removed and an error will occur.

    +
    +
    +
    +
    a = '1.2'
    +b = '3.4'
    +a.to_f / b.to_f # Both `to_f` calls are required here
    +
    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: single_coerce (default)

    +
    +
    +
    # bad
    +a.to_f / b.to_f
    +
    +# good
    +a.to_f / b
    +a / b.to_f
    +
    +
    +
    +
    +

    EnforcedStyle: left_coerce

    +
    +
    +
    # bad
    +a / b.to_f
    +a.to_f / b.to_f
    +
    +# good
    +a.to_f / b
    +
    +
    +
    +
    +

    EnforcedStyle: right_coerce

    +
    +
    +
    # bad
    +a.to_f / b
    +a.to_f / b.to_f
    +
    +# good
    +a / b.to_f
    +
    +
    +
    +
    +

    EnforcedStyle: fdiv

    +
    +
    +
    # bad
    +a / b.to_f
    +a.to_f / b
    +a.to_f / b.to_f
    +
    +# good
    +a.fdiv(b)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    single_coerce

    left_coerce, right_coerce, single_coerce, fdiv

    +
    + +
    +
    +
    +

    Style/For

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.13

    1.26

    +
    +

    Looks for uses of the for keyword or each method. The +preferred alternative is set in the EnforcedStyle configuration +parameter. An each call with a block on a single line is always +allowed.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because the scope of +variables is different between each and for.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: each (default)

    +
    +
    +
    # bad
    +def foo
    +  for n in [1, 2, 3] do
    +    puts n
    +  end
    +end
    +
    +# good
    +def foo
    +  [1, 2, 3].each do |n|
    +    puts n
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: for

    +
    +
    +
    # bad
    +def foo
    +  [1, 2, 3].each do |n|
    +    puts n
    +  end
    +end
    +
    +# good
    +def foo
    +  for n in [1, 2, 3] do
    +    puts n
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    each

    each, for

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/FormatString

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    0.49

    +
    +

    Enforces the use of a single string formatting utility. +Valid options include Kernel#format, Kernel#sprintf, and String#%.

    +
    +
    +

    The detection of String#% cannot be implemented in a reliable +manner for all cases, so only two scenarios are considered - +if the first argument is a string literal and if the second +argument is an array literal.

    +
    +
    +

    Autocorrection will be applied when using argument is a literal or known built-in conversion +methods such as to_d, to_f, to_h, to_i, to_r, to_s, and to_sym on variables, +provided that their return value is not an array. For example, when using to_s, +'%s' % [1, 2, 3].to_s can be autocorrected without any incompatibility:

    +
    +
    +
    +
    '%s' % [1, 2, 3]        #=> '1'
    +format('%s', [1, 2, 3]) #=> '[1, 2, 3]'
    +'%s' % [1, 2, 3].to_s   #=> '[1, 2, 3]'
    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: format (default)

    +
    +
    +
    # bad
    +puts sprintf('%10s', 'foo')
    +puts '%10s' % 'foo'
    +
    +# good
    +puts format('%10s', 'foo')
    +
    +
    +
    +
    +

    EnforcedStyle: sprintf

    +
    +
    +
    # bad
    +puts format('%10s', 'foo')
    +puts '%10s' % 'foo'
    +
    +# good
    +puts sprintf('%10s', 'foo')
    +
    +
    +
    +
    +

    EnforcedStyle: percent

    +
    +
    +
    # bad
    +puts format('%10s', 'foo')
    +puts sprintf('%10s', 'foo')
    +
    +# good
    +puts '%10s' % 'foo'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    format

    format, sprintf, percent

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/FormatStringToken

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.0

    +
    +

    Use a consistent style for named format string tokens.

    +
    +
    + + + + + +
    + + +unannotated style cop only works for strings +which are passed as arguments to those methods: +printf, sprintf, format, %. +The reason is that unannotated format is very similar +to encoded URLs or Date/Time formatting strings. +
    +
    +
    +

    This cop’s allowed methods can be customized with AllowedMethods. +By default, there are no allowed methods.

    +
    +
    +

    It is allowed to contain unannotated token +if the number of them is less than or equals to +MaxUnannotatedPlaceholdersAllowed.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: annotated (default)

    +
    +
    +
    # bad
    +format('%{greeting}', greeting: 'Hello')
    +format('%s', 'Hello')
    +
    +# good
    +format('%<greeting>s', greeting: 'Hello')
    +
    +
    +
    +
    +

    EnforcedStyle: template

    +
    +
    +
    # bad
    +format('%<greeting>s', greeting: 'Hello')
    +format('%s', 'Hello')
    +
    +# good
    +format('%{greeting}', greeting: 'Hello')
    +
    +
    +
    +
    +

    EnforcedStyle: unannotated

    +
    +
    +
    # bad
    +format('%<greeting>s', greeting: 'Hello')
    +format('%{greeting}', greeting: 'Hello')
    +
    +# good
    +format('%s', 'Hello')
    +
    +
    +
    +
    +

    MaxUnannotatedPlaceholdersAllowed: 0

    +
    +
    +
    # bad
    +format('%06d', 10)
    +format('%s %s.', 'Hello', 'world')
    +
    +# good
    +format('%<number>06d', number: 10)
    +
    +
    +
    +
    +

    MaxUnannotatedPlaceholdersAllowed: 1 (default)

    +
    +
    +
    # bad
    +format('%s %s.', 'Hello', 'world')
    +
    +# good
    +format('%06d', 10)
    +
    +
    +
    +
    +

    AllowedMethods: [] (default)

    +
    +
    +
    # bad
    +redirect('foo/%{bar_id}')
    +
    +
    +
    +
    +

    AllowedMethods: [redirect]

    +
    +
    +
    # good
    +redirect('foo/%{bar_id}')
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +redirect('foo/%{bar_id}')
    +
    +
    +
    +
    +

    AllowedPatterns: ['redirect']

    +
    +
    +
    # good
    +redirect('foo/%{bar_id}')
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    annotated

    annotated, template, unannotated

    MaxUnannotatedPlaceholdersAllowed

    1

    Integer

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    +
    +
    +
    +

    Style/FrozenStringLiteralComment

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.3 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.36

    0.79

    +
    +

    Helps you transition from mutable string literals +to frozen string literals. +It will add the # frozen_string_literal: true magic comment to the top +of files to enable frozen string literals. Frozen string literals may be +default in future Ruby. The comment will be added below a shebang and +encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.

    +
    +
    +

    Note that the cop will accept files where the comment exists but is set +to false instead of true.

    +
    +
    +

    To require a blank line after this comment, please see +Layout/EmptyLineAfterMagicComment cop.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe since any strings mutations will +change from being accepted to raising FrozenError, as all strings +will become frozen by default, and will need to be manually refactored.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: always (default)

    +
    +
    +
    # The `always` style will always add the frozen string literal comment
    +# to a file, regardless of the Ruby version or if `freeze` or `<<` are
    +# called on a string literal.
    +# bad
    +module Bar
    +  # ...
    +end
    +
    +# good
    +# frozen_string_literal: true
    +
    +module Bar
    +  # ...
    +end
    +
    +# good
    +# frozen_string_literal: false
    +
    +module Bar
    +  # ...
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: never

    +
    +
    +
    # The `never` will enforce that the frozen string literal comment does
    +# not exist in a file.
    +# bad
    +# frozen_string_literal: true
    +
    +module Baz
    +  # ...
    +end
    +
    +# good
    +module Baz
    +  # ...
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: always_true

    +
    +
    +
    # The `always_true` style enforces that the frozen string literal
    +# comment is set to `true`. This is a stricter option than `always`
    +# and forces projects to use frozen string literals.
    +# bad
    +# frozen_string_literal: false
    +
    +module Baz
    +  # ...
    +end
    +
    +# bad
    +module Baz
    +  # ...
    +end
    +
    +# good
    +# frozen_string_literal: true
    +
    +module Bar
    +  # ...
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    always

    always, always_true, never

    +
    +
    +
    +
    +

    Style/GlobalStdStream

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.89

    -

    +
    +

    Enforces the use of $stdout/$stderr/$stdin instead of STDOUT/STDERR/STDIN. +STDOUT/STDERR/STDIN are constants, and while you can actually +reassign (possibly to redirect some stream) constants in Ruby, you’ll get +an interpreter warning if you do so.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because STDOUT and $stdout may point to different +objects, for example.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +STDOUT.puts('hello')
    +
    +hash = { out: STDOUT, key: value }
    +
    +def m(out = STDOUT)
    +  out.puts('hello')
    +end
    +
    +# good
    +$stdout.puts('hello')
    +
    +hash = { out: $stdout, key: value }
    +
    +def m(out = $stdout)
    +  out.puts('hello')
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/GlobalVars

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.13

    -

    +
    +

    Looks for uses of global variables. +It does not report offenses for built-in global variables. +Built-in global variables are allowed by default. Additionally +users can allow additional variables via the AllowedVariables option.

    +
    +
    +

    Note that backreferences like $1, $2, etc are not global variables.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +$foo = 2
    +bar = $foo + 5
    +
    +# good
    +FOO = 2
    +foo = 2
    +$stdin.read
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedVariables

    []

    Array

    +
    + +
    +
    +
    +

    Style/GuardClause

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.20

    1.31

    +
    +

    Use a guard clause instead of wrapping the code inside a conditional +expression

    +
    +
    +

    A condition with an elsif or else branch is allowed unless +one of return, break, next, raise, or fail is used +in the body of the conditional expression.

    +
    +
    + + + + + +
    + + +Autocorrect works in most cases except with if-else statements + that contain logical operators such as foo || raise('exception') +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def test
    +  if something
    +    work
    +  end
    +end
    +
    +# good
    +def test
    +  return unless something
    +
    +  work
    +end
    +
    +# also good
    +def test
    +  work if something
    +end
    +
    +# bad
    +if something
    +  raise 'exception'
    +else
    +  ok
    +end
    +
    +# good
    +raise 'exception' if something
    +ok
    +
    +# bad
    +if something
    +  foo || raise('exception')
    +else
    +  ok
    +end
    +
    +# good
    +foo || raise('exception') if something
    +ok
    +
    +# bad
    +define_method(:test) do
    +  if something
    +    work
    +  end
    +end
    +
    +# good
    +define_method(:test) do
    +  return unless something
    +
    +  work
    +end
    +
    +# also good
    +define_method(:test) do
    +  work if something
    +end
    +
    +
    +
    +

    AllowConsecutiveConditionals: false (default)

    +
    +
    +
    # bad
    +def test
    +  if foo?
    +    work
    +  end
    +
    +  if bar?  # <- reports an offense
    +    work
    +  end
    +end
    +
    +
    +
    +
    +

    AllowConsecutiveConditionals: true

    +
    +
    +
    # good
    +def test
    +  if foo?
    +    work
    +  end
    +
    +  if bar?
    +    work
    +  end
    +end
    +
    +# bad
    +def test
    +  if foo?
    +    work
    +  end
    +
    +  do_something
    +
    +  if bar?  # <- reports an offense
    +    work
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinBodyLength

    1

    Integer

    AllowConsecutiveConditionals

    false

    Boolean

    +
    + +
    +
    +
    +

    Style/HashAsLastArrayItem

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.88

    -

    +
    +

    Checks for presence or absence of braces around hash literal as a last +array item depending on configuration.

    +
    +
    + + + + + +
    + + +This cop will ignore arrays where all items are hashes, regardless of +EnforcedStyle. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: braces (default)

    +
    +
    +
    # bad
    +[1, 2, one: 1, two: 2]
    +
    +# good
    +[1, 2, { one: 1, two: 2 }]
    +
    +# good
    +[{ one: 1 }, { two: 2 }]
    +
    +
    +
    +
    +

    EnforcedStyle: no_braces

    +
    +
    +
    # bad
    +[1, 2, { one: 1, two: 2 }]
    +
    +# good
    +[1, 2, one: 1, two: 2]
    +
    +# good
    +[{ one: 1 }, { two: 2 }]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    braces

    braces, no_braces

    +
    + +
    +
    +
    +

    Style/HashConversion

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.10

    1.55

    +
    +

    Checks the usage of pre-2.1 Hash[args] method of converting enumerables and +sequences of values to hashes.

    +
    +
    +

    Correction code from splat argument (Hash[*ary]) is not simply determined. For example, +Hash[*ary] can be replaced with ary.each_slice(2).to_h but it will be complicated. +So, AllowSplatArgument option is true by default to allow splat argument for simple code.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because ArgumentError occurs +if the number of elements is odd:

    +
    +
    +
    +
    Hash[[[1, 2], [3]]] #=> {1=>2, 3=>nil}
    +[[1, 2], [5]].to_h  #=> wrong array length at 1 (expected 2, was 1) (ArgumentError)
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Hash[ary]
    +
    +# good
    +ary.to_h
    +
    +# bad
    +Hash[key1, value1, key2, value2]
    +
    +# good
    +{key1 => value1, key2 => value2}
    +
    +
    +
    +

    AllowSplatArgument: true (default)

    +
    +
    +
    # good
    +Hash[*ary]
    +
    +
    +
    +
    +

    AllowSplatArgument: false

    +
    +
    +
    # bad
    +Hash[*ary]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowSplatArgument

    true

    Boolean

    +
    + +
    +
    +
    +

    Style/HashEachMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.80

    1.16

    +
    +

    Checks for uses of each_key and each_value Hash methods.

    +
    +
    + + + + + +
    + + +If you have an array of two-element arrays, you can put + parentheses around the block arguments to indicate that you’re not + working with a hash, and suppress RuboCop offenses. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is a Hash. The AllowedReceivers configuration can mitigate, +but not fully resolve, this safety issue.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +hash.keys.each { |k| p k }
    +hash.each { |k, unused_value| p k }
    +
    +# good
    +hash.each_key { |k| p k }
    +
    +# bad
    +hash.values.each { |v| p v }
    +hash.each { |unused_key, v| p v }
    +
    +# good
    +hash.each_value { |v| p v }
    +
    +
    +
    +

    AllowedReceivers: ['execute']

    +
    +
    +
    # good
    +execute(sql).keys.each { |v| p v }
    +execute(sql).values.each { |v| p v }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedReceivers

    Thread.current

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/HashExcept

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.7

    1.39

    +
    +

    Checks for usages of Hash#reject, Hash#select, and Hash#filter methods +that can be replaced with Hash#except method.

    +
    +
    +

    This cop should only be enabled on Ruby version 3.0 or higher. +(Hash#except was added in Ruby 3.0.)

    +
    +
    +

    For safe detection, it is limited to commonly used string and symbol comparisons +when used ==. +And do not check Hash#delete_if and Hash#keep_if to change receiver object.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is a Hash or responds to the replacement method.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }
    +{foo: 1, bar: 2, baz: 3}.select {|k, v| k != :bar }
    +{foo: 1, bar: 2, baz: 3}.filter {|k, v| k != :bar }
    +{foo: 1, bar: 2, baz: 3}.reject {|k, v| %i[bar].include?(k) }
    +{foo: 1, bar: 2, baz: 3}.select {|k, v| !%i[bar].include?(k) }
    +{foo: 1, bar: 2, baz: 3}.filter {|k, v| !%i[bar].include?(k) }
    +
    +# good
    +{foo: 1, bar: 2, baz: 3}.except(:bar)
    +
    +
    +
    +
    +
    +
    +

    Style/HashLikeCase

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.88

    -

    +
    +

    Checks for places where case-when represents a simple 1:1 +mapping and can be replaced with a hash lookup.

    +
    +
    +

    Examples

    +
    +

    MinBranchesCount: 3 (default)

    +
    +
    +
    # bad
    +case country
    +when 'europe'
    +  'http://eu.example.com'
    +when 'america'
    +  'http://us.example.com'
    +when 'australia'
    +  'http://au.example.com'
    +end
    +
    +# good
    +SITES = {
    +  'europe'    => 'http://eu.example.com',
    +  'america'   => 'http://us.example.com',
    +  'australia' => 'http://au.example.com'
    +}
    +SITES[country]
    +
    +
    +
    +
    +

    MinBranchesCount: 4

    +
    +
    +
    # good
    +case country
    +when 'europe'
    +  'http://eu.example.com'
    +when 'america'
    +  'http://us.example.com'
    +when 'australia'
    +  'http://au.example.com'
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinBranchesCount

    3

    Integer

    +
    +
    +
    +
    +

    Style/HashSyntax

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    1.67

    +
    +

    Checks hash literal syntax.

    +
    +
    +

    It can enforce either the use of the class hash rocket syntax or +the use of the newer Ruby 1.9 syntax (when applicable).

    +
    +
    +

    A separate offense is registered for each problematic pair.

    +
    +
    +

    The supported styles are:

    +
    +
    +
      +
    • +

      ruby19 - forces use of the 1.9 syntax (e.g. {a: 1}) when hashes have +all symbols for keys

      +
    • +
    • +

      hash_rockets - forces use of hash rockets for all hashes

      +
    • +
    • +

      no_mixed_keys - simply checks for hashes with mixed syntaxes

      +
    • +
    • +

      ruby19_no_mixed_keys - forces use of ruby 1.9 syntax and forbids mixed +syntax hashes

      +
    • +
    +
    +
    +

    This cop has EnforcedShorthandSyntax option. +It can enforce either the use of the explicit hash value syntax or +the use of Ruby 3.1’s hash value shorthand syntax.

    +
    +
    +

    The supported styles are:

    +
    +
    +
      +
    • +

      always - forces use of the 3.1 syntax (e.g. {foo:})

      +
    • +
    • +

      never - forces use of explicit hash literal value

      +
    • +
    • +

      either - accepts both shorthand and explicit use of hash literal value

      +
    • +
    • +

      consistent - forces use of the 3.1 syntax only if all values can be omitted in the hash

      +
    • +
    • +

      either_consistent - accepts both shorthand and explicit use of hash literal value, +but they must be consistent

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: ruby19 (default)

    +
    +
    +
    # bad
    +{:a => 2}
    +{b: 1, :c => 2}
    +
    +# good
    +{a: 2, b: 1}
    +{:c => 2, 'd' => 2} # acceptable since 'd' isn't a symbol
    +{d: 1, 'e' => 2} # technically not forbidden
    +
    +
    +
    +
    +

    EnforcedStyle: hash_rockets

    +
    +
    +
    # bad
    +{a: 1, b: 2}
    +{c: 1, 'd' => 5}
    +
    +# good
    +{:a => 1, :b => 2}
    +
    +
    +
    +
    +

    EnforcedStyle: no_mixed_keys

    +
    +
    +
    # bad
    +{:a => 1, b: 2}
    +{c: 1, 'd' => 2}
    +
    +# good
    +{:a => 1, :b => 2}
    +{c: 1, d: 2}
    +
    +
    +
    +
    +

    EnforcedStyle: ruby19_no_mixed_keys

    +
    +
    +
    # bad
    +{:a => 1, :b => 2}
    +{c: 2, 'd' => 3} # should just use hash rockets
    +
    +# good
    +{a: 1, b: 2}
    +{:c => 3, 'd' => 4}
    +
    +
    +
    +
    +

    EnforcedShorthandSyntax: always

    +
    +
    +
    # bad
    +{foo: foo, bar: bar}
    +
    +# good
    +{foo:, bar:}
    +
    +
    +
    +
    +

    EnforcedShorthandSyntax: never

    +
    +
    +
    # bad
    +{foo:, bar:}
    +
    +# good
    +{foo: foo, bar: bar}
    +
    +
    +
    +
    +

    EnforcedShorthandSyntax: either (default)

    +
    +
    +
    # good
    +{foo: foo, bar: bar}
    +
    +# good
    +{foo: foo, bar:}
    +
    +# good
    +{foo:, bar:}
    +
    +
    +
    +
    +

    EnforcedShorthandSyntax: consistent

    +
    +
    +
    # bad - `foo` and `bar` values can be omitted
    +{foo: foo, bar: bar}
    +
    +# bad - `bar` value can be omitted
    +{foo:, bar: bar}
    +
    +# bad - mixed syntaxes
    +{foo:, bar: baz}
    +
    +# good
    +{foo:, bar:}
    +
    +# good - can't omit `baz`
    +{foo: foo, bar: baz}
    +
    +
    +
    +
    +

    EnforcedShorthandSyntax: either_consistent

    +
    +
    +
    # good - `foo` and `bar` values can be omitted, but they are consistent, so it's accepted
    +{foo: foo, bar: bar}
    +
    +# bad - `bar` value can be omitted
    +{foo:, bar: bar}
    +
    +# bad - mixed syntaxes
    +{foo:, bar: baz}
    +
    +# good
    +{foo:, bar:}
    +
    +# good - can't omit `baz`
    +{foo: foo, bar: baz}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    ruby19

    ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys

    EnforcedShorthandSyntax

    either

    always, never, either, consistent, either_consistent

    UseHashRocketsWithSymbolValues

    false

    Boolean

    PreferHashRocketsForNonAlnumEndingSymbols

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/HashTransformKeys

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.5 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.80

    0.90

    +
    +

    Looks for uses of _.each_with_object({}) {…​}, +\_.map {…​}.to_h, and Hash[\_.map {…​}] that are actually just +transforming the keys of a hash, and tries to use a simpler & faster +call to transform_keys instead. +It should only be enabled on Ruby version 2.5 or newer. +(transform_keys was added in Ruby 2.5.)

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if we are +transforming an enumerable of key-value-like pairs that isn’t actually +a hash, e.g.: [[k1, v1], [k2, v2], …​]

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[foo(k)] = v }
    +Hash[{a: 1, b: 2}.collect { |k, v| [foo(k), v] }]
    +{a: 1, b: 2}.map { |k, v| [k.to_s, v] }.to_h
    +{a: 1, b: 2}.to_h { |k, v| [k.to_s, v] }
    +
    +# good
    +{a: 1, b: 2}.transform_keys { |k| foo(k) }
    +{a: 1, b: 2}.transform_keys { |k| k.to_s }
    +
    +
    +
    +
    +
    +
    +

    Style/HashTransformValues

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.80

    0.90

    +
    +

    Looks for uses of _.each_with_object({}) {…​}, +\_.map {…​}.to_h, and Hash[\_.map {…​}] that are actually just +transforming the values of a hash, and tries to use a simpler & faster +call to transform_values instead.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if we are +transforming an enumerable of key-value-like pairs that isn’t actually +a hash, e.g.: [[k1, v1], [k2, v2], …​]

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[k] = foo(v) }
    +Hash[{a: 1, b: 2}.collect { |k, v| [k, foo(v)] }]
    +{a: 1, b: 2}.map { |k, v| [k, v * v] }.to_h
    +{a: 1, b: 2}.to_h { |k, v| [k, v * v] }
    +
    +# good
    +{a: 1, b: 2}.transform_values { |v| foo(v) }
    +{a: 1, b: 2}.transform_values { |v| v * v }
    +
    +
    +
    +
    +
    +
    +

    Style/IdenticalConditionalBranches

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.36

    1.19

    +
    +

    Checks for identical expressions at the beginning or end of +each branch of a conditional expression. Such expressions should normally +be placed outside the conditional expression - before or after it.

    +
    +
    + + + + + +
    + + +The cop is poorly named and some people might think that it actually +checks for duplicated conditional branches. The name will probably be changed +in a future major RuboCop release. +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because changing the order of method invocations +may change the behavior of the code. For example:

    +
    +
    +
    +
    if method_that_modifies_global_state # 1
    +  method_that_relies_on_global_state # 2
    +  foo                                # 3
    +else
    +  method_that_relies_on_global_state # 2
    +  bar                                # 3
    +end
    +
    +
    +
    +

    In this example, method_that_relies_on_global_state will be moved before +method_that_modifies_global_state, which changes the behavior of the program.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition
    +  do_x
    +  do_z
    +else
    +  do_y
    +  do_z
    +end
    +
    +# good
    +if condition
    +  do_x
    +else
    +  do_y
    +end
    +do_z
    +
    +# bad
    +if condition
    +  do_z
    +  do_x
    +else
    +  do_z
    +  do_y
    +end
    +
    +# good
    +do_z
    +if condition
    +  do_x
    +else
    +  do_y
    +end
    +
    +# bad
    +case foo
    +when 1
    +  do_x
    +when 2
    +  do_x
    +else
    +  do_x
    +end
    +
    +# good
    +case foo
    +when 1
    +  do_x
    +  do_y
    +when 2
    +  # nothing
    +else
    +  do_x
    +  do_z
    +end
    +
    +# bad
    +case foo
    +in 1
    +  do_x
    +in 2
    +  do_x
    +else
    +  do_x
    +end
    +
    +# good
    +case foo
    +in 1
    +  do_x
    +  do_y
    +in 2
    +  # nothing
    +else
    +  do_x
    +  do_z
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/IfInsideElse

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    1.3

    +
    +

    If the else branch of a conditional consists solely of an if node, +it can be combined with the else to become an elsif. +This helps to keep the nesting level from getting too deep.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition_a
    +  action_a
    +else
    +  if condition_b
    +    action_b
    +  else
    +    action_c
    +  end
    +end
    +
    +# good
    +if condition_a
    +  action_a
    +elsif condition_b
    +  action_b
    +else
    +  action_c
    +end
    +
    +
    +
    +

    AllowIfModifier: false (default)

    +
    +
    +
    # bad
    +if condition_a
    +  action_a
    +else
    +  action_b if condition_b
    +end
    +
    +# good
    +if condition_a
    +  action_a
    +elsif condition_b
    +  action_b
    +end
    +
    +
    +
    +
    +

    AllowIfModifier: true

    +
    +
    +
    # good
    +if condition_a
    +  action_a
    +else
    +  action_b if condition_b
    +end
    +
    +# good
    +if condition_a
    +  action_a
    +elsif condition_b
    +  action_b
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowIfModifier

    false

    Boolean

    +
    +
    +
    +
    +

    Style/IfUnlessModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.30

    +
    +

    Checks for if and unless statements that would fit on one line if +written as modifier if/unless. The cop also checks for modifier +if/unless lines that exceed the maximum line length.

    +
    +
    +

    The maximum line length is configured in the Layout/LineLength +cop. The tab size is configured in the IndentationWidth of the +Layout/IndentationStyle cop.

    +
    +
    +

    One-line pattern matching is always allowed. To ensure that there are few cases +where the match variable is not used, and to prevent oversights. The variable x +becomes undefined and raises NameError when the following example is changed to +the modifier form:

    +
    +
    +
    +
    if [42] in [x]
    +  x # `x` is undefined when using modifier form.
    +end
    +
    +
    +
    + + + + + +
    + + +It is allowed when defined? argument has an undefined value, +because using the modifier form causes the following incompatibility: +
    +
    +
    +
    +
    unless defined?(undefined_foo)
    +  undefined_foo = 'default_value'
    +end
    +undefined_foo # => 'default_value'
    +
    +undefined_bar = 'default_value' unless defined?(undefined_bar)
    +undefined_bar # => nil
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition
    +  do_stuff(bar)
    +end
    +
    +unless qux.empty?
    +  Foo.do_something
    +end
    +
    +do_something_with_a_long_name(arg) if long_condition_that_prevents_code_fit_on_single_line
    +
    +# good
    +do_stuff(bar) if condition
    +Foo.do_something unless qux.empty?
    +
    +if long_condition_that_prevents_code_fit_on_single_line
    +  do_something_with_a_long_name(arg)
    +end
    +
    +if short_condition # a long comment that makes it too long if it were just a single line
    +  do_something
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/IfUnlessModifierOfIfUnless

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.39

    0.87

    +
    +

    Checks for if and unless statements used as modifiers of other if or +unless statements.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +tired? ? 'stop' : 'go faster' if running?
    +
    +# bad
    +if tired?
    +  "please stop"
    +else
    +  "keep going"
    +end if running?
    +
    +# good
    +if running?
    +  tired? ? 'stop' : 'go faster'
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/IfWithBooleanLiteralBranches

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.9

    -

    +
    +

    Checks for redundant if with boolean literal branches. +It checks only conditions to return boolean value (true or false) for safe detection. +The conditions to be checked are comparison methods, predicate methods, and +double negation (!!). +nonzero? method is allowed by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    This cop targets only if`s with a single `elsif or else branch. The following +code will be allowed, because it has two elsif branches:

    +
    +
    +
    +
    if foo
    +  true
    +elsif bar > baz
    +  true
    +elsif qux > quux # Single `elsif` is warned, but two or more `elsif`s are not.
    +  true
    +else
    +  false
    +end
    +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because there is no guarantee that all predicate methods +will return a boolean value. Those methods can be allowed with AllowedMethods config.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if foo == bar
    +  true
    +else
    +  false
    +end
    +
    +# bad
    +foo == bar ? true : false
    +
    +# good
    +foo == bar
    +
    +# bad
    +if foo.do_something?
    +  true
    +else
    +  false
    +end
    +
    +# good (but potentially an unsafe correction)
    +foo.do_something?
    +
    +
    +
    +

    AllowedMethods: ['nonzero?'] (default)

    +
    +
    +
    # good
    +num.nonzero? ? true : false
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    nonzero?

    Array

    +
    +
    +
    +
    +

    Style/IfWithSemicolon

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.83

    +
    +

    Checks for uses of semicolon in if statements.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +result = if some_condition; something else another_thing end
    +
    +# good
    +result = some_condition ? something : another_thing
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ImplicitRuntimeError

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.41

    -

    +
    +

    Checks for raise or fail statements which do not specify an +explicit exception class. (This raises a RuntimeError. Some projects +might prefer to use exception classes which more precisely identify the +nature of the error.)

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +raise 'Error message here'
    +
    +# good
    +raise ArgumentError, 'Error message here'
    +
    +
    +
    +
    +
    +
    +

    Style/InPatternThen

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.16

    -

    +
    +

    Checks for in; uses in case expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case expression
    +in pattern_a; foo
    +in pattern_b; bar
    +end
    +
    +# good
    +case expression
    +in pattern_a then foo
    +in pattern_b then bar
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/InfiniteLoop

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.26

    0.61

    +
    +

    Use Kernel#loop for infinite loops.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe as the rule should not necessarily apply if the loop +body might raise a StopIteration exception; contrary to other infinite +loops, Kernel#loop silently rescues that and returns nil.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +while true
    +  work
    +end
    +
    +# good
    +loop do
    +  work
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/InlineComment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.23

    -

    +
    +

    Checks for trailing inline comments.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +foo.each do |f|
    +  # Standalone comment
    +  f.bar
    +end
    +
    +# bad
    +foo.each do |f|
    +  f.bar # Trailing inline comment
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/InverseMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.48

    -

    +
    +

    Check for usages of not (not or !) called on a method +when an inverse of that method can be used instead.

    +
    +
    +

    Methods that can be inverted by a not (not or !) should be defined +in InverseMethods.

    +
    +
    +

    Methods that are inverted by inverting the return +of the block that is passed to the method should be defined in +InverseBlocks.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the method +and its inverse method are both defined on receiver, and also are +actually inverse of each other.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +!foo.none?
    +!foo.any? { |f| f.even? }
    +!foo.blank?
    +!(foo == bar)
    +foo.select { |f| !f.even? }
    +foo.reject { |f| f != 7 }
    +
    +# good
    +foo.none?
    +foo.blank?
    +foo.any? { |f| f.even? }
    +foo != bar
    +foo == bar
    +!!('foo' =~ /^\w+$/)
    +!(foo.class < Numeric) # Checking class hierarchy is allowed
    +# Blocks with guard clauses are ignored:
    +foo.select do |f|
    +  next if f.zero?
    +  f != 1
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    InverseMethods

    {:any?⇒:none?, :even?⇒:odd?, :==⇒:!=, :=⇒:!, :<⇒:>=, :>⇒:⇐}

    InverseBlocks

    {:select⇒:reject, :select!⇒:reject!}

    +
    +
    +
    +
    +

    Style/InvertibleUnlessCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    1.44

    1.50

    +
    +

    Checks for usages of unless which can be replaced by if with inverted condition. +Code without unless is easier to read, but that is subjective, so this cop +is disabled by default.

    +
    +
    +

    Methods that can be inverted should be defined in InverseMethods. Note that +the relationship of inverse methods needs to be defined in both directions. +For example,

    +
    +
    +
    +
    InverseMethods:
    +  :!=: :==
    +  :even?: :odd?
    +  :odd?: :even?
    +
    +
    +
    +

    will suggest both even? and odd? to be inverted, but only != (and not ==).

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the method +and its inverse method are both defined on receiver, and also are +actually inverse of each other.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad (simple condition)
    +foo unless !bar
    +foo unless x != y
    +foo unless x >= 10
    +foo unless x.even?
    +foo unless odd?
    +
    +# good
    +foo if bar
    +foo if x == y
    +foo if x < 10
    +foo if x.odd?
    +foo if even?
    +
    +# bad (complex condition)
    +foo unless x != y || x.even?
    +
    +# good
    +foo if x == y && x.odd?
    +
    +# good (if)
    +foo if !condition
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    InverseMethods

    {:!=⇒:==, :>⇒:⇐, :⇐⇒:>, :<⇒:>=, :>=⇒:<, :!⇒:=, :zero?⇒:nonzero?, :nonzero?⇒:zero?, :any?⇒:none?, :none?⇒:any?, :even?⇒:odd?, :odd?⇒:even?}

    +
    +
    +
    +
    +

    Style/IpAddresses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.58

    0.91

    +
    +

    Checks for hardcoded IP addresses, which can make code +brittle. IP addresses are likely to need to be changed when code +is deployed to a different server or environment, which may break +a deployment if forgotten. Prefer setting IP addresses in ENV or +other configuration.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +ip_address = '127.59.241.29'
    +
    +# good
    +ip_address = ENV['DEPLOYMENT_IP_ADDRESS']
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedAddresses

    ::

    Array

    Exclude

    **/*.gemfile, **/Gemfile, **/gems.rb, **/*.gemspec

    Array

    +
    +
    +
    +
    +

    Style/KeywordArgumentsMerging

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.68

    -

    +
    +

    When passing an existing hash as keyword arguments, provide additional arguments +directly rather than using merge.

    +
    +
    +

    Providing arguments directly is more performant than using merge, and +also leads to shorter and simpler code.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_method(**opts.merge(foo: true))
    +some_method(**opts.merge(other_opts))
    +
    +# good
    +some_method(**opts, foo: true)
    +some_method(**opts, **other_opts)
    +
    +
    +
    + +
    +
    +
    +

    Style/KeywordParametersOrder

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.90

    1.7

    +
    +

    Enforces that optional keyword parameters are placed at the +end of the parameters list.

    +
    +
    +

    This improves readability, because when looking through the source, +it is expected to find required parameters at the beginning of parameters list +and optional parameters at the end.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method(first: false, second:, third: 10)
    +  # body omitted
    +end
    +
    +# good
    +def some_method(second:, first: false, third: 10)
    +  # body omitted
    +end
    +
    +# bad
    +do_something do |first: false, second:, third: 10|
    +  # body omitted
    +end
    +
    +# good
    +do_something do |second:, first: false, third: 10|
    +  # body omitted
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/Lambda

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.40

    +
    +

    (by default) checks for uses of the lambda literal syntax for +single line lambdas, and the method call syntax for multiline lambdas. +It is configurable to enforce one of the styles for both single line +and multiline lambdas as well.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: line_count_dependent (default)

    +
    +
    +
    # bad
    +f = lambda { |x| x }
    +f = ->(x) do
    +      x
    +    end
    +
    +# good
    +f = ->(x) { x }
    +f = lambda do |x|
    +      x
    +    end
    +
    +
    +
    +
    +

    EnforcedStyle: lambda

    +
    +
    +
    # bad
    +f = ->(x) { x }
    +f = ->(x) do
    +      x
    +    end
    +
    +# good
    +f = lambda { |x| x }
    +f = lambda do |x|
    +      x
    +    end
    +
    +
    +
    +
    +

    EnforcedStyle: literal

    +
    +
    +
    # bad
    +f = lambda { |x| x }
    +f = lambda do |x|
    +      x
    +    end
    +
    +# good
    +f = ->(x) { x }
    +f = ->(x) do
    +      x
    +    end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    line_count_dependent

    line_count_dependent, lambda, literal

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/LambdaCall

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.13

    0.14

    +
    +

    Checks for use of the lambda.(args) syntax.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: call (default)

    +
    +
    +
    # bad
    +lambda.(x, y)
    +
    +# good
    +lambda.call(x, y)
    +
    +
    +
    +
    +

    EnforcedStyle: braces

    +
    +
    +
    # bad
    +lambda.call(x, y)
    +
    +# good
    +lambda.(x, y)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    call

    call, braces

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/LineEndConcatenation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.18

    0.64

    +
    +

    Checks for string literal concatenation at +the end of a line.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the +receiver is a string, in which case replacing << with \ +would result in a syntax error.

    +
    +
    +

    For example, this would be a false positive:

    +
    +
    +
    +
    array << 'foo' <<
    +         'bar' <<
    +         'baz'
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_str = 'ala' +
    +           'bala'
    +
    +some_str = 'ala' <<
    +           'bala'
    +
    +# good
    +some_str = 'ala' \
    +           'bala'
    +
    +
    +
    +
    +
    +
    +

    Style/MagicCommentFormat

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.35

    -

    +
    +

    Ensures magic comments are written consistently throughout your code base. +Looks for discrepancies in separators (- vs _) and capitalization for +both magic comment directives and values.

    +
    +
    +

    Required capitalization can be set with the DirectiveCapitalization and +ValueCapitalization configuration keys.

    +
    +
    + + + + + +
    + + +If one of these configuration is set to nil, any capitalization is allowed. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: snake_case (default)

    +
    +
    +
    # The `snake_case` style will enforce that the frozen string literal
    +# comment is written in snake case. (Words separated by underscores)
    +# bad
    +# frozen-string-literal: true
    +
    +module Bar
    +  # ...
    +end
    +
    +# good
    +# frozen_string_literal: false
    +
    +module Bar
    +  # ...
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: kebab_case

    +
    +
    +
    # The `kebab_case` style will enforce that the frozen string literal
    +# comment is written in kebab case. (Words separated by hyphens)
    +# bad
    +# frozen_string_literal: true
    +
    +module Baz
    +  # ...
    +end
    +
    +# good
    +# frozen-string-literal: true
    +
    +module Baz
    +  # ...
    +end
    +
    +
    +
    +
    +

    DirectiveCapitalization: lowercase (default)

    +
    +
    +
    # bad
    +# FROZEN-STRING-LITERAL: true
    +
    +# good
    +# frozen-string-literal: true
    +
    +
    +
    +
    +

    DirectiveCapitalization: uppercase

    +
    +
    +
    # bad
    +# frozen-string-literal: true
    +
    +# good
    +# FROZEN-STRING-LITERAL: true
    +
    +
    +
    +
    +

    DirectiveCapitalization: nil

    +
    +
    +
    # any capitalization is accepted
    +
    +# good
    +# frozen-string-literal: true
    +
    +# good
    +# FROZEN-STRING-LITERAL: true
    +
    +
    +
    +
    +

    ValueCapitalization: nil (default)

    +
    +
    +
    # any capitalization is accepted
    +
    +# good
    +# frozen-string-literal: true
    +
    +# good
    +# frozen-string-literal: TRUE
    +
    +
    +
    +
    +

    ValueCapitalization: lowercase

    +
    +
    +
    # when a value is not given, any capitalization is accepted
    +
    +# bad
    +# frozen-string-literal: TRUE
    +
    +# good
    +# frozen-string-literal: TRUE
    +
    +
    +
    +
    +

    ValueCapitalization: uppercase

    +
    +
    +
    # bad
    +# frozen-string-literal: true
    +
    +# good
    +# frozen-string-literal: TRUE
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    snake_case

    snake_case, kebab_case

    DirectiveCapitalization

    lowercase

    String

    ValueCapitalization

    <none>

    +
    +
    +
    +
    +

    Style/MapCompactWithConditionalBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.30

    -

    +
    +

    Prefer select or reject over map { …​ }.compact. +This cop also handles filter_map { …​ }, similar to map { …​ }.compact.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array.map { |e| some_condition? ? e : next }.compact
    +
    +# bad
    +array.filter_map { |e| some_condition? ? e : next }
    +
    +# bad
    +array.map do |e|
    +  if some_condition?
    +    e
    +  else
    +    next
    +  end
    +end.compact
    +
    +# bad
    +array.map do |e|
    +  next if some_condition?
    +
    +  e
    +end.compact
    +
    +# bad
    +array.map do |e|
    +  e if some_condition?
    +end.compact
    +
    +# good
    +array.select { |e| some_condition? }
    +
    +# good
    +array.reject { |e| some_condition? }
    +
    +
    +
    +
    +
    +
    +

    Style/MapIntoArray

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.63

    1.67

    +
    +

    Checks for usages of each with <<, push, or append which +can be replaced by map.

    +
    +
    +

    If PreferredMethods is configured for map in Style/CollectionMethods, +this cop uses the specified method for replacement.

    +
    +
    + + + + + +
    + + +The return value of Enumerable#each is self, whereas the +return value of Enumerable#map is an Array. They are not autocorrected +when a return value could be used because these types differ. +
    +
    +
    + + + + + +
    + + +It only detects when the mapping destination is either: +* a local variable initialized as an empty array and referred to only by the +pushing operation; +* or, if it is the single block argument to a [].tap block. +This is because, if not, it’s challenging to statically guarantee that the +mapping destination variable remains an empty array: +
    +
    +
    +
    +
    ret = []
    +src.each { |e| ret << e * 2 } # `<<` method may mutate `ret`
    +
    +dest = []
    +src.each { |e| dest << transform(e, dest) } # `transform` method may mutate `dest`
    +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because not all objects that have an each +method also have a map method (e.g. ENV). Additionally, for calls +with a block, not all objects that have a map method return an array +(e.g. Enumerator::Lazy).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +dest = []
    +src.each { |e| dest << e * 2 }
    +dest
    +
    +# good
    +dest = src.map { |e| e * 2 }
    +
    +# bad
    +[].tap do |dest|
    +  src.each { |e| dest << e * 2 }
    +end
    +
    +# good
    +dest = src.map { |e| e * 2 }
    +
    +# good - contains another operation
    +dest = []
    +src.each { |e| dest << e * 2; puts e }
    +dest
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MapToHash

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.6 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.24

    -

    +
    +

    Looks for uses of map.to_h or collect.to_h that could be +written with just to_h in Ruby >= 2.6.

    +
    +
    + + + + + +
    + + +Style/HashTransformKeys and Style/HashTransformValues will +also change this pattern if only hash keys or hash values are being +transformed. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if the receiver +is not an Enumerable.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something.map { |v| [v, v * 2] }.to_h
    +
    +# good
    +something.to_h { |v| [v, v * 2] }
    +
    +# bad
    +{foo: bar}.collect { |k, v| [k.to_s, v.do_something] }.to_h
    +
    +# good
    +{foo: bar}.to_h { |k, v| [k.to_s, v.do_something] }
    +
    +
    +
    +
    +
    +
    +

    Style/MapToSet

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.42

    -

    +
    +

    Looks for uses of map.to_set or collect.to_set that could be +written with just to_set.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if the receiver +is not an Enumerable.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something.map { |i| i * 2 }.to_set
    +
    +# good
    +something.to_set { |i| i * 2 }
    +
    +# bad
    +[1, 2, 3].collect { |i| i.to_s }.to_set
    +
    +# good
    +[1, 2, 3].to_set { |i| i.to_s }
    +
    +
    +
    +
    +
    +
    +

    Style/MethodCallWithArgsParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.47

    1.7

    +
    +

    Enforces the presence (default) or absence of parentheses in +method calls containing arguments.

    +
    +
    +

    In the default style (require_parentheses), macro methods are allowed. +Additional methods can be added to the AllowedMethods or +AllowedPatterns list. These options are valid only in the default +style. Macros can be included by either setting IgnoreMacros to false +or adding specific macros to the IncludedMacros list.

    +
    +
    +

    Precedence of options is as follows:

    +
    +
    +
      +
    1. +

      AllowedMethods

      +
    2. +
    3. +

      AllowedPatterns

      +
    4. +
    5. +

      IncludedMacros

      +
    6. +
    +
    +
    +

    If a method is listed in both IncludedMacros and AllowedMethods, +then the latter takes precedence (that is, the method is allowed).

    +
    +
    +

    In the alternative style (omit_parentheses), there are three additional +options.

    +
    +
    +
      +
    1. +

      AllowParenthesesInChaining is false by default. Setting it to +true allows the presence of parentheses in the last call during +method chaining.

      +
    2. +
    3. +

      AllowParenthesesInMultilineCall is false by default. Setting it +to true allows the presence of parentheses in multi-line method +calls.

      +
    4. +
    5. +

      AllowParenthesesInCamelCaseMethod is false by default. This +allows the presence of parentheses when calling a method whose name +begins with a capital letter and which has no arguments. Setting it +to true allows the presence of parentheses in such a method call +even with arguments.

      +
    6. +
    +
    +
    + + + + + +
    + + +The style of omit_parentheses allows parentheses in cases where +omitting them results in ambiguous or syntactically incorrect code. +
    +
    +
    +

    Non-exhaustive list of examples:

    +
    +
    +
      +
    • +

      Parentheses are required allowed in method calls with arguments inside +literals, logical operators, setting default values in position and +keyword arguments, chaining and more.

      +
    • +
    • +

      Parentheses are allowed in method calls with arguments inside +operators to avoid ambiguity. +triple-dot syntax introduced in Ruby 2.7 as omitting them starts an +endless range.

      +
    • +
    • +

      Parentheses are allowed when forwarding arguments with the +triple-dot syntax introduced in Ruby 2.7 as omitting them starts an +endless range.

      +
    • +
    • +

      Parentheses are required in calls with arguments when inside an +endless method definition introduced in Ruby 3.0.

      +
    • +
    • +

      Ruby 3.1’s hash omission syntax allows parentheses if the method call +is in conditionals and requires parentheses if the call +is not the value-returning expression. See +https://bugs.ruby-lang.org/issues/18396.

      +
    • +
    • +

      Parentheses are required in anonymous arguments, keyword arguments +and block passing in Ruby 3.2.

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: require_parentheses (default)

    +
    +
    +
    # bad
    +array.delete e
    +
    +# good
    +array.delete(e)
    +
    +# good
    +# Operators don't need parens
    +foo == bar
    +
    +# good
    +# Setter methods don't need parens
    +foo.bar = baz
    +
    +# okay with `puts` listed in `AllowedMethods`
    +puts 'test'
    +
    +# okay with `^assert` listed in `AllowedPatterns`
    +assert_equal 'test', x
    +
    +
    +
    +
    +

    EnforcedStyle: omit_parentheses

    +
    +
    +
    # bad
    +array.delete(e)
    +
    +# good
    +array.delete e
    +
    +# bad
    +action.enforce(strict: true)
    +
    +# good
    +action.enforce strict: true
    +
    +# good
    +# Parentheses are allowed for code that can be ambiguous without
    +# them.
    +action.enforce(condition) || other_condition
    +
    +# good
    +# Parentheses are allowed for calls that won't produce valid Ruby
    +# without them.
    +yield path, File.basename(path)
    +
    +# good
    +# Omitting the parentheses in Ruby 3.1 hash omission syntax can lead
    +# to ambiguous code. We allow them in conditionals and non-last
    +# expressions. See https://bugs.ruby-lang.org/issues/18396
    +if meets(criteria:, action:)
    +  safe_action(action) || dangerous_action(action)
    +end
    +
    +
    +
    +
    +

    IgnoreMacros: true (default)

    +
    +
    +
    # good
    +class Foo
    +  bar :baz
    +end
    +
    +
    +
    +
    +

    IgnoreMacros: false

    +
    +
    +
    # bad
    +class Foo
    +  bar :baz
    +end
    +
    +
    +
    +
    +

    AllowParenthesesInMultilineCall: false (default)

    +
    +
    +
    # bad
    +foo.enforce(
    +  strict: true
    +)
    +
    +# good
    +foo.enforce \
    +  strict: true
    +
    +
    +
    +
    +

    AllowParenthesesInMultilineCall: true

    +
    +
    +
    # good
    +foo.enforce(
    +  strict: true
    +)
    +
    +# good
    +foo.enforce \
    +  strict: true
    +
    +
    +
    +
    +

    AllowParenthesesInChaining: false (default)

    +
    +
    +
    # bad
    +foo().bar(1)
    +
    +# good
    +foo().bar 1
    +
    +
    +
    +
    +

    AllowParenthesesInChaining: true

    +
    +
    +
    # good
    +foo().bar(1)
    +
    +# good
    +foo().bar 1
    +
    +
    +
    +
    +

    AllowParenthesesInCamelCaseMethod: false (default)

    +
    +
    +
    # bad
    +Array(1)
    +
    +# good
    +Array 1
    +
    +
    +
    +
    +

    AllowParenthesesInCamelCaseMethod: true

    +
    +
    +
    # good
    +Array(1)
    +
    +# good
    +Array 1
    +
    +
    +
    +
    +

    AllowParenthesesInStringInterpolation: false (default)

    +
    +
    +
    # bad
    +"#{t('this.is.bad')}"
    +
    +# good
    +"#{t 'this.is.better'}"
    +
    +
    +
    +
    +

    AllowParenthesesInStringInterpolation: true

    +
    +
    +
    # good
    +"#{t('this.is.good')}"
    +
    +# good
    +"#{t 'this.is.also.good'}"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IgnoreMacros

    true

    Boolean

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    IncludedMacros

    []

    Array

    AllowParenthesesInMultilineCall

    false

    Boolean

    AllowParenthesesInChaining

    false

    Boolean

    AllowParenthesesInCamelCaseMethod

    false

    Boolean

    AllowParenthesesInStringInterpolation

    false

    Boolean

    EnforcedStyle

    require_parentheses

    require_parentheses, omit_parentheses

    +
    + +
    +
    +
    +

    Style/MethodCallWithoutArgsParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.47

    0.55

    +
    +

    Checks for unwanted parentheses in parameterless method calls.

    +
    +
    +

    This cop’s allowed methods can be customized with AllowedMethods. +By default, there are no allowed methods.

    +
    +
    + + + + + +
    + + +This cop allows the use of it() without arguments in blocks, +as in 0.times { it() }, following Lint/ItWithoutArgumentsInBlock cop. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +object.some_method()
    +
    +# good
    +object.some_method
    +
    +
    +
    +

    AllowedMethods: [] (default)

    +
    +
    +
    # bad
    +object.foo()
    +
    +
    +
    +
    +

    AllowedMethods: [foo]

    +
    +
    +
    # good
    +object.foo()
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    + +
    +
    +
    +

    Style/MethodCalledOnDoEndBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.14

    -

    +
    +

    Checks for methods called on a do…​end block. The point of +this check is that it’s easy to miss the call tacked on to the block +when reading code.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a do
    +  b
    +end.c
    +
    +# good
    +a { b }.c
    +
    +# good
    +foo = a do
    +  b
    +end
    +foo.c
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MethodDefParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.16

    1.7

    +
    +

    Checks for parentheses around the arguments in method +definitions. Both instance and class/singleton methods are checked.

    +
    +
    +

    Regardless of style, parentheses are necessary for:

    +
    +
    +
      +
    1. +

      Endless methods

      +
    2. +
    3. +

      Argument lists containing a forward-arg (…​)

      +
    4. +
    5. +

      Argument lists containing an anonymous rest arguments forwarding (*)

      +
    6. +
    7. +

      Argument lists containing an anonymous keyword rest arguments forwarding (**)

      +
    8. +
    9. +

      Argument lists containing an anonymous block forwarding (&)

      +
    10. +
    +
    +
    +

    Removing the parens would be a syntax error here.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: require_parentheses (default)

    +
    +
    +
    # The `require_parentheses` style requires method definitions
    +# to always use parentheses
    +
    +# bad
    +def bar num1, num2
    +  num1 + num2
    +end
    +
    +def foo descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name
    +  do_something
    +end
    +
    +# good
    +def bar(num1, num2)
    +  num1 + num2
    +end
    +
    +def foo(descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name)
    +  do_something
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: require_no_parentheses

    +
    +
    +
    # The `require_no_parentheses` style requires method definitions
    +# to never use parentheses
    +
    +# bad
    +def bar(num1, num2)
    +  num1 + num2
    +end
    +
    +def foo(descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name)
    +  do_something
    +end
    +
    +# good
    +def bar num1, num2
    +  num1 + num2
    +end
    +
    +def foo descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name
    +  do_something
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: require_no_parentheses_except_multiline

    +
    +
    +
    # The `require_no_parentheses_except_multiline` style prefers no
    +# parentheses when method definition arguments fit on single line,
    +# but prefers parentheses when arguments span multiple lines.
    +
    +# bad
    +def bar(num1, num2)
    +  num1 + num2
    +end
    +
    +def foo descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name
    +  do_something
    +end
    +
    +# good
    +def bar num1, num2
    +  num1 + num2
    +end
    +
    +def foo(descriptive_var_name,
    +        another_descriptive_var_name,
    +        last_descriptive_var_name)
    +  do_something
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    require_parentheses

    require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MinMax

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Checks for potential uses of Enumerable#minmax.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +bar = [foo.min, foo.max]
    +return foo.min, foo.max
    +
    +# good
    +bar = foo.minmax
    +return foo.minmax
    +
    +
    +
    +
    +
    +
    +

    Style/MinMaxComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.42

    -

    +
    +

    Enforces the use of max or min instead of comparison for greater or less.

    +
    +
    + + + + + +
    + + +It can be used if you want to present limit or threshold in Ruby 2.7+. +That it is slow though. So autocorrection will apply generic max or min: +
    +
    +
    +
    +
    a.clamp(b..) # Same as `[a, b].max`
    +a.clamp(..b) # Same as `[a, b].min`
    +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because even if a value has < or > method, +it is not necessarily Comparable.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a > b ? a : b
    +a >= b ? a : b
    +
    +# good
    +[a, b].max
    +
    +# bad
    +a < b ? a : b
    +a <= b ? a : b
    +
    +# good
    +[a, b].min
    +
    +
    +
    +
    +
    +
    +

    Style/MissingElse

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.30

    0.38

    +
    +

    Checks for if expressions that do not have an else branch.

    +
    +
    + + + + + +
    + + +Pattern matching is allowed to have no else branch because unlike if and case, +it raises NoMatchingPatternError if the pattern doesn’t match and without having else. +
    +
    +
    +

    Supported styles are: if, case, both.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: both (default)

    +
    +
    +
    # warn when an `if` or `case` expression is missing an `else` branch.
    +
    +# bad
    +if condition
    +  statement
    +end
    +
    +# bad
    +case var
    +when condition
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +# good
    +case var
    +when condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: if

    +
    +
    +
    # warn when an `if` expression is missing an `else` branch.
    +
    +# bad
    +if condition
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +# good
    +case var
    +when condition
    +  statement
    +end
    +
    +# good
    +case var
    +when condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: case

    +
    +
    +
    # warn when a `case` expression is missing an `else` branch.
    +
    +# bad
    +case var
    +when condition
    +  statement
    +end
    +
    +# good
    +case var
    +when condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +# good
    +if condition
    +  statement
    +end
    +
    +# good
    +if condition
    +  statement
    +else
    +  # the content of `else` branch will be determined by Style/EmptyElse
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    both

    if, case, both

    +
    +
    +
    +
    +

    Style/MissingRespondToMissing

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.56

    -

    +
    +

    Checks for the presence of method_missing without also +defining respond_to_missing?.

    +
    +
    +

    Not defining respond_to_missing? will cause metaprogramming +methods like respond_to? to behave unexpectedly:

    +
    +
    +
    +
    class StringDelegator
    +  def initialize(string)
    +    @string = string
    +  end
    +
    +  def method_missing(name, *args)
    +    @string.send(name, *args)
    +  end
    +end
    +
    +delegator = StringDelegator.new("foo")
    +# Claims to not respond to `upcase`.
    +delegator.respond_to?(:upcase) # => false
    +# But you can call it.
    +delegator.upcase # => FOO
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def method_missing(name, *args)
    +  if @delegate.respond_to?(name)
    +    @delegate.send(name, *args)
    +  else
    +    super
    +  end
    +end
    +
    +# good
    +def respond_to_missing?(name, include_private)
    +  @delegate.respond_to?(name) || super
    +end
    +
    +def method_missing(name, *args)
    +  if @delegate.respond_to?(name)
    +    @delegate.send(name, *args)
    +  else
    +    super
    +  end
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MixinGrouping

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.48

    0.49

    +
    +

    Checks for grouping of mixins in class and module bodies. +By default it enforces mixins to be placed in separate declarations, +but it can be configured to enforce grouping them in one declaration.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: separated (default)

    +
    +
    +
    # bad
    +class Foo
    +  include Bar, Qox
    +end
    +
    +# good
    +class Foo
    +  include Qox
    +  include Bar
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: grouped

    +
    +
    +
    # bad
    +class Foo
    +  extend Bar
    +  extend Qox
    +end
    +
    +# good
    +class Foo
    +  extend Qox, Bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    separated

    separated, grouped

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MixinUsage

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.51

    -

    +
    +

    Checks that include, extend and prepend statements appear +inside classes and modules, not at the top level, so as to not affect +the behavior of Object.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +include M
    +
    +class C
    +end
    +
    +# bad
    +extend M
    +
    +class C
    +end
    +
    +# bad
    +prepend M
    +
    +class C
    +end
    +
    +# good
    +class C
    +  include M
    +end
    +
    +# good
    +class C
    +  extend M
    +end
    +
    +# good
    +class C
    +  prepend M
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/ModuleFunction

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.11

    0.65

    +
    +

    Checks for use of extend self or module_function in a module.

    +
    +
    +

    Supported styles are: module_function (default), extend_self and forbidden.

    +
    +
    +

    A couple of things to keep in mind:

    +
    +
    +
      +
    • +

      forbidden style prohibits the usage of both styles

      +
    • +
    • +

      in default mode (module_function), the cop won’t be activated when the module +contains any private methods

      +
    • +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe (and is disabled by default) because extend self +and module_function do not behave exactly the same.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: module_function (default)

    +
    +
    +
    # bad
    +module Test
    +  extend self
    +  # ...
    +end
    +
    +# good
    +module Test
    +  module_function
    +  # ...
    +end
    +
    +# good
    +module Test
    +  extend self
    +  # ...
    +  private
    +  # ...
    +end
    +
    +# good
    +module Test
    +  class << self
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: extend_self

    +
    +
    +
    # bad
    +module Test
    +  module_function
    +  # ...
    +end
    +
    +# good
    +module Test
    +  extend self
    +  # ...
    +end
    +
    +# good
    +module Test
    +  class << self
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: forbidden

    +
    +
    +
    # bad
    +module Test
    +  module_function
    +  # ...
    +end
    +
    +# bad
    +module Test
    +  extend self
    +  # ...
    +end
    +
    +# bad
    +module Test
    +  extend self
    +  # ...
    +  private
    +  # ...
    +end
    +
    +# good
    +module Test
    +  class << self
    +    # ...
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    module_function

    module_function, extend_self, forbidden

    Autocorrect

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MultilineBlockChain

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    No

    0.13

    -

    +
    +

    Checks for chaining of a block after another block that spans +multiple lines.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Thread.list.select do |t|
    +  t.alive?
    +end.map do |t|
    +  t.object_id
    +end
    +
    +# good
    +alive_threads = Thread.list.select do |t|
    +  t.alive?
    +end
    +alive_threads.map do |t|
    +  t.object_id
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MultilineIfModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.45

    -

    +
    +

    Checks for uses of if/unless modifiers with multiple-lines bodies.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{
    +  result: 'this should not happen'
    +} unless cond
    +
    +# good
    +{ result: 'ok' } if cond
    +
    +
    +
    + +
    +
    +
    +

    Style/MultilineIfThen

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.26

    +
    +

    Checks for uses of the then keyword in multi-line if statements.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +# This is considered bad practice.
    +if cond then
    +end
    +
    +# good
    +# If statements can contain `then` on the same line.
    +if cond then a
    +elsif cond then b
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MultilineInPatternThen

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.16

    -

    +
    +

    Checks uses of the then keyword in multi-line in statement.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case expression
    +in pattern then
    +end
    +
    +# good
    +case expression
    +in pattern
    +end
    +
    +# good
    +case expression
    +in pattern then do_something
    +end
    +
    +# good
    +case expression
    +in pattern then do_something(arg1,
    +                             arg2)
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MultilineMemoization

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.44

    0.48

    +
    +

    Checks expressions wrapping styles for multiline memoization.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: keyword (default)

    +
    +
    +
    # bad
    +foo ||= (
    +  bar
    +  baz
    +)
    +
    +# good
    +foo ||= begin
    +  bar
    +  baz
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: braces

    +
    +
    +
    # bad
    +foo ||= begin
    +  bar
    +  baz
    +end
    +
    +# good
    +foo ||= (
    +  bar
    +  baz
    +)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    keyword

    keyword, braces

    +
    +
    +
    +
    +

    Style/MultilineMethodSignature

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.59

    1.7

    +
    +

    Checks for method signatures that span multiple lines.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +
    +def foo(bar, baz)
    +end
    +
    +# bad
    +
    +def foo(bar,
    +        baz)
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/MultilineTernaryOperator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.86

    +
    +

    Checks for multi-line ternary op expressions.

    +
    +
    + + + + + +
    + + +return if …​ else …​ end is syntax error. If return is used before +multiline ternary operator expression, it will be autocorrected to single-line +ternary operator. The same is true for break, next, and method call. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a = cond ?
    +  b : c
    +a = cond ? b :
    +    c
    +a = cond ?
    +    b :
    +    c
    +
    +return cond ?
    +       b :
    +       c
    +
    +# good
    +a = cond ? b : c
    +a = if cond
    +  b
    +else
    +  c
    +end
    +
    +return cond ? b : c
    +
    +
    +
    + +
    +
    +
    +

    Style/MultilineWhenThen

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.73

    -

    +
    +

    Checks uses of the then keyword +in multi-line when statements.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case foo
    +when bar then
    +end
    +
    +# good
    +case foo
    +when bar
    +end
    +
    +# good
    +case foo
    +when bar then do_something
    +end
    +
    +# good
    +case foo
    +when bar then do_something(arg1,
    +                           arg2)
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/MultipleComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.49

    1.1

    +
    +

    Checks against comparing a variable with multiple items, where +Array#include?, Set#include? or a case could be used instead +to avoid code repetition. +It accepts comparisons of multiple method calls to avoid unnecessary method calls +by default. It can be configured by AllowMethodComparison option.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a = 'a'
    +foo if a == 'a' || a == 'b' || a == 'c'
    +
    +# good
    +a = 'a'
    +foo if ['a', 'b', 'c'].include?(a)
    +
    +VALUES = Set['a', 'b', 'c'].freeze
    +# elsewhere...
    +foo if VALUES.include?(a)
    +
    +case foo
    +when 'a', 'b', 'c' then foo
    +# ...
    +end
    +
    +# accepted (but consider `case` as above)
    +foo if a == b.lightweight || a == b.heavyweight
    +
    +
    +
    +

    AllowMethodComparison: true (default)

    +
    +
    +
    # good
    +foo if a == b.lightweight || a == b.heavyweight
    +
    +
    +
    +
    +

    AllowMethodComparison: false

    +
    +
    +
    # bad
    +foo if a == b.lightweight || a == b.heavyweight
    +
    +# good
    +foo if [b.lightweight, b.heavyweight].include?(a)
    +
    +
    +
    +
    +

    ComparisonsThreshold: 2 (default)

    +
    +
    +
    # bad
    +foo if a == 'a' || a == 'b'
    +
    +
    +
    +
    +

    ComparisonsThreshold: 3

    +
    +
    +
    # good
    +foo if a == 'a' || a == 'b'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMethodComparison

    true

    Boolean

    ComparisonsThreshold

    2

    Integer

    +
    +
    +
    +
    +

    Style/MutableConstant

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.34

    1.8

    +
    +

    Checks whether some constant value isn’t a +mutable literal (e.g. array or hash).

    +
    +
    +

    Strict mode can be used to freeze all constants, rather than +just literals. +Strict mode is considered an experimental feature. It has not been +updated with an exhaustive list of all methods that will produce +frozen objects so there is a decent chance of getting some false +positives. Luckily, there is no harm in freezing an already +frozen object.

    +
    +
    +

    From Ruby 3.0, this cop honours the magic comment +'shareable_constant_value'. When this magic comment is set to any +acceptable value other than none, it will suppress the offenses +raised by this cop. It enforces frozen state.

    +
    +
    + + + + + +
    + + +Regexp and Range literals are frozen objects since Ruby 3.0. +
    +
    +
    + + + + + +
    + + +From Ruby 3.0, interpolated strings are not frozen when +# frozen-string-literal: true is used, so this cop enforces explicit +freezing for such strings. +
    +
    +
    + + + + + +
    + + +From Ruby 3.0, this cop allows explicit freezing of constants when +the shareable_constant_value directive is used. +
    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe since any mutations on objects that +are made frozen will change from being accepted to raising FrozenError, +and will need to be manually refactored.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: literals (default)

    +
    +
    +
    # bad
    +CONST = [1, 2, 3]
    +
    +# good
    +CONST = [1, 2, 3].freeze
    +
    +# good
    +CONST = <<~TESTING.freeze
    +  This is a heredoc
    +TESTING
    +
    +# good
    +CONST = Something.new
    +
    +
    +
    +
    +

    EnforcedStyle: strict

    +
    +
    +
    # bad
    +CONST = Something.new
    +
    +# bad
    +CONST = Struct.new do
    +  def foo
    +    puts 1
    +  end
    +end
    +
    +# good
    +CONST = Something.new.freeze
    +
    +# good
    +CONST = Struct.new do
    +  def foo
    +    puts 1
    +  end
    +end.freeze
    +
    +
    +
    +
    +
    # Magic comment - shareable_constant_value: literal
    +
    +# bad
    +CONST = [1, 2, 3]
    +
    +# good
    +# shareable_constant_value: literal
    +CONST = [1, 2, 3]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    literals

    literals, strict

    +
    +
    +
    +
    +

    Style/NegatedIf

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.20

    0.48

    +
    +

    Checks for uses of if with a negated condition. Only ifs +without else are considered. There are three different styles:

    +
    +
    +
      +
    • +

      both

      +
    • +
    • +

      prefix

      +
    • +
    • +

      postfix

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: both (default)

    +
    +
    +
    # enforces `unless` for `prefix` and `postfix` conditionals
    +
    +# bad
    +
    +if !foo
    +  bar
    +end
    +
    +# good
    +
    +unless foo
    +  bar
    +end
    +
    +# bad
    +
    +bar if !foo
    +
    +# good
    +
    +bar unless foo
    +
    +
    +
    +
    +

    EnforcedStyle: prefix

    +
    +
    +
    # enforces `unless` for just `prefix` conditionals
    +
    +# bad
    +
    +if !foo
    +  bar
    +end
    +
    +# good
    +
    +unless foo
    +  bar
    +end
    +
    +# good
    +
    +bar if !foo
    +
    +
    +
    +
    +

    EnforcedStyle: postfix

    +
    +
    +
    # enforces `unless` for just `postfix` conditionals
    +
    +# bad
    +
    +bar if !foo
    +
    +# good
    +
    +bar unless foo
    +
    +# good
    +
    +if !foo
    +  bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    both

    both, prefix, postfix

    +
    + +
    +
    +
    +

    Style/NegatedIfElseCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.2

    -

    +
    +

    Checks for uses of if-else and ternary operators with a negated condition +which can be simplified by inverting condition and swapping branches.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if !x
    +  do_something
    +else
    +  do_something_else
    +end
    +
    +# good
    +if x
    +  do_something_else
    +else
    +  do_something
    +end
    +
    +# bad
    +!x ? do_something : do_something_else
    +
    +# good
    +x ? do_something_else : do_something
    +
    +
    +
    +
    +
    +
    +

    Style/NegatedUnless

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.69

    -

    +
    +

    Checks for uses of unless with a negated condition. Only unless +without else are considered. There are three different styles:

    +
    +
    +
      +
    • +

      both

      +
    • +
    • +

      prefix

      +
    • +
    • +

      postfix

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: both (default)

    +
    +
    +
    # enforces `if` for `prefix` and `postfix` conditionals
    +
    +# bad
    +unless !foo
    +  bar
    +end
    +
    +# good
    +if foo
    +  bar
    +end
    +
    +# bad
    +bar unless !foo
    +
    +# good
    +bar if foo
    +
    +
    +
    +
    +

    EnforcedStyle: prefix

    +
    +
    +
    # enforces `if` for just `prefix` conditionals
    +
    +# bad
    +unless !foo
    +  bar
    +end
    +
    +# good
    +if foo
    +  bar
    +end
    +
    +# good
    +bar unless !foo
    +
    +
    +
    +
    +

    EnforcedStyle: postfix

    +
    +
    +
    # enforces `if` for just `postfix` conditionals
    +
    +# bad
    +bar unless !foo
    +
    +# good
    +bar if foo
    +
    +# good
    +unless !foo
    +  bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    both

    both, prefix, postfix

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/NegatedWhile

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.20

    -

    +
    +

    Checks for uses of while with a negated condition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +while !foo
    +  bar
    +end
    +
    +# good
    +until foo
    +  bar
    +end
    +
    +# bad
    +bar until !foo
    +
    +# good
    +bar while foo
    +bar while !foo && baz
    +
    +
    +
    + +
    +
    +
    +

    Style/NestedFileDirname

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.1 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.26

    -

    +
    +

    Checks for nested File.dirname. +It replaces nested File.dirname with the level argument introduced in Ruby 3.1.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +File.dirname(File.dirname(path))
    +
    +# good
    +File.dirname(path, 2)
    +
    +
    +
    +
    +
    +
    +

    Style/NestedModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.35

    -

    +
    +

    Checks for nested use of if, unless, while and until in their +modifier form.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something if a if b
    +
    +# good
    +something if b && a
    +
    +
    +
    + +
    +
    +
    +

    Style/NestedParenthesizedCalls

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    0.77

    +
    +

    Checks for unparenthesized method calls in the argument list +of a parenthesized method call. +be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, +be_truthy, be_within, eq, eql, end_with, include, match, raise_error, +respond_to, and start_with methods are allowed by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    Examples

    +
    +
    +
    # good
    +method1(method2(arg))
    +
    +# bad
    +method1(method2 arg)
    +
    +
    +
    +

    AllowedMethods: [foo]

    +
    +
    +
    # good
    +method1(foo arg)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, be_truthy, be_within, eq, eql, end_with, include, match, raise_error, respond_to, start_with

    Array

    +
    +
    +
    +
    +

    Style/NestedTernaryOperator

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.86

    +
    +

    Checks for nested ternary op expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a ? (b ? b1 : b2) : a2
    +
    +# good
    +if a
    +  b ? b1 : b2
    +else
    +  a2
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Next

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.22

    0.35

    +
    +

    Use next to skip iteration instead of a condition at the end.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: skip_modifier_ifs (default)

    +
    +
    +
    # bad
    +[1, 2].each do |a|
    +  if a == 1
    +    puts a
    +  end
    +end
    +
    +# good
    +[1, 2].each do |a|
    +  next unless a == 1
    +  puts a
    +end
    +
    +# good
    +[1, 2].each do |a|
    +  puts a if a == 1
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: always

    +
    +
    +
    # With `always` all conditions at the end of an iteration needs to be
    +# replaced by next - with `skip_modifier_ifs` the modifier if like
    +# this one are ignored: `[1, 2].each { |a| puts a if a == 1 }`
    +
    +# bad
    +[1, 2].each do |a|
    +  puts a if a == 1
    +end
    +
    +# bad
    +[1, 2].each do |a|
    +  if a == 1
    +    puts a
    +  end
    +end
    +
    +# good
    +[1, 2].each do |a|
    +  next unless a == 1
    +  puts a
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    skip_modifier_ifs

    skip_modifier_ifs, always

    MinBodyLength

    3

    Integer

    +
    + +
    +
    +
    +

    Style/NilComparison

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.12

    0.59

    +
    +

    Checks for comparison of something with nil using == and +nil?.

    +
    +
    +

    Supported styles are: predicate, comparison.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: predicate (default)

    +
    +
    +
    # bad
    +if x == nil
    +end
    +
    +# good
    +if x.nil?
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: comparison

    +
    +
    +
    # bad
    +if x.nil?
    +end
    +
    +# good
    +if x == nil
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    predicate

    predicate, comparison

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/NilLambda

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.3

    1.15

    +
    +

    Checks for lambdas and procs that always return nil, +which can be replaced with an empty lambda or proc instead.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +-> { nil }
    +
    +lambda do
    +  next nil
    +end
    +
    +proc { nil }
    +
    +Proc.new do
    +  break nil
    +end
    +
    +# good
    +-> {}
    +
    +lambda do
    +end
    +
    +-> (x) { nil if x }
    +
    +proc {}
    +
    +Proc.new { nil if x }
    +
    +
    +
    +
    +
    +
    +

    Style/NonNilCheck

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.20

    0.22

    +
    +

    Checks for non-nil checks, which are usually redundant.

    +
    +
    +

    With IncludeSemanticChanges set to false by default, this cop +does not report offenses for !x.nil? and does no changes that might +change behavior. +Also IncludeSemanticChanges set to false with EnforcedStyle: comparison of +Style/NilComparison cop, this cop does not report offenses for x != nil and +does no changes to !x.nil? style.

    +
    +
    +

    With IncludeSemanticChanges set to true, this cop reports offenses +for !x.nil? and autocorrects that and x != nil to solely x, which +is usually OK, but might change behavior.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if x != nil
    +end
    +
    +# good
    +if x
    +end
    +
    +# Non-nil checks are allowed if they are the final nodes of predicate.
    +# good
    +def signed_in?
    +  !current_user.nil?
    +end
    +
    +
    +
    +

    IncludeSemanticChanges: false (default)

    +
    +
    +
    # good
    +if !x.nil?
    +end
    +
    +
    +
    +
    +

    IncludeSemanticChanges: true

    +
    +
    +
    # bad
    +if !x.nil?
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    IncludeSemanticChanges

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Not

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.20

    +
    +

    Checks for uses of the keyword not instead of !.

    +
    +
    +

    Examples

    +
    +
    +
    # bad - parentheses are required because of op precedence
    +x = (not something)
    +
    +# good
    +x = !something
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/NumberedParameters

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.22

    -

    +
    +

    Checks for numbered parameters.

    +
    +
    +

    It can either restrict the use of numbered parameters to +single-lined blocks, or disallow completely numbered parameters.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: allow_single_line (default)

    +
    +
    +
    # bad
    +collection.each do
    +  puts _1
    +end
    +
    +# good
    +collection.each { puts _1 }
    +
    +
    +
    +
    +

    EnforcedStyle: disallow

    +
    +
    +
    # bad
    +collection.each { puts _1 }
    +
    +# good
    +collection.each { |item| puts item }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    allow_single_line

    allow_single_line, disallow

    +
    +
    +
    +
    +

    Style/NumberedParametersLimit

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.7 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.22

    -

    +
    +

    Detects use of an excessive amount of numbered parameters in a +single block. Having too many numbered parameters can make code too +cryptic and hard to read.

    +
    +
    +

    The cop defaults to registering an offense if there is more than 1 numbered +parameter but this maximum can be configured by setting Max.

    +
    +
    +

    Examples

    +
    +

    Max: 1 (default)

    +
    +
    +
    # bad
    +use_multiple_numbered_parameters { _1.call(_2, _3, _4) }
    +
    +# good
    +array.each { use_array_element_as_numbered_parameter(_1) }
    +hash.each { use_only_hash_value_as_numbered_parameter(_2) }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Max

    1

    Integer

    +
    +
    +
    +
    +

    Style/NumericLiteralPrefix

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.41

    -

    +
    +

    Checks for octal, hex, binary, and decimal literals using +uppercase prefixes and corrects them to lowercase prefix +or no prefix (in case of decimals).

    +
    +
    +

    Examples

    +
    +

    EnforcedOctalStyle: zero_with_o (default)

    +
    +
    +
    # bad - missing octal prefix
    +num = 01234
    +
    +# bad - uppercase prefix
    +num = 0O1234
    +num = 0X12AB
    +num = 0B10101
    +
    +# bad - redundant decimal prefix
    +num = 0D1234
    +num = 0d1234
    +
    +# good
    +num = 0o1234
    +num = 0x12AB
    +num = 0b10101
    +num = 1234
    +
    +
    +
    +
    +

    EnforcedOctalStyle: zero_only

    +
    +
    +
    # bad
    +num = 0o1234
    +num = 0O1234
    +
    +# good
    +num = 01234
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedOctalStyle

    zero_with_o

    zero_with_o, zero_only

    +
    + +
    +
    +
    +

    Style/NumericLiterals

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.48

    +
    +

    Checks for big numeric literals without _ between groups +of digits in them.

    +
    +
    +

    Additional allowed patterns can be added by adding regexps to +the AllowedPatterns configuration. All regexps are treated +as anchored even if the patterns do not contain anchors (so +\d{4}_\d{4} will allow 1234_5678 but not 1234_5678_9012).

    +
    +
    + + + + + +
    + + +Even if AllowedPatterns are given, autocorrection will +only correct to the standard pattern of an _ every 3 digits. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +1000000
    +1_00_000
    +1_0000
    +
    +# good
    +1_000_000
    +1000
    +
    +
    +
    +

    Strict: false (default)

    +
    +
    +
    # good
    +10_000_00 # typical representation of $10,000 in cents
    +
    +
    +
    +
    +

    Strict: true

    +
    +
    +
    # bad
    +10_000_00 # typical representation of $10,000 in cents
    +
    +
    +
    +
    +

    AllowedNumbers: [3000]

    +
    +
    +
    # good
    +3000 # You can specify allowed numbers. (e.g. port number)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    MinDigits

    5

    Integer

    Strict

    false

    Boolean

    AllowedNumbers

    []

    Array

    AllowedPatterns

    []

    Array

    +
    + +
    +
    +
    +

    Style/NumericPredicate

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.42

    0.59

    +
    +

    Checks for usage of comparison operators (==, +>, <) to test numbers as zero, positive, or negative. +These can be replaced by their respective predicate methods. +This cop can also be configured to do the reverse.

    +
    +
    +

    This cop’s allowed methods can be customized with AllowedMethods. +By default, there are no allowed methods.

    +
    +
    +

    This cop disregards #nonzero? as its value is truthy or falsey, +but not true and false, and thus not always interchangeable with +!= 0.

    +
    +
    +

    This cop allows comparisons to global variables, since they are often +populated with objects which can be compared with integers, but are +not themselves Integer polymorphic.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +defines the predicates or can be compared to a number, which may lead +to a false positive for non-standard classes.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: predicate (default)

    +
    +
    +
    # bad
    +foo == 0
    +0 > foo
    +bar.baz > 0
    +
    +# good
    +foo.zero?
    +foo.negative?
    +bar.baz.positive?
    +
    +
    +
    +
    +

    EnforcedStyle: comparison

    +
    +
    +
    # bad
    +foo.zero?
    +foo.negative?
    +bar.baz.positive?
    +
    +# good
    +foo == 0
    +0 > foo
    +bar.baz > 0
    +
    +
    +
    +
    +

    AllowedMethods: [] (default) with EnforcedStyle: predicate

    +
    +
    +
    # bad
    +foo == 0
    +0 > foo
    +bar.baz > 0
    +
    +
    +
    +
    +

    AllowedMethods: [==] with EnforcedStyle: predicate

    +
    +
    +
    # good
    +foo == 0
    +
    +# bad
    +0 > foo
    +bar.baz > 0
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default) with EnforcedStyle: comparison

    +
    +
    +
    # bad
    +foo.zero?
    +foo.negative?
    +bar.baz.positive?
    +
    +
    +
    +
    +

    AllowedPatterns: ['zero'] with EnforcedStyle: predicate

    +
    +
    +
    # good
    +# bad
    +foo.zero?
    +
    +# bad
    +foo.negative?
    +bar.baz.positive?
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    predicate

    predicate, comparison

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    Exclude

    spec/**/*

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/ObjectThen

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.6 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.28

    -

    +
    +

    Enforces the use of consistent method names +Object#yield_self or Object#then.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: then (default)

    +
    +
    +
    # bad
    +obj.yield_self { |x| x.do_something }
    +
    +# good
    +obj.then { |x| x.do_something }
    +
    +
    +
    +
    +

    EnforcedStyle: yield_self

    +
    +
    +
    # bad
    +obj.then { |x| x.do_something }
    +
    +# good
    +obj.yield_self { |x| x.do_something }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    then

    then, yield_self

    +
    + +
    +
    +
    +

    Style/OneLineConditional

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.90

    +
    +

    Checks for uses of if/then/else/end constructs on a single line. +AlwaysCorrectToMultiline config option can be set to true to autocorrect all offenses to +multi-line constructs. When AlwaysCorrectToMultiline is false (default case) the +autocorrect will first try converting them to ternary operators.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if foo then bar else baz end
    +
    +# bad
    +unless foo then baz else bar end
    +
    +# good
    +foo ? bar : baz
    +
    +# good
    +bar if foo
    +
    +# good
    +if foo then bar end
    +
    +# good
    +if foo
    +  bar
    +else
    +  baz
    +end
    +
    +
    +
    +

    AlwaysCorrectToMultiline: false (default)

    +
    +
    +
    # bad
    +if cond then run else dont end
    +
    +# good
    +cond ? run : dont
    +
    +
    +
    +
    +

    AlwaysCorrectToMultiline: true

    +
    +
    +
    # bad
    +if cond then run else dont end
    +
    +# good
    +if cond
    +  run
    +else
    +  dont
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AlwaysCorrectToMultiline

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/OpenStructUse

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    No

    1.23

    1.51

    +
    +

    Flags uses of OpenStruct, as it is now officially discouraged +to be used for performance, version compatibility, and potential security issues.

    +
    +
    +

    Safety

    +
    +

    Note that this cop may flag false positives; for instance, the following legal +use of a hand-rolled OpenStruct type would be considered an offense:

    +
    +
    +
    +
    module MyNamespace
    +  class OpenStruct # not the OpenStruct we're looking for
    +  end
    +
    +  def new_struct
    +    OpenStruct.new # resolves to MyNamespace::OpenStruct
    +  end
    +end
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +point = OpenStruct.new(x: 0, y: 1)
    +
    +# good
    +Point = Struct.new(:x, :y)
    +point = Point.new(0, 1)
    +
    +# also good
    +point = { x: 0, y: 1 }
    +
    +# bad
    +test_double = OpenStruct.new(a: 'b')
    +
    +# good (assumes test using rspec-mocks)
    +test_double = double
    +allow(test_double).to receive(:a).and_return('b')
    +
    +
    +
    + +
    +
    +
    +

    Style/OperatorMethodCall

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.37

    -

    +
    +

    Checks for redundant dot before operator method call. +The target operator methods are |, ^, &, <⇒, ==, ===, =~, >, >=, <, +, <<, >>, +, -, , /, %, *, ~, !, !=, and !~.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo.+ bar
    +foo.& bar
    +
    +# good
    +foo + bar
    +foo & bar
    +
    +
    +
    + +
    +
    +
    +

    Style/OptionHash

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.33

    0.34

    +
    +

    Checks for options hashes and discourages them if the +current Ruby version supports keyword arguments.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def fry(options = {})
    +  temperature = options.fetch(:temperature, 300)
    +  # ...
    +end
    +
    +# good
    +def fry(temperature: 300)
    +  # ...
    +end
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    SuspiciousParamNames

    options, opts, args, params, parameters

    Array

    Allowlist

    []

    Array

    +
    + +
    +
    +
    +

    Style/OptionalArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.33

    0.83

    +
    +

    Checks for optional arguments to methods +that do not come at the end of the argument list.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because changing a method signature will +implicitly change behavior.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo(a = 1, b, c)
    +end
    +
    +# good
    +def baz(a, b, c = 1)
    +end
    +
    +def foobar(a = 1, b = 2, c = 3)
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/OptionalBooleanParameter

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    No

    0.89

    -

    +
    +

    Checks for places where keyword arguments can be used instead of +boolean arguments when defining methods. respond_to_missing? method is allowed by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because changing a method signature will +implicitly change behavior.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method(bar = false)
    +  puts bar
    +end
    +
    +# bad - common hack before keyword args were introduced
    +def some_method(options = {})
    +  bar = options.fetch(:bar, false)
    +  puts bar
    +end
    +
    +# good
    +def some_method(bar: false)
    +  puts bar
    +end
    +
    +
    +
    +

    AllowedMethods: ['some_method']

    +
    +
    +
    # good
    +def some_method(bar = false)
    +  puts bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    respond_to_missing?

    Array

    +
    + +
    +
    +
    +

    Style/OrAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Checks for potential usage of the ||= operator.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +name = name ? name : 'Bozhidar'
    +
    +# bad
    +name = if name
    +         name
    +       else
    +         'Bozhidar'
    +       end
    +
    +# bad
    +unless name
    +  name = 'Bozhidar'
    +end
    +
    +# bad
    +name = 'Bozhidar' unless name
    +
    +# good - set name to 'Bozhidar', only if it's nil or false
    +name ||= 'Bozhidar'
    +
    +
    +
    + +
    +
    +
    +

    Style/ParallelAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.32

    -

    +
    +

    Checks for simple usages of parallel assignment. +This will only complain when the number of variables +being assigned matched the number of assigning variables.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a, b, c = 1, 2, 3
    +a, b, c = [1, 2, 3]
    +
    +# good
    +one, two = *foo
    +a, b = foo()
    +a, b = b, a
    +
    +a = 1
    +b = 2
    +c = 3
    +
    +
    +
    + +
    +
    +
    +

    Style/ParenthesesAroundCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.56

    +
    +

    Checks for the presence of superfluous parentheses around the +condition of if/unless/while/until.

    +
    +
    +

    AllowSafeAssignment option for safe assignment. +By safe assignment we mean putting parentheses around +an assignment to indicate "I know I’m using an assignment +as a condition. It’s not a mistake."

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x += 1 while (x < 10)
    +foo unless (bar || baz)
    +
    +if (x > 10)
    +elsif (x < 3)
    +end
    +
    +# good
    +x += 1 while x < 10
    +foo unless bar || baz
    +
    +if x > 10
    +elsif x < 3
    +end
    +
    +
    +
    +

    AllowSafeAssignment: true (default)

    +
    +
    +
    # good
    +foo unless (bar = baz)
    +
    +
    +
    +
    +

    AllowSafeAssignment: false

    +
    +
    +
    # bad
    +foo unless (bar = baz)
    +
    +
    +
    +
    +

    AllowInMultilineConditions: false (default)

    +
    +
    +
    # bad
    +if (x > 10 &&
    +   y > 10)
    +end
    +
    +# good
    + if x > 10 &&
    +    y > 10
    + end
    +
    +
    +
    +
    +

    AllowInMultilineConditions: true

    +
    +
    +
    # good
    +if (x > 10 &&
    +   y > 10)
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowSafeAssignment

    true

    Boolean

    AllowInMultilineConditions

    false

    Boolean

    +
    + +
    +
    +
    +

    Style/PercentLiteralDelimiters

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    0.48

    +
    +

    Enforces the consistent usage of %-literal delimiters.

    +
    +
    +

    Specify the 'default' key to set all preferred delimiters at once. You +can continue to specify individual preferred delimiters to override the +default.

    +
    +
    +

    Examples

    +
    +
    +
    # Style/PercentLiteralDelimiters:
    +#   PreferredDelimiters:
    +#     default: '[]'
    +#     '%i':    '()'
    +
    +# good
    +%w[alpha beta] + %i(gamma delta)
    +
    +# bad
    +%W(alpha #{beta})
    +
    +# bad
    +%I(alpha beta)
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    PreferredDelimiters

    {"default"⇒"()", "%i"⇒"[]", "%I"⇒"[]", "%r"⇒"{}", "%w"⇒"[]", "%W"⇒"[]"}

    +
    + +
    +
    +
    +

    Style/PercentQLiterals

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.25

    -

    +
    +

    Checks for usage of the %Q() syntax when %q() would do.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: lower_case_q (default)

    +
    +
    +
    # The `lower_case_q` style prefers `%q` unless
    +# interpolation is needed.
    +# bad
    +%Q[Mix the foo into the baz.]
    +%Q(They all said: 'Hooray!')
    +
    +# good
    +%q[Mix the foo into the baz]
    +%q(They all said: 'Hooray!')
    +
    +
    +
    +
    +

    EnforcedStyle: upper_case_q

    +
    +
    +
    # The `upper_case_q` style requires the sole use of `%Q`.
    +# bad
    +%q/Mix the foo into the baz./
    +%q{They all said: 'Hooray!'}
    +
    +# good
    +%Q/Mix the foo into the baz./
    +%Q{They all said: 'Hooray!'}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    lower_case_q

    lower_case_q, upper_case_q

    +
    +
    +
    +
    +

    Style/PerlBackrefs

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.13

    -

    +
    +

    Looks for uses of Perl-style regexp match +backreferences and their English versions like +$1, $2, $&, &+, $MATCH, $PREMATCH, etc.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +puts $1
    +
    +# good
    +puts Regexp.last_match(1)
    +
    +
    +
    + +
    +
    +
    +

    Style/PreferredHashMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.41

    0.70

    +
    +

    Checks for uses of methods Hash#has_key? and +Hash#has_value?, and suggests using Hash#key? and Hash#value? instead.

    +
    +
    +

    It is configurable to enforce the verbose method names, by using the +EnforcedStyle: verbose configuration.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is a Hash or responds to the replacement methods.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: short (default)

    +
    +
    +
    # bad
    +Hash#has_key?
    +Hash#has_value?
    +
    +# good
    +Hash#key?
    +Hash#value?
    +
    +
    +
    +
    +

    EnforcedStyle: verbose

    +
    +
    +
    # bad
    +Hash#key?
    +Hash#value?
    +
    +# good
    +Hash#has_key?
    +Hash#has_value?
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    short

    short, verbose

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Proc

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.18

    +
    +

    Checks for uses of Proc.new where Kernel#proc +would be more appropriate.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +p = Proc.new { |n| puts n }
    +
    +# good
    +p = proc { |n| puts n }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/QuotedSymbols

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.16

    -

    +
    +

    Checks if the quotes used for quoted symbols match the configured defaults. +By default uses the same configuration as Style/StringLiterals; if that +cop is not enabled, the default EnforcedStyle is single_quotes.

    +
    +
    +

    String interpolation is always kept in double quotes.

    +
    +
    + + + + + +
    + + +Lint/SymbolConversion can be used in parallel to ensure that symbols +are not quoted that don’t need to be. This cop is for configuring the quoting +style to use for symbols that require quotes. +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: same_as_string_literals (default) / single_quotes

    +
    +
    +
    # bad
    +:"abc-def"
    +
    +# good
    +:'abc-def'
    +:"#{str}"
    +:"a\'b"
    +
    +
    +
    +
    +

    EnforcedStyle: double_quotes

    +
    +
    +
    # bad
    +:'abc-def'
    +
    +# good
    +:"abc-def"
    +:"#{str}"
    +:"a\'b"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    same_as_string_literals

    same_as_string_literals, single_quotes, double_quotes

    +
    +
    +
    +
    +

    Style/RaiseArgs

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.14

    1.61

    +
    +

    Checks the args passed to fail and raise. For exploded +style (default), it recommends passing the exception class and message +to raise, rather than construct an instance of the error. It will +still allow passing just a message, or the construction of an error +with more than one argument.

    +
    +
    +

    The exploded style works identically, but with the addition that it +will also suggest constructing error objects when the exception is +passed multiple arguments.

    +
    +
    +

    The exploded style has an AllowedCompactTypes configuration +option that takes an Array of exception name Strings.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because raise Foo calls Foo.exception, not Foo.new.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: exploded (default)

    +
    +
    +
    # bad
    +raise StandardError.new('message')
    +
    +# good
    +raise StandardError, 'message'
    +fail 'message'
    +raise MyCustomError
    +raise MyCustomError.new(arg1, arg2, arg3)
    +raise MyKwArgError.new(key1: val1, key2: val2)
    +
    +# With `AllowedCompactTypes` set to ['MyWrappedError']
    +raise MyWrappedError.new(obj)
    +raise MyWrappedError.new(obj), 'message'
    +
    +
    +
    +
    +

    EnforcedStyle: compact

    +
    +
    +
    # bad
    +raise StandardError, 'message'
    +raise RuntimeError, arg1, arg2, arg3
    +
    +# good
    +raise StandardError.new('message')
    +raise MyCustomError
    +raise MyCustomError.new(arg1, arg2, arg3)
    +fail 'message'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    exploded

    compact, exploded

    AllowedCompactTypes

    []

    Array

    +
    + +
    +
    +
    +

    Style/RandomWithOffset

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for the use of randomly generated numbers, +added/subtracted with integer literals, as well as those with +Integer#succ and Integer#pred methods. Prefer using ranges instead, +as it clearly states the intentions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +rand(6) + 1
    +1 + rand(6)
    +rand(6) - 1
    +1 - rand(6)
    +rand(6).succ
    +rand(6).pred
    +Random.rand(6) + 1
    +Kernel.rand(6) + 1
    +rand(0..5) + 1
    +
    +# good
    +rand(1..6)
    +rand(1...7)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/RedundantArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.4

    1.55

    +
    +

    Checks for a redundant argument passed to certain methods.

    +
    +
    + + + + + +
    + + +This cop is limited to methods with single parameter. +
    +
    +
    +

    Method names and their redundant arguments can be configured like this:

    +
    +
    +
    +
    Methods:
    +  join: ''
    +  sum: 0
    +  split: ' '
    +  chomp: "\n"
    +  chomp!: "\n"
    +  foo: 2
    +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because of the following limitations:

    +
    +
    +
      +
    1. +

      This cop matches by method names only and hence cannot tell apart +methods with same name in different classes.

      +
    2. +
    3. +

      This cop may be unsafe if certain special global variables (e.g. $;, $/) are set. +That depends on the nature of the target methods, of course. For example, the default +argument to join is $OUTPUT_FIELD_SEPARATOR (or $,) rather than '', and if that +global is changed, '' is no longer a redundant argument.

      +
    4. +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array.join('')
    +[1, 2, 3].join("")
    +array.sum(0)
    +exit(true)
    +exit!(false)
    +string.split(" ")
    +"first\nsecond".split(" ")
    +string.chomp("\n")
    +string.chomp!("\n")
    +A.foo(2)
    +
    +# good
    +array.join
    +[1, 2, 3].join
    +array.sum
    +exit
    +exit!
    +string.split
    +"first second".split
    +string.chomp
    +string.chomp!
    +A.foo
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Methods

    {"join"⇒"", "sum"⇒0, "exit"⇒true, "exit!"⇒false, "split"⇒" ", "chomp"⇒"\n", "chomp!"⇒"\n"}

    +
    +
    +
    +
    +

    Style/RedundantArrayConstructor

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.52

    -

    +
    +

    Checks for the instantiation of array using redundant Array constructor. +Autocorrect replaces to array literal which is the simplest and fastest.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Array.new([])
    +Array[]
    +Array([])
    +Array.new(['foo', 'foo', 'foo'])
    +Array['foo', 'foo', 'foo']
    +Array(['foo', 'foo', 'foo'])
    +
    +# good
    +[]
    +['foo', 'foo', 'foo']
    +Array.new(3, 'foo')
    +Array.new(3) { 'foo' }
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.87

    -

    +
    +

    Checks for redundant assignment before returning.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def test
    +  x = foo
    +  x
    +end
    +
    +# bad
    +def test
    +  if x
    +    z = foo
    +    z
    +  elsif y
    +    z = bar
    +    z
    +  end
    +end
    +
    +# good
    +def test
    +  foo
    +end
    +
    +# good
    +def test
    +  if x
    +    foo
    +  elsif y
    +    bar
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantBegin

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.10

    0.21

    +
    +

    Checks for redundant begin blocks.

    +
    +
    +

    Currently it checks for code like this:

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def redundant
    +  begin
    +    ala
    +    bala
    +  rescue StandardError => e
    +    something
    +  end
    +end
    +
    +# good
    +def preferred
    +  ala
    +  bala
    +rescue StandardError => e
    +  something
    +end
    +
    +# bad
    +begin
    +  do_something
    +end
    +
    +# good
    +do_something
    +
    +# bad
    +# When using Ruby 2.5 or later.
    +do_something do
    +  begin
    +    something
    +  rescue => ex
    +    anything
    +  end
    +end
    +
    +# good
    +# In Ruby 2.5 or later, you can omit `begin` in `do-end` block.
    +do_something do
    +  something
    +rescue => ex
    +  anything
    +end
    +
    +# good
    +# Stabby lambdas don't support implicit `begin` in `do-end` blocks.
    +-> do
    +  begin
    +    foo
    +  rescue Bar
    +    baz
    +  end
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/RedundantCapitalW

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    -

    +
    +

    Checks for usage of the %W() syntax when %w() would do.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%W(cat dog pig)
    +%W[door wall floor]
    +
    +# good
    +%w/swim run bike/
    +%w[shirt pants shoes]
    +%W(apple #{fruit} grape)
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    -

    +
    +

    Checks for unnecessary conditional expressions.

    +
    +
    + + + + + +
    + + +Since the intention of the comment cannot be automatically determined, +autocorrection is not applied when a comment is used, as shown below: +
    +
    +
    +
    +
    if b
    +  # Important note.
    +  b
    +else
    +  c
    +end
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a = b ? b : c
    +
    +# good
    +a = b || c
    +
    +# bad
    +if b
    +  b
    +else
    +  c
    +end
    +
    +# good
    +b || c
    +
    +# good
    +if b
    +  b
    +elsif cond
    +  c
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantConditional

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.50

    -

    +
    +

    Checks for redundant returning of true/false in conditionals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x == y ? true : false
    +
    +# bad
    +if x == y
    +  true
    +else
    +  false
    +end
    +
    +# good
    +x == y
    +
    +# bad
    +x == y ? false : true
    +
    +# good
    +x != y
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantConstantBase

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.40

    -

    +
    +

    Avoid redundant :: prefix on constant.

    +
    +
    +

    How Ruby searches constant is a bit complicated, and it can often be difficult to +understand from the code whether the :: is intended or not. Where Module.nesting +is empty, there is no need to prepend ::, so it would be nice to consistently +avoid such meaningless :: prefix to avoid confusion.

    +
    +
    + + + + + +
    + + +This cop is disabled if Lint/ConstantResolution cop is enabled to prevent +conflicting rules. Because it respects user configurations that want to enable +Lint/ConstantResolution cop which is disabled by default. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +::Const
    +
    +# good
    +Const
    +
    +# bad
    +class << self
    +  ::Const
    +end
    +
    +# good
    +class << self
    +  Const
    +end
    +
    +# good
    +class A
    +  ::Const
    +end
    +
    +# good
    +module A
    +  ::Const
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantCurrentDirectoryInPath

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.53

    -

    +
    +

    Checks for uses a redundant current directory in path.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +require_relative './path/to/feature'
    +
    +# good
    +require_relative 'path/to/feature'
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantDoubleSplatHashBraces

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.41

    -

    +
    +

    Checks for redundant uses of double splat hash braces.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something(**{foo: bar, baz: qux})
    +
    +# good
    +do_something(foo: bar, baz: qux)
    +
    +# bad
    +do_something(**{foo: bar, baz: qux}.merge(options))
    +
    +# good
    +do_something(foo: bar, baz: qux, **options)
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantEach

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.38

    -

    +
    +

    Checks for redundant each.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, as it can produce false positives if the receiver +is not an Enumerator.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array.each.each { |v| do_something(v) }
    +
    +# good
    +array.each { |v| do_something(v) }
    +
    +# bad
    +array.each.each_with_index { |v, i| do_something(v, i) }
    +
    +# good
    +array.each.with_index { |v, i| do_something(v, i) }
    +array.each_with_index { |v, i| do_something(v, i) }
    +
    +# bad
    +array.each.each_with_object { |v, o| do_something(v, o) }
    +
    +# good
    +array.each.with_object { |v, o| do_something(v, o) }
    +array.each_with_object { |v, o| do_something(v, o) }
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.14

    0.29

    +
    +

    Checks for RuntimeError as the argument of raise/fail.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +raise RuntimeError, 'message'
    +raise RuntimeError.new('message')
    +
    +# good
    +raise 'message'
    +
    +# bad - message is not a string
    +raise RuntimeError, Object.new
    +raise RuntimeError.new(Object.new)
    +
    +# good
    +raise Object.new.to_s
    +
    +
    +
    + +
    +
    +
    +

    Style/RedundantFetchBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.86

    -

    +
    +

    Identifies places where fetch(key) { value } can be replaced by fetch(key, value).

    +
    +
    +

    In such cases fetch(key, value) method is faster than fetch(key) { value }.

    +
    +
    + + + + + +
    + + +The block string 'value' in hash.fetch(:key) { 'value' } is detected +when frozen string literal magic comment is enabled (i.e. # frozen_string_literal: true), +but not when disabled. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +does not have a different implementation of fetch.

    +
    +
    +
    +

    Examples

    +
    +

    SafeForConstants: false (default)

    +
    +
    +
    # bad
    +hash.fetch(:key) { 5 }
    +hash.fetch(:key) { true }
    +hash.fetch(:key) { nil }
    +array.fetch(5) { :value }
    +ENV.fetch(:key) { 'value' }
    +
    +# good
    +hash.fetch(:key, 5)
    +hash.fetch(:key, true)
    +hash.fetch(:key, nil)
    +array.fetch(5, :value)
    +ENV.fetch(:key, 'value')
    +
    +
    +
    +
    +

    SafeForConstants: true

    +
    +
    +
    # bad
    +ENV.fetch(:key) { VALUE }
    +
    +# good
    +ENV.fetch(:key, VALUE)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    SafeForConstants

    false

    Boolean

    +
    + +
    +
    +
    +

    Style/RedundantFileExtensionInRequire

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.88

    -

    +
    +

    Checks for the presence of superfluous .rb extension in +the filename provided to require and require_relative.

    +
    +
    + + + + + +
    + + +If the extension is omitted, Ruby tries adding '.rb', '.so', + and so on to the name until found. If the file named cannot be found, + a LoadError will be raised. + There is an edge case where foo.so file is loaded instead of a LoadError + if foo.so file exists when require 'foo.rb' will be changed to require 'foo', + but that seems harmless. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +require 'foo.rb'
    +require_relative '../foo.rb'
    +
    +# good
    +require 'foo'
    +require 'foo.so'
    +require_relative '../foo'
    +require_relative '../foo.so'
    +
    +
    +
    + +
    +
    +
    +

    Style/RedundantFilterChain

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.52

    1.57

    +
    +

    Identifies usages of any?, empty? or none? predicate methods +chained to select/filter/find_all and change them to use predicate method instead.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because array.select.any? evaluates all elements +through the select method, while array.any? uses short-circuit evaluation. +In other words, array.select.any? guarantees the evaluation of every element, +but array.any? does not necessarily evaluate all of them.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +arr.select { |x| x > 1 }.any?
    +
    +# good
    +arr.any? { |x| x > 1 }
    +
    +# bad
    +arr.select { |x| x > 1 }.empty?
    +arr.select { |x| x > 1 }.none?
    +
    +# good
    +arr.none? { |x| x > 1 }
    +
    +# good
    +relation.select(:name).any?
    +arr.select { |x| x > 1 }.any?(&:odd?)
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: false (default)

    +
    +
    +
    # good
    +arr.select { |x| x > 1 }.many?
    +
    +# good
    +arr.select { |x| x > 1 }.present?
    +
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: true

    +
    +
    +
    # bad
    +arr.select { |x| x > 1 }.many?
    +
    +# good
    +arr.many? { |x| x > 1 }
    +
    +# bad
    +arr.select { |x| x > 1 }.present?
    +
    +# good
    +arr.any? { |x| x > 1 }
    +
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantFreeze

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.34

    0.66

    +
    +

    Check for uses of Object#freeze on immutable objects.

    +
    +
    + + + + + +
    + + +Regexp and Range literals are frozen objects since Ruby 3.0. +
    +
    +
    + + + + + +
    + + +From Ruby 3.0, this cop allows explicit freezing of interpolated +string literals when # frozen-string-literal: true is used. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +CONST = 1.freeze
    +
    +# good
    +CONST = 1
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantHeredocDelimiterQuotes

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.45

    -

    +
    +

    Checks for redundant heredoc delimiter quotes.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +do_something(<<~'EOS')
    +  no string interpolation style text
    +EOS
    +
    +# good
    +do_something(<<~EOS)
    +  no string interpolation style text
    +EOS
    +
    +do_something(<<~'EOS')
    +  #{string_interpolation_style_text_not_evaluated}
    +EOS
    +
    +do_something(<<~'EOS')
    +  Preserve \
    +  newlines
    +EOS
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantInitialize

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Command-line only (Unsafe)

    1.27

    1.61

    +
    +

    Checks for initialize methods that are redundant.

    +
    +
    +

    An initializer is redundant if it does not do anything, or if it only +calls super with the same arguments given to it. If the initializer takes +an argument that accepts multiple values (restarg, kwrestarg, etc.) it +will not register an offense, because it allows the initializer to take a different +number of arguments as its superclass potentially does.

    +
    +
    + + + + + +
    + + +If an initializer argument has a default value, RuboCop assumes it +to not be redundant. +
    +
    +
    + + + + + +
    + + +Empty initializers are registered as offenses, but it is possible +to purposely create an empty initialize method to override a superclass’s +initializer. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because if subclass overrides initialize method with +a different arity than superclass.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def initialize
    +end
    +
    +# bad
    +def initialize
    +  super
    +end
    +
    +# bad
    +def initialize(a, b)
    +  super
    +end
    +
    +# bad
    +def initialize(a, b)
    +  super(a, b)
    +end
    +
    +# good
    +def initialize
    +  do_something
    +end
    +
    +# good
    +def initialize
    +  do_something
    +  super
    +end
    +
    +# good (different number of parameters)
    +def initialize(a, b)
    +  super(a)
    +end
    +
    +# good (default value)
    +def initialize(a, b = 5)
    +  super
    +end
    +
    +# good (default value)
    +def initialize(a, b: 5)
    +  super
    +end
    +
    +# good (changes the parameter requirements)
    +def initialize(*)
    +end
    +
    +# good (changes the parameter requirements)
    +def initialize(**)
    +end
    +
    +# good (changes the parameter requirements)
    +def initialize(...)
    +end
    +
    +
    +
    +

    AllowComments: true (default)

    +
    +
    +
    # good
    +def initialize
    +  # Overriding to negate superclass `initialize` method.
    +end
    +
    +
    +
    +
    +

    AllowComments: false

    +
    +
    +
    # bad
    +def initialize
    +  # Overriding to negate superclass `initialize` method.
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowComments

    true

    Boolean

    +
    +
    +
    +
    +

    Style/RedundantInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.76

    1.30

    +
    +

    Checks for strings that are just an interpolated expression.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because when calling a destructive method to string, +the resulting string may have different behavior or raise FrozenError.

    +
    +
    +
    +
    x = 'a'
    +y = "#{x}"
    +y << 'b'   # return 'ab'
    +x          # return 'a'
    +y = x.to_s
    +y << 'b'   # return 'ab'
    +x          # return 'ab'
    +
    +x = 'a'.freeze
    +y = "#{x}"
    +y << 'b'   # return 'ab'.
    +y = x.to_s
    +y << 'b'   # raise `FrozenError`.
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"#{@var}"
    +
    +# good
    +@var.to_s
    +
    +# good if @var is already a String
    +@var
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantInterpolationUnfreeze

    +
    +
    + + + + + +
    + + +Requires Ruby version 3.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.66

    -

    +
    +

    Before Ruby 3.0, interpolated strings followed the frozen string literal +magic comment which sometimes made it necessary to explicitly unfreeze them. +Ruby 3.0 changed interpolated strings to always be unfrozen which makes +unfreezing them redundant.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    ++"#{foo} bar"
    +
    +# bad
    +"#{foo} bar".dup
    +
    +# good
    +"#{foo} bar"
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantLineContinuation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.49

    -

    +
    +

    Check for redundant line continuation.

    +
    +
    +

    This cop marks a line continuation as redundant if removing the backslash +does not result in a syntax error. +However, a backslash at the end of a comment or +for string concatenation is not redundant and is not considered an offense.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo. \
    +  bar
    +foo \
    +  &.bar \
    +    .baz
    +
    +# good
    +foo.
    +  bar
    +foo
    +  &.bar
    +    .baz
    +
    +# bad
    +[foo, \
    +  bar]
    +{foo: \
    +  bar}
    +
    +# good
    +[foo,
    +  bar]
    +{foo:
    +  bar}
    +
    +# bad
    +foo(bar, \
    +  baz)
    +
    +# good
    +foo(bar,
    +  baz)
    +
    +# also good - backslash in string concatenation is not redundant
    +foo('bar' \
    +  'baz')
    +
    +# also good - backslash at the end of a comment is not redundant
    +foo(bar, # \
    +  baz)
    +
    +# also good - backslash at the line following the newline begins with a + or -,
    +# it is not redundant
    +1 \
    +  + 2 \
    +    - 3
    +
    +# also good - backslash with newline between the method name and its arguments,
    +# it is not redundant.
    +some_method \
    +  (argument)
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    -

    +
    +

    Checks for redundant parentheses.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +(x) if ((y.z).nil?)
    +
    +# good
    +x if y.z.nil?
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantPercentQ

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.76

    -

    +
    +

    Checks for usage of the %q/%Q syntax when '' or "" would do.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +name = %q(Bruce Wayne)
    +time = %q(8 o'clock)
    +question = %q("What did you say?")
    +
    +# good
    +name = 'Bruce Wayne'
    +time = "8 o'clock"
    +question = '"What did you say?"'
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/RedundantRegexpArgument

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.53

    -

    +
    +

    Identifies places where argument can be replaced from +a deterministic regexp to a string.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'foo'.byteindex(/f/)
    +'foo'.byterindex(/f/)
    +'foo'.gsub(/f/, 'x')
    +'foo'.gsub!(/f/, 'x')
    +'foo'.partition(/f/)
    +'foo'.rpartition(/f/)
    +'foo'.scan(/f/)
    +'foo'.split(/f/)
    +'foo'.start_with?(/f/)
    +'foo'.sub(/f/, 'x')
    +'foo'.sub!(/f/, 'x')
    +
    +# good
    +'foo'.byteindex('f')
    +'foo'.byterindex('f')
    +'foo'.gsub('f', 'x')
    +'foo'.gsub!('f', 'x')
    +'foo'.partition('f')
    +'foo'.rpartition('f')
    +'foo'.scan('f')
    +'foo'.split('f')
    +'foo'.start_with?('f')
    +'foo'.sub('f', 'x')
    +'foo'.sub!('f', 'x')
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantRegexpCharacterClass

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.85

    -

    +
    +

    Checks for unnecessary single-element Regexp character classes.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +r = /[x]/
    +
    +# good
    +r = /x/
    +
    +# bad
    +r = /[\s]/
    +
    +# good
    +r = /\s/
    +
    +# bad
    +r = %r{/[b]}
    +
    +# good
    +r = %r{/b}
    +
    +# good
    +r = /[ab]/
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantRegexpConstructor

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.52

    -

    +
    +

    Checks for the instantiation of regexp using redundant Regexp.new or Regexp.compile. +Autocorrect replaces to regexp literal which is the simplest and fastest.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Regexp.new(/regexp/)
    +Regexp.compile(/regexp/)
    +
    +# good
    +/regexp/
    +Regexp.new('regexp')
    +Regexp.compile('regexp')
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantRegexpEscape

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.85

    -

    +
    +

    Checks for redundant escapes inside Regexp literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +%r{foo\/bar}
    +
    +# good
    +%r{foo/bar}
    +
    +# good
    +/foo\/bar/
    +
    +# good
    +%r/foo\/bar/
    +
    +# good
    +%r!foo\!bar!
    +
    +# bad
    +/a\-b/
    +
    +# good
    +/a-b/
    +
    +# bad
    +/[\+\-]\d/
    +
    +# good
    +/[+\-]\d/
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantReturn

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.10

    0.14

    +
    +

    Checks for redundant return expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # These bad cases should be extended to handle methods whose body is
    +# if/else or a case expression with a default branch.
    +
    +# bad
    +def test
    +  return something
    +end
    +
    +# bad
    +def test
    +  one
    +  two
    +  three
    +  return something
    +end
    +
    +# bad
    +def test
    +  return something if something_else
    +end
    +
    +# good
    +def test
    +  something if something_else
    +end
    +
    +# good
    +def test
    +  if x
    +  elsif y
    +  else
    +  end
    +end
    +
    +
    +
    +

    AllowMultipleReturnValues: false (default)

    +
    +
    +
    # bad
    +def test
    +  return x, y
    +end
    +
    +
    +
    +
    +

    AllowMultipleReturnValues: true

    +
    +
    +
    # good
    +def test
    +  return x, y
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMultipleReturnValues

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/RedundantSelf

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.10

    0.13

    +
    +

    Checks for redundant uses of self.

    +
    +
    +

    The usage of self is only needed when:

    +
    +
    +
      +
    • +

      Sending a message to same object with zero arguments in +presence of a method name clash with an argument or a local +variable.

      +
    • +
    • +

      Calling an attribute writer to prevent a local variable assignment.

      +
    • +
    +
    +
    +

    Note, with using explicit self you can only send messages with public or +protected scope, you cannot send private messages this way.

    +
    +
    +

    Note we allow uses of self with operators because it would be awkward +otherwise. Also allows the use of self.it without arguments in blocks, +as in 0.times { self.it }, following Lint/ItWithoutArgumentsInBlock cop.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo(bar)
    +  self.baz
    +end
    +
    +# good
    +def foo(bar)
    +  self.bar  # Resolves name clash with the argument.
    +end
    +
    +def foo
    +  bar = 1
    +  self.bar  # Resolves name clash with the local variable.
    +end
    +
    +def foo
    +  %w[x y z].select do |bar|
    +    self.bar == bar  # Resolves name clash with argument of the block.
    +  end
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/RedundantSelfAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.90

    -

    +
    +

    Checks for places where redundant assignments are made for in place +modification methods.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because it can produce false positives for +user defined methods having one of the expected names, but not modifying +its receiver in place.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +args = args.concat(ary)
    +hash = hash.merge!(other)
    +
    +# good
    +args.concat(foo)
    +args += foo
    +hash.merge!(other)
    +
    +# bad
    +self.foo = foo.concat(ary)
    +
    +# good
    +foo.concat(ary)
    +self.foo += ary
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantSelfAssignmentBranch

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.19

    -

    +
    +

    Checks for places where conditional branch makes redundant self-assignment.

    +
    +
    +

    It only detects local variable because it may replace state of instance variable, +class variable, and global variable that have state across methods with nil.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo = condition ? bar : foo
    +
    +# good
    +foo = bar if condition
    +
    +# bad
    +foo = condition ? foo : bar
    +
    +# good
    +foo = bar unless condition
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantSort

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.76

    1.22

    +
    +

    Identifies instances of sorting and then +taking only the first or last element. The same behavior can +be accomplished without a relatively expensive sort by using +Enumerable#min instead of sorting and taking the first +element and Enumerable#max instead of sorting and taking the +last element. Similarly, Enumerable#min_by and +Enumerable#max_by can replace Enumerable#sort_by calls +after which only the first or last element is used.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because sort…​last and max may not return the +same element in all cases.

    +
    +
    +

    In an enumerable where there are multiple elements where a <⇒ b == 0, +or where the transformation done by the sort_by block has the +same result, sort.last and max (or sort_by.last and max_by) +will return different elements. sort.last will return the last +element but max will return the first element.

    +
    +
    +

    For example:

    +
    +
    +
    +
      class MyString < String; end
    +  strings = [MyString.new('test'), 'test']
    +  strings.sort.last.class   #=> String
    +  strings.max.class         #=> MyString
    +
    +
    +
    +
    +
      words = %w(dog horse mouse)
    +  words.sort_by { |word| word.length }.last   #=> 'mouse'
    +  words.max_by { |word| word.length }         #=> 'horse'
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[2, 1, 3].sort.first
    +[2, 1, 3].sort[0]
    +[2, 1, 3].sort.at(0)
    +[2, 1, 3].sort.slice(0)
    +
    +# good
    +[2, 1, 3].min
    +
    +# bad
    +[2, 1, 3].sort.last
    +[2, 1, 3].sort[-1]
    +[2, 1, 3].sort.at(-1)
    +[2, 1, 3].sort.slice(-1)
    +
    +# good
    +[2, 1, 3].max
    +
    +# bad
    +arr.sort_by(&:foo).first
    +arr.sort_by(&:foo)[0]
    +arr.sort_by(&:foo).at(0)
    +arr.sort_by(&:foo).slice(0)
    +
    +# good
    +arr.min_by(&:foo)
    +
    +# bad
    +arr.sort_by(&:foo).last
    +arr.sort_by(&:foo)[-1]
    +arr.sort_by(&:foo).at(-1)
    +arr.sort_by(&:foo).slice(-1)
    +
    +# good
    +arr.max_by(&:foo)
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantSortBy

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    -

    +
    +

    Identifies places where sort_by { …​ } can be replaced by +sort.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +array.sort_by { |x| x }
    +array.sort_by do |var|
    +  var
    +end
    +
    +# good
    +array.sort
    +
    +
    +
    +
    +
    +
    +

    Style/RedundantStringEscape

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.37

    -

    +
    +

    Checks for redundant escapes in string literals.

    +
    +
    +

    Examples

    +
    +
    +
    # bad - no need to escape # without following {/$/@
    +"\#foo"
    +
    +# bad - no need to escape single quotes inside double quoted string
    +"\'foo\'"
    +
    +# bad - heredocs are also checked for unnecessary escapes
    +<<~STR
    +  \#foo \"foo\"
    +STR
    +
    +# good
    +"#foo"
    +
    +# good
    +"\#{no_interpolation}"
    +
    +# good
    +"'foo'"
    +
    +# good
    +"foo\
    +bar"
    +
    +# good
    +<<~STR
    +  #foo "foo"
    +STR
    +
    +
    +
    +
    +
    +
    +

    Style/RegexpLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.30

    +
    +

    Enforces using // or %r around regular expressions.

    +
    +
    + + + + + +
    + + +The following %r cases using a regexp starts with a blank or = +as a method argument allowed to prevent syntax errors. +
    +
    +
    +
    +
    do_something %r{ regexp} # `do_something / regexp/` is an invalid syntax.
    +do_something %r{=regexp} # `do_something /=regexp/` is an invalid syntax.
    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: slashes (default)

    +
    +
    +
    # bad
    +snake_case = %r{^[\dA-Z_]+$}
    +
    +# bad
    +regex = %r{
    +  foo
    +  (bar)
    +  (baz)
    +}x
    +
    +# good
    +snake_case = /^[\dA-Z_]+$/
    +
    +# good
    +regex = /
    +  foo
    +  (bar)
    +  (baz)
    +/x
    +
    +
    +
    +
    +

    EnforcedStyle: percent_r

    +
    +
    +
    # bad
    +snake_case = /^[\dA-Z_]+$/
    +
    +# bad
    +regex = /
    +  foo
    +  (bar)
    +  (baz)
    +/x
    +
    +# good
    +snake_case = %r{^[\dA-Z_]+$}
    +
    +# good
    +regex = %r{
    +  foo
    +  (bar)
    +  (baz)
    +}x
    +
    +
    +
    +
    +

    EnforcedStyle: mixed

    +
    +
    +
    # bad
    +snake_case = %r{^[\dA-Z_]+$}
    +
    +# bad
    +regex = /
    +  foo
    +  (bar)
    +  (baz)
    +/x
    +
    +# good
    +snake_case = /^[\dA-Z_]+$/
    +
    +# good
    +regex = %r{
    +  foo
    +  (bar)
    +  (baz)
    +}x
    +
    +
    +
    +
    +

    AllowInnerSlashes: false (default)

    +
    +
    +
    # If `false`, the cop will always recommend using `%r` if one or more
    +# slashes are found in the regexp string.
    +
    +# bad
    +x =~ /home\//
    +
    +# good
    +x =~ %r{home/}
    +
    +
    +
    +
    +

    AllowInnerSlashes: true

    +
    +
    +
    # good
    +x =~ /home\//
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    slashes

    slashes, percent_r, mixed

    AllowInnerSlashes

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/RequireOrder

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always (Unsafe)

    1.40

    -

    +
    +

    Sort require and require_relative in alphabetical order.

    +
    +
    +

    Safety

    +
    +

    This cop’s autocorrection is unsafe because it will obviously change the execution order.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +require 'b'
    +require 'a'
    +
    +# good
    +require 'a'
    +require 'b'
    +
    +# bad
    +require_relative 'b'
    +require_relative 'a'
    +
    +# good
    +require_relative 'a'
    +require_relative 'b'
    +
    +# good (sorted within each section separated by a blank line)
    +require 'a'
    +require 'd'
    +
    +require 'b'
    +require 'c'
    +
    +# good
    +require 'b'
    +require_relative 'c'
    +require 'a'
    +
    +# bad
    +require 'a'
    +require 'c' if foo
    +require 'b'
    +
    +# good
    +require 'a'
    +require 'b'
    +require 'c' if foo
    +
    +# bad
    +require 'c'
    +if foo
    +  require 'd'
    +  require 'b'
    +end
    +require 'a'
    +
    +# good
    +require 'c'
    +if foo
    +  require 'b'
    +  require 'd'
    +end
    +require 'a'
    +
    +
    +
    +
    +
    +
    +

    Style/RescueModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.34

    +
    +

    Checks for uses of rescue in its modifier form is added for following +reasons:

    +
    +
    +
      +
    • +

      The syntax of modifier form rescue can be misleading because it +might lead us to believe that rescue handles the given exception +but it actually rescue all exceptions to return the given rescue +block. In this case, value returned by handle_error or +SomeException.

      +
    • +
    • +

      Modifier form rescue would rescue all the exceptions. It would +silently skip all exception or errors and handle the error. +Example: If NoMethodError is raised, modifier form rescue would +handle the exception.

      +
    • +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +some_method rescue handle_error
    +
    +# bad
    +some_method rescue SomeException
    +
    +# good
    +begin
    +  some_method
    +rescue
    +  handle_error
    +end
    +
    +# good
    +begin
    +  some_method
    +rescue SomeException
    +  handle_error
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/RescueStandardError

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for rescuing StandardError. There are two supported +styles implicit and explicit. This cop will not register an offense +if any error other than StandardError is specified.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: explicit (default)

    +
    +
    +
    # `explicit` will enforce using `rescue StandardError`
    +# instead of `rescue`.
    +
    +# bad
    +begin
    +  foo
    +rescue
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue StandardError
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue OtherError
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue StandardError, SecurityError
    +  bar
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: implicit

    +
    +
    +
    # `implicit` will enforce using `rescue` instead of
    +# `rescue StandardError`.
    +
    +# bad
    +begin
    +  foo
    +rescue StandardError
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue OtherError
    +  bar
    +end
    +
    +# good
    +begin
    +  foo
    +rescue StandardError, SecurityError
    +  bar
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    explicit

    implicit, explicit

    +
    +
    +
    +
    +

    Style/ReturnNil

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.50

    -

    +
    +

    Enforces consistency between return nil and return.

    +
    +
    +

    This cop is disabled by default. Because there seems to be a perceived semantic difference +between return and return nil. The former can be seen as just halting evaluation, +while the latter might be used when the return value is of specific concern.

    +
    +
    +

    Supported styles are return and return_nil.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: return (default)

    +
    +
    +
    # bad
    +def foo(arg)
    +  return nil if arg
    +end
    +
    +# good
    +def foo(arg)
    +  return if arg
    +end
    +
    +
    +
    +
    +

    EnforcedStyle: return_nil

    +
    +
    +
    # bad
    +def foo(arg)
    +  return if arg
    +end
    +
    +# good
    +def foo(arg)
    +  return nil if arg
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    return

    return, return_nil

    +
    +
    +
    +
    +

    Style/ReturnNilInPredicateMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.53

    1.67

    +
    +

    Checks for predicate method definitions that return nil. +A predicate method should only return a boolean value.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is marked as unsafe because the change of the return value +from nil to false could potentially lead to incompatibility issues.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo?
    +  return if condition
    +
    +  do_something?
    +end
    +
    +# bad
    +def foo?
    +  return nil if condition
    +
    +  do_something?
    +end
    +
    +# good
    +def foo?
    +  return false if condition
    +
    +  do_something?
    +end
    +
    +# bad
    +def foo?
    +  if condition
    +    nil
    +  else
    +    true
    +  end
    +end
    +
    +# good
    +def foo?
    +  if condition
    +    false
    +  else
    +    true
    +  end
    +end
    +
    +
    +
    +

    AllowedMethods: ['foo?']

    +
    +
    +
    # good
    +def foo?
    +  return if condition
    +
    +  do_something?
    +end
    +
    +
    +
    +
    +

    AllowedPatterns: [/foo/]

    +
    +
    +
    # good
    +def foo?
    +  return if condition
    +
    +  do_something?
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowedMethods

    []

    Array

    AllowedPatterns

    []

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/SafeNavigation

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.3 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.43

    1.67

    +
    +

    Transforms usages of a method call safeguarded by a non nil +check for the variable whose method is being called to +safe navigation (&.). If there is a method chain, all of the methods +in the chain need to be checked for safety, and all of the methods will +need to be changed to use safe navigation.

    +
    +
    +

    The default for ConvertCodeThatCanStartToReturnNil is false. +When configured to true, this will +check for code in the format !foo.nil? && foo.bar. As it is written, +the return of this code is limited to false and whatever the return +of the method is. If this is converted to safe navigation, +foo&.bar can start returning nil as well as what the method +returns.

    +
    +
    +

    The default for MaxChainLength is 2. +We have limited the cop to not register an offense for method chains +that exceed this option’s value.

    +
    +
    + + + + + +
    + + +This cop will recognize offenses but not autocorrect code when the +right hand side (RHS) of the && statement is an || statement +(eg. foo && (foo.bar? || foo.baz?)). It can be corrected +manually by removing the foo && and adding &. to each foo on the RHS. +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because if a value is false, the resulting +code will have different behavior or raise an error.

    +
    +
    +
    +
    x = false
    +x && x.foo  # return false
    +x&.foo      # raises NoMethodError
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo.bar if foo
    +foo.bar.baz if foo
    +foo.bar(param1, param2) if foo
    +foo.bar { |e| e.something } if foo
    +foo.bar(param) { |e| e.something } if foo
    +
    +foo.bar if !foo.nil?
    +foo.bar unless !foo
    +foo.bar unless foo.nil?
    +
    +foo && foo.bar
    +foo && foo.bar.baz
    +foo && foo.bar(param1, param2)
    +foo && foo.bar { |e| e.something }
    +foo && foo.bar(param) { |e| e.something }
    +
    +foo ? foo.bar : nil
    +foo.nil? ? nil : foo.bar
    +!foo.nil? ? foo.bar : nil
    +!foo ? nil : foo.bar
    +
    +# good
    +foo&.bar
    +foo&.bar&.baz
    +foo&.bar(param1, param2)
    +foo&.bar { |e| e.something }
    +foo&.bar(param) { |e| e.something }
    +foo && foo.bar.baz.qux # method chain with more than 2 methods
    +foo && foo.nil? # method that `nil` responds to
    +
    +# Method calls that do not use `.`
    +foo && foo < bar
    +foo < bar if foo
    +
    +# When checking `foo&.empty?` in a conditional, `foo` being `nil` will actually
    +# do the opposite of what the author intends.
    +foo && foo.empty?
    +
    +# This could start returning `nil` as well as the return of the method
    +foo.nil? || foo.bar
    +!foo || foo.bar
    +
    +# Methods that are used on assignment, arithmetic operation or
    +# comparison should not be converted to use safe navigation
    +foo.baz = bar if foo
    +foo.baz + bar if foo
    +foo.bar > 2 if foo
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    ConvertCodeThatCanStartToReturnNil

    false

    Boolean

    AllowedMethods

    present?, blank?, presence, try, try!

    Array

    MaxChainLength

    2

    Integer

    +
    +
    +
    +
    +

    Style/SafeNavigationChainLength

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    No

    1.68

    -

    +
    +

    Enforces safe navigation chains length to not exceed the configured maximum. +The longer the chain is, the harder it becomes to track what on it could be +returning nil.

    +
    +
    +

    There is a potential interplay with Style/SafeNavigation - if both are enabled +and their settings are "incompatible", one of the cops will complain about what +the other proposes.

    +
    +
    +

    E.g. if Style/SafeNavigation is configured with MaxChainLength: 2 (default) +and this cop is configured with Max: 1, then for foo.bar.baz if foo the former +will suggest foo&.bar&.baz, which is an offense for the latter.

    +
    +
    +

    Examples

    +
    +

    Max: 2 (default)

    +
    +
    +
    # bad
    +user&.address&.zip&.upcase
    +
    +# good
    +user&.address&.zip
    +user.address.zip if user
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Max

    2

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Sample

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.30

    -

    +
    +

    Identifies usages of shuffle.first, +shuffle.last, and shuffle[] and change them to use +sample instead.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[1, 2, 3].shuffle.first
    +[1, 2, 3].shuffle.first(2)
    +[1, 2, 3].shuffle.last
    +[2, 1, 3].shuffle.at(0)
    +[2, 1, 3].shuffle.slice(0)
    +[1, 2, 3].shuffle[2]
    +[1, 2, 3].shuffle[0, 2]    # sample(2) will do the same
    +[1, 2, 3].shuffle[0..2]    # sample(3) will do the same
    +[1, 2, 3].shuffle(random: Random.new).first
    +
    +# good
    +[1, 2, 3].shuffle
    +[1, 2, 3].sample
    +[1, 2, 3].sample(3)
    +[1, 2, 3].shuffle[1, 3]    # sample(3) might return a longer Array
    +[1, 2, 3].shuffle[1..3]    # sample(3) might return a longer Array
    +[1, 2, 3].shuffle[foo, bar]
    +[1, 2, 3].shuffle(random: Random.new)
    +
    +
    +
    + +
    +
    +
    +

    Style/SelectByRegexp

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.22

    -

    +
    +

    Looks for places where a subset of an Enumerable (array, +range, set, etc.; see note below) is calculated based on a Regexp +match, and suggests grep or grep_v instead.

    +
    +
    + + + + + +
    + + +Hashes do not behave as you may expect with grep, which +means that hash.grep is not equivalent to hash.select. Although +RuboCop is limited by static analysis, this cop attempts to avoid +registering an offense when the receiver is a hash (hash literal, +Hash.new, Hash#[], or to_h/to_hash). +
    +
    +
    + + + + + +
    + + +grep and grep_v were optimized when used without a block +in Ruby 3.0, but may be slower in previous versions. +See https://bugs.ruby-lang.org/issues/17030 +
    +
    +
    +

    Safety

    +
    +

    Autocorrection is marked as unsafe because MatchData will +not be created by grep, but may have previously been relied +upon after the match? or =~ call.

    +
    +
    +

    Additionally, the cop cannot guarantee that the receiver of +select or reject is actually an array by static analysis, +so the correction may not be actually equivalent.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad (select, filter, or find_all)
    +array.select { |x| x.match? /regexp/ }
    +array.select { |x| /regexp/.match?(x) }
    +array.select { |x| x =~ /regexp/ }
    +array.select { |x| /regexp/ =~ x }
    +
    +# bad (reject)
    +array.reject { |x| x.match? /regexp/ }
    +array.reject { |x| /regexp/.match?(x) }
    +array.reject { |x| x =~ /regexp/ }
    +array.reject { |x| /regexp/ =~ x }
    +
    +# good
    +array.grep(regexp)
    +array.grep_v(regexp)
    +
    +
    +
    +
    +
    +
    +

    Style/SelfAssignment

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.19

    0.29

    +
    +

    Enforces the use the shorthand for self-assignment.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +x = x + 1
    +
    +# good
    +x += 1
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Semicolon

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.19

    +
    +

    Checks for multiple expressions placed on the same line. +It also checks for lines terminated with a semicolon.

    +
    +
    +

    This cop has AllowAsExpressionSeparator configuration option. +It allows ; to separate several expressions on the same line.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo = 1; bar = 2;
    +baz = 3;
    +
    +# good
    +foo = 1
    +bar = 2
    +baz = 3
    +
    +
    +
    +

    AllowAsExpressionSeparator: false (default)

    +
    +
    +
    # bad
    +foo = 1; bar = 2
    +
    +
    +
    +
    +

    AllowAsExpressionSeparator: true

    +
    +
    +
    # good
    +foo = 1; bar = 2
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowAsExpressionSeparator

    false

    Boolean

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/Send

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    0.33

    -

    +
    +

    Checks for the use of the send method.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +Foo.send(bar)
    +quuz.send(fred)
    +
    +# good
    +Foo.__send__(bar)
    +quuz.public_send(fred)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/SendWithLiteralMethodName

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.64

    -

    +
    +

    Detects the use of the public_send method with a literal method name argument. +Since the send method can be used to call private methods, by default, +only the public_send method is detected.

    +
    +
    + + + + + +
    + + +Writer methods with names ending in = are always permitted because their +behavior differs as follows: +
    +
    +
    +
    +
    def foo=(foo)
    +  @foo = foo
    +  42
    +end
    +
    +self.foo = 1   # => 1
    +send(:foo=, 1) # => 42
    +
    +
    +
    +

    Safety

    +
    +

    This cop is not safe because it can incorrectly detect based on the receiver. +Additionally, when AllowSend is set to true, it cannot determine whether +the send method being detected is calling a private method.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +obj.public_send(:method_name)
    +obj.public_send('method_name')
    +
    +# good
    +obj.method_name
    +
    +
    +
    +

    AllowSend: true (default)

    +
    +
    +
    # good
    +obj.send(:method_name)
    +obj.send('method_name')
    +obj.__send__(:method_name)
    +obj.__send__('method_name')
    +
    +
    +
    +
    +

    AllowSend: false

    +
    +
    +
    # bad
    +obj.send(:method_name)
    +obj.send('method_name')
    +obj.__send__(:method_name)
    +obj.__send__('method_name')
    +
    +# good
    +obj.method_name
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowSend

    true

    Boolean

    +
    +
    +
    +
    +

    Style/SignalException

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.11

    0.37

    +
    +

    Checks for uses of fail and raise.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: only_raise (default)

    +
    +
    +
    # The `only_raise` style enforces the sole use of `raise`.
    +# bad
    +begin
    +  fail
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  fail
    +rescue Exception
    +  # handle it
    +end
    +
    +Kernel.fail
    +
    +# good
    +begin
    +  raise
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  raise
    +rescue Exception
    +  # handle it
    +end
    +
    +Kernel.raise
    +
    +
    +
    +
    +

    EnforcedStyle: only_fail

    +
    +
    +
    # The `only_fail` style enforces the sole use of `fail`.
    +# bad
    +begin
    +  raise
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  raise
    +rescue Exception
    +  # handle it
    +end
    +
    +Kernel.raise
    +
    +# good
    +begin
    +  fail
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  fail
    +rescue Exception
    +  # handle it
    +end
    +
    +Kernel.fail
    +
    +
    +
    +
    +

    EnforcedStyle: semantic

    +
    +
    +
    # The `semantic` style enforces the use of `fail` to signal an
    +# exception, then will use `raise` to trigger an offense after
    +# it has been rescued.
    +# bad
    +begin
    +  raise
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  # Error thrown
    +rescue Exception
    +  fail
    +end
    +
    +Kernel.fail
    +Kernel.raise
    +
    +# good
    +begin
    +  fail
    +rescue Exception
    +  # handle it
    +end
    +
    +def watch_out
    +  fail
    +rescue Exception
    +  raise 'Preferably with descriptive message'
    +end
    +
    +explicit_receiver.fail
    +explicit_receiver.raise
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    only_raise

    only_raise, only_fail, semantic

    +
    + +
    +
    +
    +

    Style/SingleArgumentDig

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.89

    -

    +
    +

    Sometimes using dig method ends up with just a single +argument. In such cases, dig should be replaced with [].

    +
    +
    +

    Since replacing hash&.dig(:key) with hash[:key] could potentially lead to error, +calls to the dig method using safe navigation will be ignored.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is an Enumerable or does not have a nonstandard implementation +of dig.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{ key: 'value' }.dig(:key)
    +[1, 2, 3].dig(0)
    +
    +# good
    +{ key: 'value' }[:key]
    +[1, 2, 3][0]
    +
    +# good
    +{ key1: { key2: 'value' } }.dig(:key1, :key2)
    +[1, [2, [3]]].dig(1, 1)
    +
    +# good
    +keys = %i[key1 key2]
    +{ key1: { key2: 'value' } }.dig(*keys)
    +
    +
    +
    +
    +
    +
    +

    Style/SingleLineBlockParams

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.16

    1.6

    +
    +

    Checks whether the block parameters of a single-line +method accepting a block match the names specified via configuration.

    +
    +
    +

    For instance one can configure reduce(inject) to use |a, e| as +parameters.

    +
    +
    +

    Configuration option: Methods +Should be set to use this cop. Array of hashes, where each key is the +method name and value - array of argument names.

    +
    +
    +

    Examples

    +
    +

    Methods: [{reduce: %w[a b]}]

    +
    +
    +
    # bad
    +foo.reduce { |c, d| c + d }
    +foo.reduce { |_, _d| 1 }
    +
    +# good
    +foo.reduce { |a, b| a + b }
    +foo.reduce { |a, _b| a }
    +foo.reduce { |a, (id, _)| a + id }
    +foo.reduce { true }
    +
    +# good
    +foo.reduce do |c, d|
    +  c + d
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Methods

    {"reduce"⇒["acc", "elem"]}, {"inject"⇒["acc", "elem"]}

    Array

    +
    +
    +
    +
    +

    Style/SingleLineDoEndBlock

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.57

    -

    +
    +

    Checks for single-line do…​end block.

    +
    +
    +

    In practice a single line do…​end is autocorrected when EnforcedStyle: semantic +is configured for Style/BlockDelimiters. The autocorrection maintains the +do …​ end syntax to preserve semantics and does not change it to {…​} block.

    +
    +
    + + + + + +
    + + +If InspectBlocks is set to true for Layout/RedundantLineBreak, blocks will +be autocorrected to be on a single line if possible. This cop respects that configuration +by not registering an offense if it would subsequently cause a +Layout/RedundantLineBreak offense. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +foo do |arg| bar(arg) end
    +
    +# good
    +foo do |arg|
    +  bar(arg)
    +end
    +
    +# bad
    +->(arg) do bar(arg) end
    +
    +# good
    +->(arg) { bar(arg) }
    +
    +
    +
    + +
    +
    +
    +

    Style/SingleLineMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    1.8

    +
    +

    Checks for single-line method definitions that contain a body. +It will accept single-line methods with no body.

    +
    +
    +

    Endless methods added in Ruby 3.0 are also accepted by this cop.

    +
    +
    +

    If Style/EndlessMethod is enabled with EnforcedStyle: allow_single_line or +allow_always, single-line methods will be autocorrected to endless +methods if there is only one statement in the body.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method; body end
    +def link_to(url); {:name => url}; end
    +def @table.columns; super; end
    +
    +# good
    +def self.resource_class=(klass); end
    +def @table.columns; end
    +def some_method() = body
    +
    +
    +
    +

    AllowIfMethodIsEmpty: true (default)

    +
    +
    +
    # good
    +def no_op; end
    +
    +
    +
    +
    +

    AllowIfMethodIsEmpty: false

    +
    +
    +
    # bad
    +def no_op; end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowIfMethodIsEmpty

    true

    Boolean

    +
    + +
    +
    +
    +

    Style/SlicingWithRange

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.6 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.83

    -

    +
    +

    Checks that arrays are not sliced with the redundant ary[0..-1], replacing it with ary, +and ensures arrays are sliced with endless ranges instead of ary[start..-1] on Ruby 2.6+, +and with beginless ranges instead of ary[nil..end] on Ruby 2.7+.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because x..-1 and x.. are only guaranteed to +be equivalent for Array#[], String#[], and the cop cannot determine what class +the receiver is.

    +
    +
    +

    For example:

    +
    +
    +
    +
    sum = proc { |ary| ary.sum }
    +sum[-3..-1] # => -6
    +sum[-3..] # Hangs forever
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +items[0..-1]
    +items[0..nil]
    +items[0...nil]
    +
    +# good
    +items
    +
    +# bad
    +items[1..-1]   # Ruby 2.6+
    +items[1..nil]  # Ruby 2.6+
    +
    +# good
    +items[1..]     # Ruby 2.6+
    +
    +# bad
    +items[nil..42] # Ruby 2.7+
    +
    +# good
    +items[..42]    # Ruby 2.7+
    +items[0..42]   # Ruby 2.7+
    +
    +
    +
    + +
    +
    +
    +

    Style/SoleNestedConditional

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.89

    1.5

    +
    +

    If the branch of a conditional consists solely of a conditional node, +its conditions can be combined with the conditions of the outer branch. +This helps to keep the nesting level from getting too deep.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +if condition_a
    +  if condition_b
    +    do_something
    +  end
    +end
    +
    +# bad
    +if condition_b
    +  do_something
    +end if condition_a
    +
    +# good
    +if condition_a && condition_b
    +  do_something
    +end
    +
    +
    +
    +

    AllowModifier: false (default)

    +
    +
    +
    # bad
    +if condition_a
    +  do_something if condition_b
    +end
    +
    +# bad
    +if condition_b
    +  do_something
    +end if condition_a
    +
    +
    +
    +
    +

    AllowModifier: true

    +
    +
    +
    # good
    +if condition_a
    +  do_something if condition_b
    +end
    +
    +# good
    +if condition_b
    +  do_something
    +end if condition_a
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowModifier

    false

    Boolean

    +
    +
    +
    +
    +

    Style/SpecialGlobalVars

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.13

    0.36

    +
    +

    Looks for uses of Perl-style global variables. +Correcting to global variables in the 'English' library +will add a require statement to the top of the file if +enabled by RequireEnglish config.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is marked as unsafe because if RequireEnglish is not +true, replacing perl-style variables with english variables will break.

    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: use_english_names (default)

    +
    +
    +
    # good
    +require 'English' # or this could be in another file.
    +
    +puts $LOAD_PATH
    +puts $LOADED_FEATURES
    +puts $PROGRAM_NAME
    +puts $ERROR_INFO
    +puts $ERROR_POSITION
    +puts $FIELD_SEPARATOR # or $FS
    +puts $OUTPUT_FIELD_SEPARATOR # or $OFS
    +puts $INPUT_RECORD_SEPARATOR # or $RS
    +puts $OUTPUT_RECORD_SEPARATOR # or $ORS
    +puts $INPUT_LINE_NUMBER # or $NR
    +puts $LAST_READ_LINE
    +puts $DEFAULT_OUTPUT
    +puts $DEFAULT_INPUT
    +puts $PROCESS_ID # or $PID
    +puts $CHILD_STATUS
    +puts $LAST_MATCH_INFO
    +puts $IGNORECASE
    +puts $ARGV # or ARGV
    +
    +
    +
    +
    +

    EnforcedStyle: use_perl_names

    +
    +
    +
    # good
    +puts $:
    +puts $"
    +puts $0
    +puts $!
    +puts $@
    +puts $;
    +puts $,
    +puts $/
    +puts $\
    +puts $.
    +puts $_
    +puts $>
    +puts $<
    +puts $$
    +puts $?
    +puts $~
    +puts $=
    +puts $*
    +
    +
    +
    +
    +

    EnforcedStyle: use_builtin_english_names

    +
    +
    +
    # good
    +# Like `use_perl_names` but allows builtin global vars.
    +puts $LOAD_PATH
    +puts $LOADED_FEATURES
    +puts $PROGRAM_NAME
    +puts ARGV
    +puts $:
    +puts $"
    +puts $0
    +puts $!
    +puts $@
    +puts $;
    +puts $,
    +puts $/
    +puts $\
    +puts $.
    +puts $_
    +puts $>
    +puts $<
    +puts $$
    +puts $?
    +puts $~
    +puts $=
    +puts $*
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    RequireEnglish

    true

    Boolean

    EnforcedStyle

    use_english_names

    use_perl_names, use_english_names, use_builtin_english_names

    +
    + +
    +
    +
    +

    Style/StabbyLambdaParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.35

    -

    +
    +

    Check for parentheses around stabby lambda arguments. +There are two different styles. Defaults to require_parentheses.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: require_parentheses (default)

    +
    +
    +
    # bad
    +->a,b,c { a + b + c }
    +
    +# good
    +->(a,b,c) { a + b + c}
    +
    +
    +
    +
    +

    EnforcedStyle: require_no_parentheses

    +
    +
    +
    # bad
    +->(a,b,c) { a + b + c }
    +
    +# good
    +->a,b,c { a + b + c}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    require_parentheses

    require_parentheses, require_no_parentheses

    +
    + +
    +
    +
    +

    Style/StaticClass

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    1.3

    -

    +
    +

    Checks for places where classes with only class methods can be +replaced with a module. Classes should be used only when it makes sense to create +instances out of them.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe, because it is possible that this class is a parent +for some other subclass, monkey-patched with instance methods or +a dummy instance is instantiated from it somewhere.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class SomeClass
    +  def self.some_method
    +    # body omitted
    +  end
    +
    +  def self.some_other_method
    +    # body omitted
    +  end
    +end
    +
    +# good
    +module SomeModule
    +  module_function
    +
    +  def some_method
    +    # body omitted
    +  end
    +
    +  def some_other_method
    +    # body omitted
    +  end
    +end
    +
    +# good - has instance method
    +class SomeClass
    +  def instance_method; end
    +  def self.class_method; end
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/StderrPuts

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.51

    -

    +
    +

    Identifies places where $stderr.puts can be replaced by +warn. The latter has the advantage of easily being disabled by, +the -W0 interpreter flag or setting $VERBOSE to nil.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +$stderr.puts('hello')
    +
    +# good
    +warn('hello')
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/StringChars

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    No

    Always (Unsafe)

    1.12

    -

    +
    +

    Checks for uses of String#split with empty string or regexp literal argument.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +is actually a string. If another class has a split method with +different behavior, it would be registered as a false positive.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +string.split(//)
    +string.split('')
    +
    +# good
    +string.chars
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/StringConcatenation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.89

    1.18

    +
    +

    Checks for places where string concatenation +can be replaced with string interpolation.

    +
    +
    +

    The cop can autocorrect simple cases but will skip autocorrecting +more complex cases where the resulting code would be harder to read. +In those cases, it might be useful to extract statements to local +variables or methods which you can then interpolate in a string.

    +
    +
    + + + + + +
    + + +When concatenation between two strings is broken over multiple +lines, this cop does not register an offense; instead, +Style/LineEndConcatenation will pick up the offense if enabled. +
    +
    +
    +

    Two modes are supported: +1. aggressive style checks and corrects all occurrences of + where +either the left or right side of + is a string literal. +2. conservative style on the other hand, checks and corrects only if +left side (receiver of + method call) is a string literal. +This is useful when the receiver is some expression that returns string like Pathname +instead of a string literal.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe in aggressive mode, as it cannot be guaranteed that +the receiver is actually a string, which can result in a false positive.

    +
    +
    +
    +

    Examples

    +
    +

    Mode: aggressive (default)

    +
    +
    +
    # bad
    +email_with_name = user.name + ' <' + user.email + '>'
    +Pathname.new('/') + 'test'
    +
    +# good
    +email_with_name = "#{user.name} <#{user.email}>"
    +email_with_name = format('%s <%s>', user.name, user.email)
    +"#{Pathname.new('/')}test"
    +
    +# accepted, line-end concatenation
    +name = 'First' +
    +  'Last'
    +
    +
    +
    +
    +

    Mode: conservative

    +
    +
    +
    # bad
    +'Hello' + user.name
    +
    +# good
    +"Hello #{user.name}"
    +user.name + '!!'
    +Pathname.new('/') + 'test'
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    Mode

    aggressive

    String

    +
    + +
    +
    +
    +

    Style/StringHashKeys

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    0.52

    0.75

    +
    +

    Checks for the use of strings as keys in hashes. The use of +symbols is preferred instead.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because while symbols are preferred for hash keys, +there are instances when string keys are required.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +{ 'one' => 1, 'two' => 2, 'three' => 3 }
    +
    +# good
    +{ one: 1, two: 2, three: 3 }
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/StringLiterals

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.36

    +
    +

    Checks if uses of quotes match the configured preference.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: single_quotes (default)

    +
    +
    +
    # bad
    +"No special symbols"
    +"No string interpolation"
    +"Just text"
    +
    +# good
    +'No special symbols'
    +'No string interpolation'
    +'Just text'
    +"Wait! What's #{this}!"
    +
    +
    +
    +
    +

    EnforcedStyle: double_quotes

    +
    +
    +
    # bad
    +'Just some text'
    +'No special chars or interpolation'
    +
    +# good
    +"Just some text"
    +"No special chars or interpolation"
    +"Every string in #{project} uses double_quotes"
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    single_quotes

    single_quotes, double_quotes

    ConsistentQuotesInMultiline

    false

    Boolean

    +
    + +
    +
    +
    +

    Style/StringLiteralsInInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.27

    -

    +
    +

    Checks that quotes inside string, symbol, and regexp interpolations +match the configured preference.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: single_quotes (default)

    +
    +
    +
    # bad
    +string = "Tests #{success ? "PASS" : "FAIL"}"
    +symbol = :"Tests #{success ? "PASS" : "FAIL"}"
    +heredoc = <<~TEXT
    +  Tests #{success ? "PASS" : "FAIL"}
    +TEXT
    +regexp = /Tests #{success ? "PASS" : "FAIL"}/
    +
    +# good
    +string = "Tests #{success ? 'PASS' : 'FAIL'}"
    +symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
    +heredoc = <<~TEXT
    +  Tests #{success ? 'PASS' : 'FAIL'}
    +TEXT
    +regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
    +
    +
    +
    +
    +

    EnforcedStyle: double_quotes

    +
    +
    +
    # bad
    +string = "Tests #{success ? 'PASS' : 'FAIL'}"
    +symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
    +heredoc = <<~TEXT
    +  Tests #{success ? 'PASS' : 'FAIL'}
    +TEXT
    +regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
    +
    +# good
    +string = "Tests #{success ? "PASS" : "FAIL"}"
    +symbol = :"Tests #{success ? "PASS" : "FAIL"}"
    +heredoc = <<~TEXT
    +  Tests #{success ? "PASS" : "FAIL"}
    +TEXT
    +regexp = /Tests #{success ? "PASS" : "FAIL"}/
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    single_quotes

    single_quotes, double_quotes

    +
    +
    +
    +
    +

    Style/StringMethods

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    Always

    0.34

    0.34

    +
    +

    Enforces the use of consistent method names +from the String class.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'name'.intern
    +'var'.unfavored_method
    +
    +# good
    +'name'.to_sym
    +'var'.preferred_method
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    PreferredMethods

    {"intern"⇒"to_sym"}

    +
    +
    +
    +
    +

    Style/Strip

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    -

    +
    +

    Identifies places where lstrip.rstrip can be replaced by +strip.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'abc'.lstrip.rstrip
    +'abc'.rstrip.lstrip
    +
    +# good
    +'abc'.strip
    +
    +
    +
    +
    +
    +
    +

    Style/StructInheritance

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always (Unsafe)

    0.29

    1.20

    +
    +

    Checks for inheritance from Struct.new.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe because it will change the inheritance +tree (e.g. return value of Module#ancestors) of the constant.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Person < Struct.new(:first_name, :last_name)
    +  def age
    +    42
    +  end
    +end
    +
    +# good
    +Person = Struct.new(:first_name, :last_name) do
    +  def age
    +    42
    +  end
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/SuperArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.64

    -

    +
    +

    Checks for redundant argument forwarding when calling super with arguments identical to +the method definition.

    +
    +
    +

    Using zero arity super within a define_method block results in RuntimeError:

    +
    +
    +
    +
    def m
    +  define_method(:foo) { super() } # => OK
    +end
    +
    +def m
    +  define_method(:foo) { super }   # => RuntimeError
    +end
    +
    +
    +
    +

    Furthermore, any arguments accompanied by a block may potentially be delegating to +define_method, therefore, super used within these blocks will be allowed. +This approach might result in false negatives, yet ensuring safe detection takes precedence.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def method(*args, **kwargs)
    +  super(*args, **kwargs)
    +end
    +
    +# good - implicitly passing all arguments
    +def method(*args, **kwargs)
    +  super
    +end
    +
    +# good - forwarding a subset of the arguments
    +def method(*args, **kwargs)
    +  super(*args)
    +end
    +
    +# good - forwarding no arguments
    +def method(*args, **kwargs)
    +  super()
    +end
    +
    +# good - assigning to the block variable before calling super
    +def method(&block)
    +  # Assigning to the block variable would pass the old value to super,
    +  # under this circumstance the block must be referenced explicitly.
    +  block ||= proc { 'fallback behavior' }
    +  super(&block)
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/SuperWithArgsParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.58

    -

    +
    +

    Enforces the presence of parentheses in super containing arguments.

    +
    +
    +

    super is a keyword and is provided as a distinct cop from those designed for method call.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +super name, age
    +
    +# good
    +super(name, age)
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/SwapValues

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always (Unsafe)

    1.1

    -

    +
    +

    Enforces the use of shorthand-style swapping of 2 variables.

    +
    +
    +

    Safety

    +
    +

    Autocorrection is unsafe, because the temporary variable used to +swap variables will be removed, but may be referred to elsewhere.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +tmp = x
    +x = y
    +y = tmp
    +
    +# good
    +x, y = y, x
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/SymbolArray

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.0 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.49

    +
    +

    Checks for array literals made up of symbols that are not +using the %i() syntax.

    +
    +
    +

    Alternatively, it checks for symbol arrays using the %i() syntax on +projects which do not want to use that syntax, perhaps because they +support a version of Ruby lower than 2.0.

    +
    +
    +

    Configuration option: MinSize +If set, arrays with fewer elements than this value will not trigger the +cop. For example, a MinSize of 3 will not enforce a style on an +array of 2 or fewer elements.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: percent (default)

    +
    +
    +
    # good
    +%i[foo bar baz]
    +
    +# bad
    +[:foo, :bar, :baz]
    +
    +# bad (contains spaces)
    +%i[foo\ bar baz\ quux]
    +
    +# bad (contains [] with spaces)
    +%i[foo \[ \]]
    +
    +# bad (contains () with spaces)
    +%i(foo \( \))
    +
    +
    +
    +
    +

    EnforcedStyle: brackets

    +
    +
    +
    # good
    +[:foo, :bar, :baz]
    +
    +# bad
    +%i[foo bar baz]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    percent

    percent, brackets

    MinSize

    2

    Integer

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/SymbolLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.30

    -

    +
    +

    Checks symbol literal syntax.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +:"symbol"
    +
    +# good
    +:symbol
    +
    +
    +
    +
    +
    +
    +

    Style/SymbolProc

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.26

    1.64

    +
    +

    Use symbols as procs when possible.

    +
    +
    +

    If you prefer a style that allows block for method with arguments, +please set true to AllowMethodsWithArguments. +define_method? methods are allowed by default. +These are customizable with AllowedMethods option.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because there is a difference that a Proc +generated from Symbol#to_proc behaves as a lambda, while +a Proc generated from a block does not. +For example, a lambda will raise an ArgumentError if the +number of arguments is wrong, but a non-lambda Proc will not.

    +
    +
    +

    For example:

    +
    +
    +
    +
    class Foo
    +  def bar
    +    :bar
    +  end
    +end
    +
    +def call(options = {}, &block)
    +  block.call(Foo.new, options)
    +end
    +
    +call { |x| x.bar }
    +#=> :bar
    +call(&:bar)
    +# ArgumentError: wrong number of arguments (given 1, expected 0)
    +
    +
    +
    +

    It is also unsafe because Symbol#to_proc does not work with +protected methods which would otherwise be accessible.

    +
    +
    +

    For example:

    +
    +
    +
    +
    class Box
    +  def initialize
    +    @secret = rand
    +  end
    +
    +  def normal_matches?(*others)
    +    others.map { |other| other.secret }.any?(secret)
    +  end
    +
    +  def symbol_to_proc_matches?(*others)
    +    others.map(&:secret).any?(secret)
    +  end
    +
    +  protected
    +
    +  attr_reader :secret
    +end
    +
    +boxes = [Box.new, Box.new]
    +Box.new.normal_matches?(*boxes)
    +# => false
    +boxes.first.normal_matches?(*boxes)
    +# => true
    +Box.new.symbol_to_proc_matches?(*boxes)
    +# => NoMethodError: protected method `secret' called for #<Box...>
    +boxes.first.symbol_to_proc_matches?(*boxes)
    +# => NoMethodError: protected method `secret' called for #<Box...>
    +
    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +something.map { |s| s.upcase }
    +something.map { _1.upcase }
    +
    +# good
    +something.map(&:upcase)
    +
    +
    +
    +

    AllowMethodsWithArguments: false (default)

    +
    +
    +
    # bad
    +something.do_something(foo) { |o| o.bar }
    +
    +# good
    +something.do_something(foo, &:bar)
    +
    +
    +
    +
    +

    AllowMethodsWithArguments: true

    +
    +
    +
    # good
    +something.do_something(foo) { |o| o.bar }
    +
    +
    +
    +
    +

    AllowComments: false (default)

    +
    +
    +
    # bad
    +something.do_something do |s| # some comment
    +  # some comment
    +  s.upcase # some comment
    +  # some comment
    +end
    +
    +
    +
    +
    +

    AllowComments: true

    +
    +
    +
    # good  - if there are comment in either position
    +something.do_something do |s| # some comment
    +  # some comment
    +  s.upcase # some comment
    +  # some comment
    +end
    +
    +
    +
    +
    +

    AllowedMethods: [define_method] (default)

    +
    +
    +
    # good
    +define_method(:foo) { |foo| foo.bar }
    +
    +
    +
    +
    +

    AllowedPatterns: [] (default)

    +
    +
    +
    # bad
    +something.map { |s| s.upcase }
    +
    +
    +
    +
    +

    AllowedPatterns: ['map'] (default)

    +
    +
    +
    # good
    +something.map { |s| s.upcase }
    +
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: false (default)

    +
    +
    +
    # bad
    +->(x) { x.foo }
    +proc { |x| x.foo }
    +Proc.new { |x| x.foo }
    +
    +# good
    +lambda(&:foo)
    +proc(&:foo)
    +Proc.new(&:foo)
    +
    +
    +
    +
    +

    AllCops:ActiveSupportExtensionsEnabled: true

    +
    +
    +
    # good
    +->(x) { x.foo }
    +proc { |x| x.foo }
    +Proc.new { |x| x.foo }
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowMethodsWithArguments

    false

    Boolean

    AllowedMethods

    define_method

    Array

    AllowedPatterns

    []

    Array

    AllowComments

    false

    Boolean

    +
    +
    +
    +
    +

    Style/TernaryParentheses

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.42

    0.46

    +
    +

    Checks for the presence of parentheses around ternary +conditions. It is configurable to enforce inclusion or omission of +parentheses using EnforcedStyle. Omission is only enforced when +removing the parentheses won’t cause a different behavior.

    +
    +
    +

    AllowSafeAssignment option for safe assignment. +By safe assignment we mean putting parentheses around +an assignment to indicate "I know I’m using an assignment +as a condition. It’s not a mistake."

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: require_no_parentheses (default)

    +
    +
    +
    # bad
    +foo = (bar?) ? a : b
    +foo = (bar.baz?) ? a : b
    +foo = (bar && baz) ? a : b
    +
    +# good
    +foo = bar? ? a : b
    +foo = bar.baz? ? a : b
    +foo = bar && baz ? a : b
    +
    +
    +
    +
    +

    EnforcedStyle: require_parentheses

    +
    +
    +
    # bad
    +foo = bar? ? a : b
    +foo = bar.baz? ? a : b
    +foo = bar && baz ? a : b
    +
    +# good
    +foo = (bar?) ? a : b
    +foo = (bar.baz?) ? a : b
    +foo = (bar && baz) ? a : b
    +
    +
    +
    +
    +

    EnforcedStyle: require_parentheses_when_complex

    +
    +
    +
    # bad
    +foo = (bar?) ? a : b
    +foo = (bar.baz?) ? a : b
    +foo = bar && baz ? a : b
    +
    +# good
    +foo = bar? ? a : b
    +foo = bar.baz? ? a : b
    +foo = (bar && baz) ? a : b
    +
    +
    +
    +
    +

    AllowSafeAssignment: true (default)

    +
    +
    +
    # good
    +foo = (bar = baz) ? a : b
    +
    +
    +
    +
    +

    AllowSafeAssignment: false

    +
    +
    +
    # bad
    +foo = (bar = baz) ? a : b
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    require_no_parentheses

    require_parentheses, require_no_parentheses, require_parentheses_when_complex

    AllowSafeAssignment

    true

    Boolean

    +
    +
    +
    +
    +

    Style/TopLevelMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    1.15

    -

    +
    +

    Newcomers to ruby applications may write top-level methods, +when ideally they should be organized in appropriate classes or modules. +This cop looks for definitions of top-level methods and warns about them.

    +
    +
    +

    However for ruby scripts it is perfectly fine to use top-level methods. +Hence this cop is disabled by default.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +end
    +
    +# bad
    +def self.some_method
    +end
    +
    +# bad
    +define_method(:foo) { puts 1 }
    +
    +# good
    +module Foo
    +  def some_method
    +  end
    +end
    +
    +# good
    +class Foo
    +  def self.some_method
    +  end
    +end
    +
    +# good
    +Struct.new do
    +  def some_method
    +  end
    +end
    +
    +# good
    +class Foo
    +  define_method(:foo) { puts 1 }
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/TrailingBodyOnClass

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks for trailing code after the class definition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +class Foo; def foo; end
    +end
    +
    +# good
    +class Foo
    +  def foo; end
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/TrailingBodyOnMethodDefinition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for trailing code after the method definition.

    +
    +
    + + + + + +
    + + +It always accepts endless method definitions that are basically on the same line. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method; do_stuff
    +end
    +
    +def f(x); b = foo
    +  b[c: x]
    +end
    +
    +# good
    +def some_method
    +  do_stuff
    +end
    +
    +def f(x)
    +  b = foo
    +  b[c: x]
    +end
    +
    +def endless_method = do_stuff
    +
    +
    +
    +
    +
    +
    +

    Style/TrailingBodyOnModule

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks for trailing code after the module definition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +module Foo extend self
    +end
    +
    +# good
    +module Foo
    +  extend self
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/TrailingCommaInArguments

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.36

    -

    +
    +

    Checks for trailing comma in argument lists. +The supported styles are:

    +
    +
    +
      +
    • +

      consistent_comma: Requires a comma after the last argument, +for all parenthesized method calls with arguments.

      +
    • +
    • +

      comma: Requires a comma after the last argument, but only for +parenthesized method calls where each argument is on its own line.

      +
    • +
    • +

      no_comma: Requires that there is no comma after the last +argument.

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyleForMultiline: consistent_comma

    +
    +
    +
    # bad
    +method(1, 2,)
    +
    +# good
    +method(1, 2)
    +
    +# good
    +method(
    +  1, 2,
    +  3,
    +)
    +
    +# good
    +method(
    +  1, 2, 3,
    +)
    +
    +# good
    +method(
    +  1,
    +  2,
    +)
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: comma

    +
    +
    +
    # bad
    +method(1, 2,)
    +
    +# good
    +method(1, 2)
    +
    +# bad
    +method(
    +  1, 2,
    +  3,
    +)
    +
    +# good
    +method(
    +  1, 2,
    +  3
    +)
    +
    +# bad
    +method(
    +  1, 2, 3,
    +)
    +
    +# good
    +method(
    +  1, 2, 3
    +)
    +
    +# good
    +method(
    +  1,
    +  2,
    +)
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: no_comma (default)

    +
    +
    +
    # bad
    +method(1, 2,)
    +
    +# good
    +method(1, 2)
    +
    +# good
    +method(
    +  1,
    +  2
    +)
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleForMultiline

    no_comma

    comma, consistent_comma, no_comma

    +
    + +
    +
    +
    +

    Style/TrailingCommaInArrayLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks for trailing comma in array literals. +The configuration options are:

    +
    +
    +
      +
    • +

      consistent_comma: Requires a comma after the +last item of all non-empty, multiline array literals.

      +
    • +
    • +

      comma: Requires a comma after last item in an array, +but only when each item is on its own line.

      +
    • +
    • +

      no_comma: Does not require a comma after the +last item in an array

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyleForMultiline: consistent_comma

    +
    +
    +
    # bad
    +a = [1, 2,]
    +
    +# good
    +a = [1, 2]
    +
    +# good
    +a = [
    +  1, 2,
    +  3,
    +]
    +
    +# good
    +a = [
    +  1, 2, 3,
    +]
    +
    +# good
    +a = [
    +  1,
    +  2,
    +]
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: comma

    +
    +
    +
    # bad
    +a = [1, 2,]
    +
    +# good
    +a = [1, 2]
    +
    +# bad
    +a = [
    +  1, 2,
    +  3,
    +]
    +
    +# good
    +a = [
    +  1, 2,
    +  3
    +]
    +
    +# bad
    +a = [
    +  1, 2, 3,
    +]
    +
    +# good
    +a = [
    +  1, 2, 3
    +]
    +
    +# good
    +a = [
    +  1,
    +  2,
    +]
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: no_comma (default)

    +
    +
    +
    # bad
    +a = [1, 2,]
    +
    +# good
    +a = [
    +  1,
    +  2
    +]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleForMultiline

    no_comma

    comma, consistent_comma, no_comma

    +
    + +
    +
    +
    +

    Style/TrailingCommaInBlockArgs

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    0.81

    -

    +
    +

    Checks whether trailing commas in block arguments are +required. Blocks with only one argument and a trailing comma require +that comma to be present. Blocks with more than one argument never +require a trailing comma.

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because a trailing comma can indicate there are +more parameters that are not used.

    +
    +
    +

    For example:

    +
    +
    +
    +
    # with a trailing comma
    +{foo: 1, bar: 2, baz: 3}.map {|key,| key }
    +#=> [:foo, :bar, :baz]
    +
    +# without a trailing comma
    +{foo: 1, bar: 2, baz: 3}.map {|key| key }
    +#=> [[:foo, 1], [:bar, 2], [:baz, 3]]
    +
    +
    +
    +

    This can be fixed by replacing the trailing comma with a placeholder +argument (such as |key, _value|).

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +add { |foo, bar,| foo + bar }
    +
    +# good
    +add { |foo, bar| foo + bar }
    +
    +# good
    +add { |foo,| foo }
    +
    +# good
    +add { foo }
    +
    +# bad
    +add do |foo, bar,|
    +  foo + bar
    +end
    +
    +# good
    +add do |foo, bar|
    +  foo + bar
    +end
    +
    +# good
    +add do |foo,|
    +  foo
    +end
    +
    +# good
    +add do
    +  foo + bar
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/TrailingCommaInHashLiteral

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.53

    -

    +
    +

    Checks for trailing comma in hash literals. +The configuration options are:

    +
    +
    +
      +
    • +

      consistent_comma: Requires a comma after the +last item of all non-empty, multiline hash literals.

      +
    • +
    • +

      comma: Requires a comma after the last item in a hash, +but only when each item is on its own line.

      +
    • +
    • +

      no_comma: Does not require a comma after the +last item in a hash

      +
    • +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyleForMultiline: consistent_comma

    +
    +
    +
    # bad
    +a = { foo: 1, bar: 2, }
    +
    +# good
    +a = { foo: 1, bar: 2 }
    +
    +# good
    +a = {
    +  foo: 1, bar: 2,
    +  qux: 3,
    +}
    +
    +# good
    +a = {
    +  foo: 1, bar: 2, qux: 3,
    +}
    +
    +# good
    +a = {
    +  foo: 1,
    +  bar: 2,
    +}
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: comma

    +
    +
    +
    # bad
    +a = { foo: 1, bar: 2, }
    +
    +# good
    +a = { foo: 1, bar: 2 }
    +
    +# bad
    +a = {
    +  foo: 1, bar: 2,
    +  qux: 3,
    +}
    +
    +# good
    +a = {
    +  foo: 1, bar: 2,
    +  qux: 3
    +}
    +
    +# bad
    +a = {
    +  foo: 1, bar: 2, qux: 3,
    +}
    +
    +# good
    +a = {
    +  foo: 1, bar: 2, qux: 3
    +}
    +
    +# good
    +a = {
    +  foo: 1,
    +  bar: 2,
    +}
    +
    +
    +
    +
    +

    EnforcedStyleForMultiline: no_comma (default)

    +
    +
    +
    # bad
    +a = { foo: 1, bar: 2, }
    +
    +# good
    +a = {
    +  foo: 1,
    +  bar: 2
    +}
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyleForMultiline

    no_comma

    comma, consistent_comma, no_comma

    +
    +
    +
    +
    +

    Style/TrailingMethodEndStatement

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.52

    -

    +
    +

    Checks for trailing code after the method definition.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def some_method
    +do_stuff; end
    +
    +def do_this(x)
    +  baz.map { |b| b.this(x) } end
    +
    +def foo
    +  block do
    +    bar
    +  end end
    +
    +# good
    +def some_method
    +  do_stuff
    +end
    +
    +def do_this(x)
    +  baz.map { |b| b.this(x) }
    +end
    +
    +def foo
    +  block do
    +    bar
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Style/TrailingUnderscoreVariable

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.31

    0.35

    +
    +

    Checks for extra underscores in variable assignment.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +a, b, _ = foo()
    +a, b, _, = foo()
    +a, _, _ = foo()
    +a, _, _, = foo()
    +
    +# good
    +a, b, = foo()
    +a, = foo()
    +*a, b, _ = foo()
    +# => We need to know to not include 2 variables in a
    +a, *b, _ = foo()
    +# => The correction `a, *b, = foo()` is a syntax error
    +
    +
    +
    +

    AllowNamedUnderscoreVariables: true (default)

    +
    +
    +
    # good
    +a, b, _something = foo()
    +
    +
    +
    +
    +

    AllowNamedUnderscoreVariables: false

    +
    +
    +
    # bad
    +a, b, _something = foo()
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    AllowNamedUnderscoreVariables

    true

    Boolean

    +
    +
    +
    +
    +

    Style/TrivialAccessors

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    1.15

    +
    +

    Looks for trivial reader/writer methods, that could +have been created with the attr_* family of functions automatically. +to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, +to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, and to_sym methods +are allowed by default. These are customizable with AllowedMethods option.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +def foo
    +  @foo
    +end
    +
    +def bar=(val)
    +  @bar = val
    +end
    +
    +def self.baz
    +  @baz
    +end
    +
    +# good
    +attr_reader :foo
    +attr_writer :bar
    +
    +class << self
    +  attr_reader :baz
    +end
    +
    +
    +
    +

    ExactNameMatch: true (default)

    +
    +
    +
    # good
    +def name
    +  @other_name
    +end
    +
    +
    +
    +
    +

    ExactNameMatch: false

    +
    +
    +
    # bad
    +def name
    +  @other_name
    +end
    +
    +
    +
    +
    +

    AllowPredicates: true (default)

    +
    +
    +
    # good
    +def foo?
    +  @foo
    +end
    +
    +
    +
    +
    +

    AllowPredicates: false

    +
    +
    +
    # bad
    +def foo?
    +  @foo
    +end
    +
    +# good
    +attr_reader :foo
    +
    +
    +
    +
    +

    AllowDSLWriters: true (default)

    +
    +
    +
    # good
    +def on_exception(action)
    +  @on_exception=action
    +end
    +
    +
    +
    +
    +

    AllowDSLWriters: false

    +
    +
    +
    # bad
    +def on_exception(action)
    +  @on_exception=action
    +end
    +
    +# good
    +attr_writer :on_exception
    +
    +
    +
    +
    +

    IgnoreClassMethods: false (default)

    +
    +
    +
    # bad
    +def self.foo
    +  @foo
    +end
    +
    +# good
    +class << self
    +  attr_reader :foo
    +end
    +
    +
    +
    +
    +

    IgnoreClassMethods: true

    +
    +
    +
    # good
    +def self.foo
    +  @foo
    +end
    +
    +
    +
    +
    +

    AllowedMethods: ['allowed_method']

    +
    +
    +
    # good
    +def allowed_method
    +  @foo
    +end
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    ExactNameMatch

    true

    Boolean

    AllowPredicates

    true

    Boolean

    AllowDSLWriters

    true

    Boolean

    IgnoreClassMethods

    false

    Boolean

    AllowedMethods

    to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym

    Array

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/UnlessElse

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    -

    +
    +

    Looks for unless expressions with else clauses.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +unless foo_bar.nil?
    +  # do something...
    +else
    +  # do a different thing...
    +end
    +
    +# good
    +if foo_bar.present?
    +  # do something...
    +else
    +  # do a different thing...
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/UnlessLogicalOperators

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    Yes

    No

    1.11

    -

    +
    +

    Checks for the use of logical operators in an unless condition. +It discourages such code, as the condition becomes more difficult +to read and understand.

    +
    +
    +

    This cop supports two styles:

    +
    +
    +
      +
    • +

      forbid_mixed_logical_operators (default)

      +
    • +
    • +

      forbid_logical_operators

      +
    • +
    +
    +
    +

    forbid_mixed_logical_operators style forbids the use of more than one type +of logical operators. This makes the unless condition easier to read +because either all conditions need to be met or any condition need to be met +in order for the expression to be truthy or falsey.

    +
    +
    +

    forbid_logical_operators style forbids any use of logical operator. +This makes it even more easy to read the unless condition as +there is only one condition in the expression.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: forbid_mixed_logical_operators (default)

    +
    +
    +
    # bad
    +return unless a || b && c
    +return unless a && b || c
    +return unless a && b and c
    +return unless a || b or c
    +return unless a && b or c
    +return unless a || b and c
    +
    +# good
    +return unless a && b && c
    +return unless a || b || c
    +return unless a and b and c
    +return unless a or b or c
    +return unless a?
    +
    +
    +
    +
    +

    EnforcedStyle: forbid_logical_operators

    +
    +
    +
    # bad
    +return unless a || b
    +return unless a && b
    +return unless a or b
    +return unless a and b
    +
    +# good
    +return unless a
    +return unless a?
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    forbid_mixed_logical_operators

    forbid_mixed_logical_operators, forbid_logical_operators

    +
    +
    +
    +
    +

    Style/UnpackFirst

    +
    +
    + + + + + +
    + + +Requires Ruby version 2.4 +
    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.54

    -

    +
    +

    Checks for accessing the first element of String#unpack +which can be replaced with the shorter method unpack1.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +'foo'.unpack('h*').first
    +'foo'.unpack('h*')[0]
    +'foo'.unpack('h*').slice(0)
    +'foo'.unpack('h*').at(0)
    +
    +# good
    +'foo'.unpack1('h*')
    +
    +
    +
    +
    +
    +
    +

    Style/VariableInterpolation

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.20

    +
    +

    Checks for variable interpolation (like "#@ivar").

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +"His name is #$name"
    +/check #$pattern/
    +"Let's go to the #@store"
    +
    +# good
    +"His name is #{$name}"
    +/check #{$pattern}/
    +"Let's go to the #{@store}"
    +
    +
    +
    + +
    +
    +
    +

    Style/WhenThen

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    -

    +
    +

    Checks for when; uses in case expressions.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +case foo
    +when 1; 'baz'
    +when 2; 'bar'
    +end
    +
    +# good
    +case foo
    +when 1 then 'baz'
    +when 2 then 'bar'
    +end
    +
    +
    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/WhileUntilDo

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    -

    +
    +

    Checks for uses of do in multi-line while/until statements.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +while x.any? do
    +  do_something(x.pop)
    +end
    +
    +# good
    +while x.any?
    +  do_something(x.pop)
    +end
    +
    +# bad
    +until x.empty? do
    +  do_something(x.pop)
    +end
    +
    +# good
    +until x.empty?
    +  do_something(x.pop)
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/WhileUntilModifier

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    0.30

    +
    +

    Checks for while and until statements that would fit on one line +if written as a modifier while/until. The maximum line length is +configured in the Layout/LineLength cop.

    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +while x < 10
    +  x += 1
    +end
    +
    +# good
    +x += 1 while x < 10
    +
    +# bad
    +until x > 10
    +  x += 1
    +end
    +
    +# good
    +x += 1 until x > 10
    +
    +
    +
    +
    +
    # bad
    +x += 100 while x < 500 # a long comment that makes code too long if it were a single line
    +
    +# good
    +while x < 500 # a long comment that makes code too long if it were a single line
    +  x += 100
    +end
    +
    +
    +
    + +
    +
    +
    +

    Style/WordArray

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    Yes

    Always

    0.9

    1.19

    +
    +

    Checks for array literals made up of word-like +strings, that are not using the %w() syntax.

    +
    +
    +

    Alternatively, it can check for uses of the %w() syntax, in projects +which do not want to include that syntax.

    +
    +
    + + + + + +
    + + +When using the percent style, %w() arrays containing a space +will be registered as offenses. +
    +
    +
    +

    Configuration option: MinSize +If set, arrays with fewer elements than this value will not trigger the +cop. For example, a MinSize of 3 will not enforce a style on an +array of 2 or fewer elements.

    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: percent (default)

    +
    +
    +
    # good
    +%w[foo bar baz]
    +
    +# bad
    +['foo', 'bar', 'baz']
    +
    +# bad (contains spaces)
    +%w[foo\ bar baz\ quux]
    +
    +# bad
    +[
    +  ['one', 'One'],
    +  ['two', 'Two']
    +]
    +
    +# good
    +[
    +  %w[one One],
    +  %w[two Two]
    +]
    +
    +# good (2d array containing spaces)
    +[
    +  ['one', 'One'],
    +  ['two', 'Two'],
    +  ['forty two', 'Forty Two']
    +]
    +
    +
    +
    +
    +

    EnforcedStyle: brackets

    +
    +
    +
    # good
    +['foo', 'bar', 'baz']
    +
    +# bad
    +%w[foo bar baz]
    +
    +# good (contains spaces)
    +['foo bar', 'baz quux']
    +
    +# good
    +[
    +  ['one', 'One'],
    +  ['two', 'Two']
    +]
    +
    +# bad
    +[
    +  %w[one One],
    +  %w[two Two]
    +]
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    percent

    percent, brackets

    MinSize

    2

    Integer

    WordRegex

    (?-mix:\A(?:\p{Word}|\p{Word}-\p{Word}|\n|\t)+\z)

    +
    +
    +

    References

    + +
    +
    +
    +
    +

    Style/YAMLFileRead

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Pending

    Yes

    Always

    1.53

    -

    +
    +

    Checks for the use of YAML.load, YAML.safe_load, and YAML.parse with +File.read argument.

    +
    +
    + + + + + +
    + + +YAML.safe_load_file was introduced in Ruby 3.0. +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +YAML.load(File.read(path))
    +YAML.parse(File.read(path))
    +
    +# good
    +YAML.load_file(path)
    +YAML.parse_file(path)
    +
    +# bad
    +YAML.safe_load(File.read(path)) # Ruby 3.0 and newer
    +
    +# good
    +YAML.safe_load_file(path)       # Ruby 3.0 and newer
    +
    +
    +
    +
    +
    +
    +

    Style/YodaCondition

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.49

    0.75

    +
    +

    Enforces or forbids Yoda conditions, +i.e. comparison operations where the order of expression is reversed. +eg. 5 == x

    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because comparison operators can be defined +differently on different classes, and are not guaranteed to +have the same result if reversed.

    +
    +
    +

    For example:

    +
    +
    +
    +
    class MyKlass
    +  def ==(other)
    +    true
    +  end
    +end
    +
    +obj = MyKlass.new
    +obj == 'string'   #=> true
    +'string' == obj   #=> false
    +
    +
    +
    +
    +

    Examples

    +
    +

    EnforcedStyle: forbid_for_all_comparison_operators (default)

    +
    +
    +
    # bad
    +99 == foo
    +"bar" != foo
    +42 >= foo
    +10 < bar
    +99 == CONST
    +
    +# good
    +foo == 99
    +foo == "bar"
    +foo <= 42
    +bar > 10
    +CONST == 99
    +"#{interpolation}" == foo
    +/#{interpolation}/ == foo
    +
    +
    +
    +
    +

    EnforcedStyle: forbid_for_equality_operators_only

    +
    +
    +
    # bad
    +99 == foo
    +"bar" != foo
    +
    +# good
    +99 >= foo
    +3 < a && a < 5
    +
    +
    +
    +
    +

    EnforcedStyle: require_for_all_comparison_operators

    +
    +
    +
    # bad
    +foo == 99
    +foo == "bar"
    +foo <= 42
    +bar > 10
    +
    +# good
    +99 == foo
    +"bar" != foo
    +42 >= foo
    +10 < bar
    +
    +
    +
    +
    +

    EnforcedStyle: require_for_equality_operators_only

    +
    +
    +
    # bad
    +99 >= foo
    +3 < a && a < 5
    +
    +# good
    +99 == foo
    +"bar" != foo
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + + + + + + + + +
    NameDefault valueConfigurable values

    EnforcedStyle

    forbid_for_all_comparison_operators

    forbid_for_all_comparison_operators, forbid_for_equality_operators_only, require_for_all_comparison_operators, require_for_equality_operators_only

    +
    + +
    +
    +
    +

    Style/YodaExpression

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Disabled

    No

    Always (Unsafe)

    1.42

    1.43

    +
    +

    Forbids Yoda expressions, i.e. binary operations (using *, +, &, |, +and ^ operators) where the order of expression is reversed, eg. 1 + x. +This cop complements Style/YodaCondition cop, which has a similar purpose.

    +
    +
    +

    This cop is disabled by default to respect user intentions such as:

    +
    +
    +
    +
    config.server_port = 9000 + ENV["TEST_ENV_NUMBER"].to_i
    +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because binary operators can be defined +differently on different classes, and are not guaranteed to +have the same result if reversed.

    +
    +
    +
    +

    Examples

    +
    +

    SupportedOperators: ['*', '+', '&', '|', '^'] (default)

    +
    +
    +
    # bad
    +10 * y
    +1 + x
    +1 & z
    +1 | x
    +1 ^ x
    +1 + CONST
    +
    +# good
    +y * 10
    +x + 1
    +z & 1
    +x | 1
    +x ^ 1
    +CONST + 1
    +60 * 24
    +
    +
    +
    +
    +
    +

    Configurable attributes

    + +++++ + + + + + + + +
    NameDefault valueConfigurable values
    +
    +
    +
    +
    +

    Style/ZeroLengthPredicate

    +
    + +++++++ + + + + + + + + + + + + + + + + + + +
    Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed

    Enabled

    No

    Always (Unsafe)

    0.37

    0.39

    +
    +

    Checks for numeric comparisons that can be replaced +by a predicate method, such as receiver.length == 0, +receiver.length > 0, and receiver.length != 0, +receiver.length < 1 and receiver.size == 0 that can be +replaced by receiver.empty? and !receiver.empty?.

    +
    +
    + + + + + +
    + + +File, Tempfile, and StringIO do not have empty? +so allow size == 0 and size.zero?. +
    +
    +
    +

    Safety

    +
    +

    This cop is unsafe because it cannot be guaranteed that the receiver +has an empty? method that is defined in terms of length. If there +is a non-standard class that redefines length or empty?, the cop +may register a false positive.

    +
    +
    +
    +

    Examples

    +
    +
    +
    # bad
    +[1, 2, 3].length == 0
    +0 == "foobar".length
    +array.length < 1
    +{a: 1, b: 2}.length != 0
    +string.length > 0
    +hash.size > 0
    +
    +# good
    +[1, 2, 3].empty?
    +"foobar".empty?
    +array.empty?
    +!{a: 1, b: 2}.empty?
    +!string.empty?
    +!hash.empty?
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/development.html b/docs/rubocop/1.69/development.html new file mode 100644 index 000000000..999cd61f5 --- /dev/null +++ b/docs/rubocop/1.69/development.html @@ -0,0 +1,1347 @@ + + + + + + Development :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Development

    +
    +
    +
    +

    This section of the documentation will teach you how to develop new cops. We’ll +start with generating a cop template and then we’ll address the various aspects +of its implementation (interacting with the AST, autocorrect, configuration) +and testing.

    +
    +
    +
    +
    +

    Create a new cop

    +
    +
    + + + + + +
    + + +Clone the repository and run bundle install if not done yet. +The following rake task can only be run inside the rubocop project directory itself. +
    +
    +
    +

    Use the bundled rake task new_cop to generate a cop template:

    +
    +
    +
    +
    $ bundle exec rake 'new_cop[Department/Name]'
    +[create] lib/rubocop/cop/department/name.rb
    +[create] spec/rubocop/cop/department/name_spec.rb
    +[modify] lib/rubocop.rb - `require_relative 'rubocop/cop/department/name'` was injected.
    +[modify] A configuration for the cop is added into config/default.yml.
    +Do 4 steps:
    +  1. Modify the description of Department/Name in config/default.yml
    +  2. Implement your new cop in the generated file!
    +  3. Commit your new cop with a message such as
    +     e.g. "Add new `Department/Name` cop"
    +  4. Run `bundle exec rake changelog:new` to generate a changelog entry
    +     for your new cop.
    +
    +
    +
    +
    +
    +

    Basics

    +
    +
    +

    RuboCop uses the parser library to create the +Abstract Syntax Tree (AST) representation of the code.

    +
    +
    +

    You can install parser gem and use ruby-parse command line utility to check +what the AST looks like in the output.

    +
    +
    +
    +
    $ gem install parser
    +
    +
    +
    +

    And then try to parse a simple integer representation with ruby-parse:

    +
    +
    +
    +
    $ ruby-parse -e '1'
    +(int 1)
    +
    +
    +
    +

    Each expression surrounded by parentheses represents a node in the AST. The first +element is the node type and the tail contains the children with all +information needed to represent the code.

    +
    +
    +

    Here’s another example - a local variable name being assigned the +string value "John":

    +
    +
    +
    +
    $ ruby-parse -e 'name = "John"'
    +(lvasgn :name
    +  (str "John"))
    +
    +
    +
    +

    Inspecting the AST representation

    +
    +

    Let’s imagine we want to simplify statements from !array.empty? to +array.any?:

    +
    +
    +

    First, check what the bad code returns in the Abstract Syntax Tree +representation.

    +
    +
    +
    +
    $ ruby-parse -e '!array.empty?'
    +(send
    +  (send
    +    (send nil :array) :empty?) :!)
    +
    +
    +
    +

    Now, it’s time to debug our expression using the REPL from RuboCop:

    +
    +
    +
    +
    $ bin/console
    +
    +
    +
    +

    First we need to declare the code that we want to match, and use the +ProcessedSource +that is a simple wrap to make the parser interpret the code and build the AST:

    +
    +
    +
    +
    code = '!something.empty?'
    +source = RuboCop::ProcessedSource.new(code, RUBY_VERSION.to_f)
    +node = source.ast
    +# => s(:send, s(:send, s(:send, nil, :something), :empty?), :!)
    +
    +
    +
    +

    The node has a few attributes that can be useful in the journey:

    +
    +
    +
    +
    node.type # => :send
    +node.children # => [s(:send, s(:send, nil, :something), :empty?), :!]
    +node.source # => "!something.empty?"
    +
    +
    +
    +
    +
    +
    +

    Implementation

    +
    +
    +

    Writing Node Pattern Rules

    +
    + + + + + +
    + + +You can write cops without using NodePattern (and many older cops don’t use it), but it +generally simplifies a lot the code, as manual node matching and destructuring can be +quite verbose. +
    +
    +
    +

    Now that you’re familiar with AST, you can learn a bit about the +node pattern +and use patterns to match with specific nodes that you want to match.

    +
    +
    +

    You can learn more about Node Pattern here.

    +
    +
    +

    Node pattern matches something very similar to the current output from AST +representation, then let’s start with something very generic:

    +
    +
    +
    +
    NodePattern.new('send').match(node) # => true
    +
    +
    +
    +

    It matches because the root is a send type. Now lets match it deeply using +parentheses to define details for sub-nodes. If you don’t care about what an internal +node is, you can use ... to skip it and just consider " a node".

    +
    +
    +
    +
    NodePattern.new('(send ...)').match(node) # => true
    +NodePattern.new('(send (send ...) :!)').match(node) # => true
    +NodePattern.new('(send (send (send ...) :empty?) :!)').match(node) # => true
    +
    +
    +
    +

    Sometimes it’s hard to comprehend complex expressions you’re building with the +pattern, then, if you got lost with the node pattern parens surrounding deeply, +try to use the $ to capture the internal expression and check exactly each +piece of the expression:

    +
    +
    +
    +
    NodePattern.new('(send (send (send $...) :empty?) :!)').match(node) # => [nil, :something]
    +
    +
    +
    +

    It’s not needed to strictly receive a send in the internal node because maybe +it can also be a literal array like:

    +
    +
    +
    +
    ![].empty?
    +
    +
    +
    +

    The code above has the following representation:

    +
    +
    +
    +
    => s(:send, s(:send, s(:array), :empty?), :!)
    +
    +
    +
    +

    It’s possible to skip the internal node with ... to make sure that it’s just +another internal node:

    +
    +
    +
    +
    NodePattern.new('(send (send (...) :empty?) :!)').match(node) # => true
    +
    +
    +
    +

    In other words, it says: "Match code calling !<expression>.empty?".

    +
    +
    +

    Great! Now, lets implement our cop to simplify such statements:

    +
    +
    +
    +
    $ rake 'new_cop[Style/SimplifyNotEmptyWithAny]'
    +
    +
    +
    +

    After the cop scaffold is generated, change the node matcher to match with +the expression achieved previously:

    +
    +
    +
    +
    def_node_matcher :not_empty_call?, <<~PATTERN
    +  (send (send $(...) :empty?) :!)
    +PATTERN
    +
    +
    +
    +

    Note that we added a $ sign to capture the "expression" in !<expression>.empty?, +it will become useful later.

    +
    +
    +

    Get yourself familiar with the AST node hooks that +parser +and rubocop-ast +provide.

    +
    +
    +

    As it starts with a send type, it’s needed to implement the on_send method, as the +cop scaffold already suggested:

    +
    +
    +
    +
    def on_send(node)
    +  return unless not_empty_call?(node)
    +
    +  add_offense(node)
    +end
    +
    +
    +
    +

    The on_send callback is the most used and can be optimized by restricting the acceptable +method names with a constant RESTRICT_ON_SEND.

    +
    +
    +

    And the final cop code will look like something like this:

    +
    +
    +
    +
    module RuboCop
    +  module Cop
    +    module Style
    +      # `array.any?` is a simplified way to say `!array.empty?`
    +      #
    +      # @example
    +      #   # bad
    +      #   !array.empty?
    +      #
    +      #   # good
    +      #   array.any?
    +      #
    +      class SimplifyNotEmptyWithAny < Base
    +        MSG = 'Use `.any?` and remove the negation part.'.freeze
    +        RESTRICT_ON_SEND = [:!].freeze # optimization: don't call `on_send` unless
    +                                       # the method name is in this list
    +
    +        def_node_matcher :not_empty_call?, <<~PATTERN
    +          (send (send $(...) :empty?) :!)
    +        PATTERN
    +
    +        def on_send(node)
    +          return unless not_empty_call?(node)
    +
    +          add_offense(node)
    +        end
    +      end
    +    end
    +  end
    +end
    +
    +
    +
    +

    Note that on_send will be called on a given node before the callbacks on_<some type> for its children are called. There’s also a callback after_send that is called after the children are processed. There’s a similar after_<some type> callback for all types, except those that never have children.

    +
    +
    +

    Update the spec to cover the expected syntax:

    +
    +
    +
    +
    describe RuboCop::Cop::Style::SimplifyNotEmptyWithAny, :config do
    +  it 'registers an offense when using `!a.empty?`' do
    +    expect_offense(<<~RUBY)
    +      !array.empty?
    +      ^^^^^^^^^^^^^ Use `.any?` and remove the negation part.
    +    RUBY
    +  end
    +
    +  it 'does not register an offense when using `.any?` or `.empty?`' do
    +    expect_no_offenses(<<~RUBY)
    +      array.any?
    +      array.empty?
    +    RUBY
    +  end
    +end
    +
    +
    +
    +

    If your code has variables of different lengths, you can use %{foo}, +^{foo}, and _{foo} to format your template; you can also abbreviate +offense messages with […​]:

    +
    +
    +
    +
    %w[raise fail].each do |keyword|
    +  expect_offense(<<~RUBY, keyword: keyword)
    +    %{keyword}(RuntimeError, msg)
    +    ^{keyword}^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument [...]
    +  RUBY
    +
    +%w[has_one has_many].each do |type|
    +  expect_offense(<<~RUBY, type: type)
    +    class Book
    +      %{type} :chapter, foreign_key: 'book_id'
    +      _{type}           ^^^^^^^^^^^^^^^^^^^^^^ Specifying the default [...]
    +    end
    +  RUBY
    +end
    +
    +
    +
    +
    +

    Autocorrect

    +
    +

    The autocorrect can help humans automatically fix offenses that have been detected. +It’s necessary to extend AutoCorrector. +The method add_offense yields a corrector object that is a thin wrapper on +parser’s TreeRewriter +to which you can give instructions about what to do with the +offensive node.

    +
    +
    +

    Let’s start with a simple spec to cover it:

    +
    +
    +
    +
    it 'corrects `!a.empty?`' do
    +  expect_offense(<<~RUBY)
    +    !array.empty?
    +    ^^^^^^^^^^^^^ Use `.any?` and remove the negation part.
    +  RUBY
    +
    +  expect_correction(<<~RUBY)
    +    array.any?
    +  RUBY
    +end
    +
    +
    +
    +

    And then add the autocorrecting block on the cop side:

    +
    +
    +
    +
    extend AutoCorrector
    +
    +def on_send(node)
    +  expression = not_empty_call?(node)
    +  return unless expression
    +
    +  add_offense(node) do |corrector|
    +    corrector.replace(node, "#{expression.source}.any?")
    +  end
    +end
    +
    +
    +
    +

    The corrector allows you to insert_after, insert_before, wrap or +replace a specific node or in any specific range of the code.

    +
    +
    +

    Range can be determined on node.location where it brings specific +ranges for expression or other internal information that the node holds.

    +
    +
    +

    Preventing Clobbering

    +
    +

    The corrector detects and prevents correcting overlapping nodes, to prevent one correction from clobbering another. +Supporting nested corrections is done by taking multiple passes, and skipping corrections for nested nodes. +This can be implemented using the IgnoredNode module:

    +
    +
    +
    +
     extend AutoCorrector
    ++include IgnoredNode
    +
    + def on_send(node)
    +   return unless some_condition?(node)
    +
    +   add_offense(node) do |corrector|
    ++    next if part_of_ignored_node?(node)
    ++
    +     corrector.replace(node, "...")
    +   end
    ++
    ++  ignore_node(node)
    + end
    +
    +
    +
    +

    This works because the correcting a file is implemented by repeating investigation and correction until the file no longer requires correction, meaning all nested nodes will eventually be processed.

    +
    +
    +

    Note that expect_correction in Cop specs only asserts the result after one pass.

    +
    +
    +
    +
    +

    Limit by Ruby or Gem versions

    +
    +

    Some cops apply changes that only apply in particular contexts, such as if the user has a minimum Ruby version. There are helpers that let you constrain your cops automatically, to only run where applicable.

    +
    +
    +

    Requiring a minimum Ruby version

    +
    +

    If your cop uses new Ruby syntax or standard library APIs, it should only autocorrect if the user has the target Ruby version, which you set with TargetRubyVersion#minimum_target_ruby_version.

    +
    +
    +

    For example, the Performance/SelectMap cop requires Ruby 2.7, which introduced Enumerable#filter_map:

    +
    +
    +
    +
    module RuboCop::Cop::Performance::SelectMap < Base
    +  extend TargetRubyVersion
    +
    +  minimum_target_ruby_version 2.7
    +
    +  # ...
    +end
    +
    +
    +
    +

    This cop won’t autocorrect on Ruby 2.6 or older, and it won’t even report an offense (since there’s no better alternative to recommend instead).

    +
    +
    +
    +

    Requiring a gem

    +
    +

    If your cop depends on the presence of a gem, you can declare that with RuboCop::Cop::Base.requires_gem.

    +
    +
    +

    For example, to declare that MyCop should only apply if the bundle is using my-gem with a version between 1.2.3 and 4.5.6:

    +
    +
    +
    +
    class MyCop < Base
    +  requires_gem "my-gem", ">= 1.2.3", "< 4.5.6"
    +
    +  # ...
    +end
    +
    +
    +
    +

    You can specify any gem requirement using the same syntax as your Gemfile.

    +
    +
    +
    +

    Special case: Rails

    +
    +

    Historically, many cops in rubocop-rails aren’t actually specific to Rails itself, but some of its components (e.g. ActiveSupport). These dependencies are declared with TargetRailsVersion.minimum_target_rails_version.

    +
    +
    +

    For example, the Rails/Pluck cop requires ActiveSupport 6.0, which introduces Enumerable#pluck:

    +
    +
    +
    +
    module RuboCop::Cop::Rails::Pluck < Base
    +  extend TargetRailsVersion
    +
    +  minimum_target_rails_version 6.0
    +
    +  #...
    +end
    +
    +
    +
    +
    +
    +

    Run tests

    +
    +

    RuboCop supports two parser engines: the Parser gem and Prism. By default, tests are executed with the Parser:

    +
    +
    +
    +
    $ bundle exec rake spec
    +
    +
    +
    +

    To run all tests with the experimental support for Prism, use bundle exec prism_spec, and to execute tests for individual files, +specify the environment variable PARSER_ENGINE=parser_prism.

    +
    +
    +

    e.g., PARSER_ENGINE=parser_prism spec/rubocop/cop/style/hash_syntax_spec.rb

    +
    +
    +

    bundle exec rake runs tests for both Parser gem and Prism parsers.

    +
    +
    +

    But ascii_spec rake task does not run by default. Because ascii_spec task has not been failing for a while. +However, ascii_spec task will continue to be checked in CI.

    +
    +
    +

    In CI, all tests required for merging are executed. Please investigate if anything fails.

    +
    +
    +
    +

    Configuration

    +
    +

    Each cop can hold a configuration and you can refer to cop_config in the +instance and it will bring a hash with options declared in the .rubocop.yml +file.

    +
    +
    +

    For example, lets imagine we want to make configurable to make the replacement +works with other method than .any?:

    +
    +
    +
    +
    Style/SimplifyNotEmptyWithAny:
    +  Enabled: true
    +  ReplaceAnyWith: "size > 0"
    +
    +
    +
    +

    And then on the autocorrect method, you just need to use the cop_config it:

    +
    +
    +
    +
    def on_send(node)
    +  expression = not_empty_call?(node)
    +  return unless expression
    +
    +  add_offense(node) do |corrector|
    +    replacement = cop_config['ReplaceAnyWith'] || 'any?'
    +    corrector.replace(node, "#{expression.source}.#{replacement}")
    +  end
    +end
    +
    +
    +
    +
    +
    +
    +

    Documentation

    +
    +
    +

    Every new cop requires explanation and examples to make it easy for the community +to understand its purpose. This documentation is generated by yard and is added +directly into the cop.rb file. For every SupportedStyle and unique +configuration you have included in the cop, there needs to be examples. Examples must +have valid Ruby syntax. Do not use upticks.

    +
    +
    +
    +
    module Department
    +  # Description of your cop. Include description of ALL config options. Particularly
    +  # ones that take booleans and arrays, because we generally do not show examples for
    +  # configs with these value types.
    +  #
    +  # @example EnforcedStyle: bar
    +  #   # Description about this particular option
    +  #
    +  #   # bad
    +  #   bad_example1
    +  #   bad_example2
    +  #
    +  #   # good
    +  #   good_example1
    +  #   good_example2
    +  #
    +  # @example EnforcedStyle: foo (default)
    +  #   # Description about this particular option
    +  #
    +  #   # bad
    +  #   bad_example1
    +  #   bad_example2
    +  #
    +  #   # good
    +  #   good_example1
    +  #   good_example2
    +  #
    +  # @example AnyUniqueConfigKeyThatIsAString: qux (default)
    +  #   # Description about this particular option
    +  #
    +  #   # bad
    +  #   bad_example1
    +  #   bad_example2
    +  #
    +  #   # good
    +  #   good_example1
    +  #   good_example2
    +  #
    +  # @example AnyUniqueConfigKeyThatIsAString: thud
    +  #   # Description about this particular option
    +  #
    +  #   # bad
    +  #   bad_example1
    +  #   bad_example2
    +  #
    +  #   # good
    +  #   good_example1
    +  #   good_example2
    +  #
    +  class YourCop
    +    # ...
    +
    +
    +
    +

    Take note of the placement and spacing of all the documentation pieces. Such as config +keys being in alphabetical order, the (default) being specified, and one empty line +before class YourCop. While not all examples in the codebase follow this exact format, +we strive to make this consistent. PRs improving RuboCop documentation are very welcome.

    +
    +
    +
    +
    +

    Testing your cop in a real codebase

    +
    +
    +

    It’s generally good practice to check if your cop is working properly over a +significant codebase (e.g. Rails or some big project you’re working on) to +guarantee it’s working in a range of different syntaxes.

    +
    +
    +

    There are several ways to do this. Two common approaches:

    +
    +
    +
      +
    1. +

      From within your local rubocop repo, run exe/rubocop ~/your/other/codebase.

      +
    2. +
    3. +

      From within the other codebase’s Gemfile, set a path to your local repo like this: gem 'rubocop', path: '/full/path/to/rubocop'. Then run rubocop within your codebase.

      +
    4. +
    +
    +
    +

    With approach #2, you can use local versions of RuboCop extension repos such as rubocop-rspec as well.

    +
    +
    +

    To make it fast and do not get confused with other cops in action, you can use +--only parameter in the command line to filter by your cop name:

    +
    +
    +
    +
    $ rubocop --only Style/SimplifyNotEmptyWithAny
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/extensions.html b/docs/rubocop/1.69/extensions.html new file mode 100644 index 000000000..e48f586fb --- /dev/null +++ b/docs/rubocop/1.69/extensions.html @@ -0,0 +1,1072 @@ + + + + + + Extensions :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Extensions

    +
    +
    +
    +

    It’s possible to extend RuboCop with custom (third-party) cops and formatters.

    +
    +
    +
    +
    +

    Loading Extensions

    +
    +
    +

    Besides the --require command line option you can also specify ruby +files that should be loaded with the optional require directive in the +.rubocop.yml file:

    +
    +
    +
    +
    require:
    + - ../my/custom/file.rb
    + - rubocop-extension
    +
    +
    +
    + + + + + +
    + + +The paths are directly passed to Kernel.require. If your +extension file is not in $LOAD_PATH, you need to specify the path as +relative path prefixed with ./ explicitly or absolute path. Paths +starting with a . are resolved relative to .rubocop.yml. +If a path containing - is given, it will be used as is, but if we +cannot find the file to load, we will replace - with / and try it +again as when Bundler loads gems. +
    +
    +
    +
    +
    +

    Extension Suggestions

    +
    +
    +

    Depending on what gems you have in your bundle, RuboCop might suggest extensions +that can be added to provide further functionality. For instance, if you are using +rspec without the corresponding rubocop-rspec extension, RuboCop will suggest +enabling it.

    +
    +
    +

    This message can be disabled by adding the following to your configuration:

    +
    +
    +
    +
    AllCops:
    +  SuggestExtensions: false
    +
    +
    +
    +

    Suggest default extensions if SuggestExtensions: true.

    +
    +
    +

    You can also opt-out of suggestions for a particular extension library as so (unspecified +extensions will continue to be notified, as appropriate):

    +
    +
    +
    +
    AllCops:
    +  SuggestExtensions:
    +    rubocop-rake: false
    +
    +
    +
    +
    +
    +

    Custom Cops

    +
    +
    +

    You can configure the custom cops in your .rubocop.yml just like any +other cop.

    +
    +
    +

    Writing your own Cops

    +
    +

    If you’d like to create an extension gem, you can use rubocop-extension-generator.

    +
    +
    +

    See development to learn how to implement a cop.

    +
    +
    +
    +

    Cop Extensions

    +
    +

    The main RuboCop gem focuses on the core Ruby language and doesn’t +include functionality related to any external Ruby libraries/frameworks. +There are, however, many RuboCop extensions dedicated to those and +a few of them are maintained by RuboCop’s Core Team.

    +
    +
    +

    Official Extensions

    +
    + +
    +
    +
    +

    Third-party Extensions

    +
    + +
    +
    +

    Any extensions missing? Send us a Pull Request!

    +
    +
    +
    +
    +
    +
    +

    Configuring an Extension

    +
    +
    +

    If you’re writing an extension, you can tie some of its configuration into RuboCop.

    +
    +
    +

    Config Obsoletions

    +
    +

    When a cop that has been released is later renamed or removed, or one of its parameters is, RuboCop can output error messages letting users know to update their configuration to the newest values. If any obsolete configurations are encountered, RuboCop will output an error message and quit.

    +
    +
    +

    You can tie your extension into this system by creating your own obsoletions.yml file and letting RuboCop know where to find it:

    +
    +
    +
    +
    RuboCop::ConfigObsoletion.files << File.expand_path(filename)
    +
    +
    +
    +

    There are currently three types of obsoletions that can be defined for cops:

    +
    +
    +
      +
    • +

      renamed: A cop was changed to have a new name, or moved to a different department.

      +
    • +
    • +

      removed: A cop was deleted (usually this is configured with alternatives or a reason why it was removed).

      +
    • +
    • +

      split: A cop was removed and replaced with multiple other cops.

      +
    • +
    +
    +
    +

    Two additional types are available to be defined for parameter changes. These configurations can apply to multiple cops and multiple parameters at the same time (so are expressed in YAML as an array of hashes):

    +
    +
    +
      +
    • +

      changed_parameters: A parameter has been renamed.

      +
    • +
    • +

      changed_enforced_styles: A previously accepted EnforcedStyle value has been changed or removed.

      +
    • +
    +
    +
    + + + + + +
    + + +Parameter obsoletions can be set with severity: warning to deprecate an old parameter but still accept it. RuboCop will output a warning but continue to run. +
    +
    +
    +

    Example Obsoletion Configuration

    +
    +

    See config/obsoletion.yml for more examples.

    +
    +
    + + + + + +
    + + +All plural keys (eg. cops, parameters, alternatives, etc.) can either take a single value or an array. +
    +
    +
    +
    +
    renamed:
    +  Layout/AlignArguments: Layout/ArgumentAlignment
    +  Lint/BlockAlignment: Layout/BlockAlignment
    +
    +removed:
    +  Layout/SpaceAfterControlKeyword:
    +    alternatives: Layout/SpaceAroundKeyword
    +  Lint/InvalidCharacterLiteral:
    +    reason: it was never being actually triggered
    +
    +split:
    +  Style/MethodMissing:
    +    alternatives:
    +      - Style/MethodMissingSuper
    +      - Style/MissingRespondToMissing
    +
    +changed_parameters: # must be an array of hashes
    +  - cops:
    +      - Metrics/BlockLength
    +      - Metrics/MethodLength
    +    parameters: ExcludedMethods
    +    alternative: IgnoredMethods
    +    severity: warning
    +
    +changed_enforced_styles: # must be an array of hashes
    +  - cops: Layout/IndentationConsistency
    +    parameters: EnforcedStyle
    +    value: rails
    +    reason: >
    +      `EnforcedStyle: rails` has been renamed to
    +      `EnforcedStyle: indented_internal_methods`
    +
    +
    +
    +
    +
    +
    +
    +

    Custom Formatters

    +
    +
    +

    You can customize RuboCop’s output format with custom formatters.

    +
    +
    +

    Creating a Custom Formatter

    +
    +

    To implement a custom formatter, you need to subclass +RuboCop::Formatter::BaseFormatter and override some methods, +or implement all formatter API methods by duck typing.

    +
    +
    +

    Please see the documents below for more formatter API details.

    +
    + +
    +
    +

    Using a Custom Formatter from the Command Line

    +
    +

    You can tell RuboCop to use your custom formatter with a combination of +--format and --require option. +For example, when you have defined MyCustomFormatter in +./path/to/my_custom_formatter.rb, you would type this command:

    +
    +
    +
    +
    $ rubocop --require ./path/to/my_custom_formatter --format MyCustomFormatter
    +
    +
    +
    +
    +
    +
    +

    Template support

    +
    +
    +

    RuboCop has API for extensions to support templates such as ERB, Haml, Slim, etc.

    +
    +
    +

    Normally, RuboCop extracts one Ruby code from one Ruby file, however there are multiple embedded Ruby codes in one template file. To solve this problem, RuboCop has a mechanism called RuboCop::Runner.ruby_extractors, to which any Ruby extractor can be added on the extension side.

    +
    +
    +

    Ruby extractor must be a callable object that takes a RuboCop::ProcessedSource and returns an Array of Hash that contains Ruby source codes and their offsets from original source code, or returns nil for unrelated file.

    +
    +
    +
    +
    ruby_extractor.call(processed_source)
    +
    +
    +
    +

    An example returned value from a Ruby extractor would be as follows:

    +
    +
    +
    +
    [
    +  {
    +    offset: 2,
    +    processed_source: #<RuboCop::ProcessedSource>
    +  },
    +  {
    +    offset: 10,
    +    processed_source: #<RuboCop::ProcessedSource>
    +  }
    +]
    +
    +
    +
    +

    On the extension side, the code would be something like this:

    +
    +
    +
    +
    RuboCop::Runner.ruby_extractors.unshift(ruby_extractor)
    +
    +
    +
    +

    RuboCop::Runners.ruby_extractors is processed from the beginning and ends when one of them returns a non-nil value. By default, there is a Ruby extractor that returns the given Ruby source code with offset 0, so you can unshift any Ruby extractor before it.

    +
    +
    + + + + + +
    + + +This is still an experimental feature and may change in the future. +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/formatters.html b/docs/rubocop/1.69/formatters.html new file mode 100644 index 000000000..70dfdcdf4 --- /dev/null +++ b/docs/rubocop/1.69/formatters.html @@ -0,0 +1,1140 @@ + + + + + + Formatters :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Formatters

    +
    +
    +
    +

    You can change the output format of RuboCop by specifying formatters with the -f/--format option. +RuboCop ships with several built-in formatters, and also you can create your custom formatter.

    +
    +
    +

    Additionally the output can be redirected to a file instead of $stdout with the -o/--out option.

    +
    +
    +

    Some of the built-in formatters produce machine-parsable output +and they are considered public APIs. +The rest of the formatters are for humans, so parsing their outputs is discouraged.

    +
    +
    +

    You can enable multiple formatters at the same time by specifying -f/--format multiple times. +The -o/--out option applies to the previously specified -f/--format, +or the default progress format if no -f/--format is specified before the -o/--out option.

    +
    +
    +
    +
    # Simple format to $stdout.
    +$ rubocop --format simple
    +
    +# Progress (default) format to the file result.txt.
    +$ rubocop --out result.txt
    +
    +# Both progress and offense count formats to $stdout.
    +# The offense count formatter outputs only the final summary,
    +# so you'll mostly see the outputs from the progress formatter,
    +# and at the end the offense count summary will be outputted.
    +$ rubocop --format progress --format offenses
    +
    +# Progress format to $stdout and JSON format to the file rubocop.json.
    +$ rubocop --format progress --format json --out rubocop.json
    +#         ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
    +#                 |               |_______________|
    +#              $stdout
    +
    +# Progress format to result.txt and simple format to $stdout.
    +$ rubocop --out result.txt --format simple
    +#         ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
    +#                  |                 |
    +#           default format        $stdout
    +
    +
    +
    +

    You can also load custom formatters.

    +
    +
    +
    +
    +

    Progress Formatter (default)

    +
    +
    +

    The default progress formatter outputs a character for each inspected file, +and at the end it displays all detected offenses in the clang format. +A . represents a clean file, and each of the capital letters means +the severest offense (convention, warning, error, or fatal) found in a file.

    +
    +
    +
    +
    $ rubocop
    +Inspecting 26 files
    +..W.C....C..CWCW.C...WC.CC
    +
    +Offenses:
    +
    +lib/foo.rb:6:5: C: Style/Documentation: Missing top-level class documentation comment.
    +    class Foo
    +    ^^^^^
    +
    +...
    +
    +26 files inspected, 46 offenses detected
    +
    +
    +
    +
    +
    +

    Auto Gen Formatter

    +
    +
    +

    Behaves like Progress Formatter except that it will not show any offenses.

    +
    +
    +
    +
    $ rubocop --format autogenconf
    +Inspecting 26 files
    +..W.C....C..CWCW.C...WC.CC
    +
    +26 files inspected, 46 offenses detected
    +
    +
    +
    +
    +
    +

    Clang Style Formatter

    +
    +
    +

    The clang formatter displays the offenses in a manner similar to clang:

    +
    +
    +
    +
    $ rubocop --format clang test.rb
    +Inspecting 1 file
    +W
    +
    +Offenses:
    +
    +test.rb:1:5: C: Naming/MethodName: Use snake_case for method names.
    +def badName
    +    ^^^^^^^
    +test.rb:2:3: C: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
    +  if something
    +  ^^
    +test.rb:2:3: C: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    +  if something
    +  ^^
    +test.rb:4:5: W: Layout/DefEndAlignment: end at 4, 4 is not aligned with if at 2, 2
    +    end
    +    ^^^
    +
    +1 file inspected, 4 offenses detected
    +
    +
    +
    +
    +
    +

    Fuubar Style Formatter

    +
    +
    +

    The fuubar style formatter displays a progress bar +and shows details of offenses in the clang format as soon as they are detected. +This is inspired by the Fuubar formatter for RSpec.

    +
    +
    +
    +
    $ rubocop --format fuubar
    +lib/foo.rb.rb:1:1: C: Naming/MethodName: Use snake_case for method names.
    +def badName
    +    ^^^^^^^
    +lib/bar.rb:13:14: W: Lint/DeprecatedClassMethods: File.exists? is deprecated in favor of File.exist?.
    +        File.exists?(path)
    +             ^^^^^^^
    + 22/53 files |======== 43 ========>                           |  ETA: 00:00:02
    +
    +
    +
    +
    +
    +

    Pacman Style Formatter

    +
    +
    +

    The pacman style formatter prints a PACDOT per every file to be analyzed. Pacman will "eat" one PACDOT per file when no offense is detected. Otherwise it will print a Ghost. +This is inspired by the Pacman formatter for RSpec.

    +
    +
    +
    +
    $ rubocop --format pacman
    +Eating 31 files
    +src/foo.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
    +src/bar.rb:14:15: C: Style/MutableConstant: Freeze mutable objects assigned to constants.
    +      GHOST = 'ᗣ'
    +              ^^^
    +....ᗣ...ᗣ...ᗧ••••••••••••••••••
    +31 examples, 2 failures
    +
    +
    +
    +
    +
    +

    Emacs Style Formatter

    +
    +
    +

    Machine-parsable

    +
    +
    +

    The emacs formatter displays the offenses in a format suitable for consumption by Emacs (and possibly other tools).

    +
    +
    +
    +
    $ rubocop --format emacs test.rb
    +/Users/bozhidar/projects/test.rb:1:1: C: Naming/MethodName: Use snake_case for method names.
    +/Users/bozhidar/projects/test.rb:2:3: C: Style/IfUnlessModifier: Favor modifier if/unless usage when you have a single-line body. Another good alternative is the usage of control flow &&/||.
    +/Users/bozhidar/projects/test.rb:4:5: W: Layout/DefEndAlignment: end at 4, 4 is not aligned with if at 2, 2
    +
    +
    +
    +
    +
    +

    Simple Formatter

    +
    +
    +

    The name of the formatter says it all :-)

    +
    +
    +
    +
    $ rubocop --format simple test.rb
    +== test.rb ==
    +C:  1:  5: Naming/MethodName: Use snake_case for method names.
    +C:  2:  3: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
    +C:  2:  3: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    +W:  4:  5: Layout/DefEndAlignment: end at 4, 4 is not aligned with if at 2, 2
    +
    +1 file inspected, 4 offenses detected
    +
    +
    +
    +
    +
    +

    Quiet Formatter

    +
    +
    +

    Behaves like Simple Formatter if there are offenses. Completely quiet otherwise:

    +
    +
    +
    +
    $ rubocop --format quiet
    +
    +
    +
    +
    +
    +

    File List Formatter

    +
    +
    +

    Machine-parsable

    +
    +
    +

    Sometimes you might want to just open all files with offenses in your +favorite editor. This formatter outputs just the names of the files +with offenses in them and makes it possible to do something like:

    +
    +
    +
    +
    $ rubocop --format files | xargs vim
    +
    +
    +
    +
    +
    +

    JSON Formatter

    +
    +
    +

    Machine-parsable

    +
    +
    +

    You can get RuboCop’s inspection result in JSON format by passing --format json option in command line. +The JSON structure is like the following example:

    +
    +
    +
    +
    {
    +  "metadata": {
    +    "rubocop_version": "1.12.0",
    +    "ruby_engine": "ruby",
    +    "ruby_version": "3.0.0",
    +    "ruby_patchlevel": "0",
    +    "ruby_platform": "x86_64-darwin19"
    +  },
    +  "files": [{
    +      "path": "lib/foo.rb",
    +      "offenses": []
    +    }, {
    +      "path": "lib/bar.rb",
    +      "offenses": [{
    +          "severity": "convention",
    +          "message": "Line is too long. [81/80]",
    +          "cop_name": "LineLength",
    +          "corrected": true,
    +          "location": {
    +            "line": 546,
    +            "column": 80,
    +            "length": 4
    +          }
    +        }, {
    +          "severity": "warning",
    +          "message": "Unreachable code detected.",
    +          "cop_name": "UnreachableCode",
    +          "corrected": false,
    +          "location": {
    +            "line": 15,
    +            "column": 9,
    +            "length": 10
    +          }
    +        }
    +      ]
    +    }
    +  ],
    +  "summary": {
    +    "offense_count": 2,
    +    "target_file_count": 2,
    +    "inspected_file_count": 2
    +  }
    +}
    +
    +
    +
    +
    +
    +

    JUnit Style Formatter

    +
    +
    +

    Machine-parsable

    +
    +
    +

    The junit style formatter provides the JUnit formatting. +This formatter is based on the rubocop-junit-formatter gem.

    +
    +
    +
    +
    $ rubocop --format junit
    +<?xml version='1.0'?>
    +<testsuites>
    +  <testsuite name='rubocop' tests='2' failures='2'>
    +    <testcase classname='example' name='Style/FrozenStringLiteralComment'>
    +      <failure type='Style/FrozenStringLiteralComment' message='Style/FrozenStringLiteralComment: Missing frozen string literal comment.'>
    +        /tmp/src/example.rb:1:1
    +      </failure>
    +    </testcase>
    +    <testcase classname='example' name='Naming/MethodName'>
    +      <failure type='Naming/MethodName' message='Naming/MethodName: Use snake_case for method names.'>
    +        /tmp/src/example.rb:1:5
    +      </failure>
    +    </testcase>
    +    <testcase classname='example' name='Lint/DeprecatedClassMethods'>
    +      <failure type='Lint/DeprecatedClassMethods' message='Lint/DeprecatedClassMethods: `File.exists?` is deprecated in favor of `File.exist?`.'>
    +        /tmp/src/example.rb:2:8
    +      </failure>
    +    </testcase>
    +  </testsuite>
    +</testsuites>
    +
    +
    +
    +

    The junit style formatter is very useful for continuous integration systems +such as Jenkins, most of which support junit formatting when parsing test +results. A typical invocation in this type of scenario might look like:

    +
    +
    +
    +
    $ rubocop --format junit --out test-reports/junit.xml
    +
    +
    +
    +

    Since there is one XML node for each cop for each file, the size of the resulting +XML can get quite large. If it is too large for you, you can restrict the output +to just failures by adding the --display-only-failed option.

    +
    +
    +
    +
    +

    Offense Count Formatter

    +
    +
    +

    Sometimes when first applying RuboCop to a codebase, it’s nice to be able to +see where most of your style cleanup is going to be spent.

    +
    +
    +

    With this in mind, you can use the offense count formatter to outline the offended +cops and the number of offenses found for each by running:

    +
    +
    +
    +
    $ rubocop --format offenses
    +
    +36   Layout/LineLength [Safe Correctable]
    +18   Style/StringLiterals [Safe Correctable]
    +13   Style/Documentation
    +10   Style/ExpandPathArguments [Safe Correctable]
    +8    Style/EmptyMethod [Safe Correctable]
    +6    Layout/IndentationConsistency [Safe Correctable]
    +4    Lint/SuppressedException
    +3    Layout/EmptyLinesAroundAccessModifier [Safe Correctable]
    +2    Layout/ExtraSpacing [Safe Correctable]
    +1    Layout/AccessModifierIndentation [Safe Correctable]
    +1    Style/ClassAndModuleChildren [Unsafe Correctable]
    +--
    +102  Total in 31 files
    +
    +
    +
    +
    +
    +

    Worst Offenders Formatter

    +
    +
    +

    Similar to the Offense Count formatter, but lists the files which need the most attention:

    +
    +
    +
    +
    $ rubocop --format worst
    +
    +89  this/file/is/really/bad.rb
    +2   much/better.rb
    +--
    +91  Total in 2 files
    +
    +
    +
    +
    +
    +

    HTML Formatter

    +
    +
    +

    Useful for CI environments. It will create an HTML report like this.

    +
    +
    +
    +
    $ rubocop --format html -o rubocop.html
    +
    +
    +
    +
    +
    +

    Markdown Formatter

    +
    +
    +

    Useful for CI environments, especially if posting comments back to pull requests. It will create a markdown report like this.

    +
    +
    +
    +
    $ rubocop --format markdown -o rubocop.md
    +
    +
    +
    +
    +
    +

    TAP Formatter

    +
    +
    +

    Machine-parsable

    +
    +
    +

    Useful for CI environments, it will format report following the Test Anything Protocol.

    +
    +
    +
    +
    $ rubocop --format tap
    +1..3
    +not ok 1 - lib/rubocop.rb
    +# lib/rubocop.rb:2:3: C: foo
    +# This is line 2.
    +#   ^
    +ok 2 - spec/spec_helper.rb
    +not ok 3 - exe/rubocop
    +# exe/rubocop:5:2: E: bar
    +# This is line 5.
    +#  ^
    +# exe/rubocop:6:1: C: foo
    +# This is line 6.
    +# ^
    +
    +3 files inspected, 3 offenses detected
    +
    +
    +
    +
    +
    +

    GitHub Actions Formatter

    +
    +
    +

    Useful for GitHub Actions. +Formats offenses as workflow commands +to create annotations in GitHub UI.

    +
    +
    +

    The formatter uses fail_level to determine which GitHub level to use for each +annotation. By default, all RuboCop severities are errors. If fail level is +set and severity is below fail level, a warning will be created instead.

    +
    +
    +
    +
    $ rubocop --format github
    +
    +::error file=lib/foo.rb,line=6,col=5::Style/Documentation: Missing top-level class documentation comment.
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/index.html b/docs/rubocop/1.69/index.html new file mode 100644 index 000000000..b03995b00 --- /dev/null +++ b/docs/rubocop/1.69/index.html @@ -0,0 +1,794 @@ + + + + + + RuboCop :: RuboCop Docs + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    RuboCop

    +
    +
    +
    +
    +
    +

    Role models are important.

    +
    +
    +
    +— Officer Alex J. Murphy / RoboCop +
    +
    +
    +
    +
    +

    Overview

    +
    +
    +

    RuboCop is a Ruby static code analyzer (a.k.a. linter) and code +formatter. Out of the box it will enforce many of the guidelines +outlined in the community Ruby Style Guide.

    +
    +
    +

    RuboCop packs a lot of features on top of what you’d normally expect from a +linter:

    +
    +
    +
      +
    • +

      Works with every major Ruby implementation

      +
    • +
    • +

      Autocorrection of many of the code offenses it detects

      +
    • +
    • +

      Robust code formatting capabilities

      +
    • +
    • +

      Multiple result formatters for both interactive use and for feeding data into other tools

      +
    • +
    • +

      Ability to have different configuration for different parts of your codebase

      +
    • +
    • +

      Ability to disable certain cops only for specific files or parts of files

      +
    • +
    • +

      Extremely flexible configuration that allows you to adapt RuboCop to pretty much every style and preference

      +
    • +
    • +

      It’s easy to extend RuboCop with custom cops and formatters

      +
    • +
    • +

      A vast number of ready-made extensions (e.g. rubocop-rails, rubocop-rspec, rubocop-performance and rubocop-minitest)

      +
    • +
    • +

      Wide editor/IDE support

      +
    • +
    • +

      Many online services use RuboCop internally (e.g. HoundCI and CodeClimate)

      +
    • +
    • +

      Best logo/stickers ever

      +
    • +
    +
    +
    +

    The project is closely tied to several efforts to document and promote the best practices of the Ruby community:

    +
    + +
    +

    A long-term goal of RuboCop (and its core extensions) is to cover with cops all the guidelines from the community style guides.

    +
    +
    +
    +
    +

    Philosophy

    +
    +
    +

    Early on RuboCop aimed to be an opinionated linter/formatter that adhered very closely to the Ruby Style Guide (think gofmt and the like). +In those days cops supported just a single style and you couldn’t even turn individual cops off. Eventually, we realized +that in the Ruby community there were so many competing styles and preferences that it was going to be really +challenging to find one set of defaults that makes everyone happy. Part of this was Ruby’s own culture and philosophy, +part was the lack of common standards for almost 20 years. It’s hard to undo any of those, but it’s also not really necessary.

    +
    +
    +

    The early feedback we got led us to adopt a philosophy of (extreme) configurability and flexibility, and trying to account for every common style +of programming in Ruby. While we still believe that there’s a lot of merit to just sticking to the community +style guides, we acknowledge that Ruby is all about diversity and doing things the way that makes you happy. Whatever +style preferences you have RuboCop is there for you. That’s our promises and our guarantee. Within the subjective limits of sanity that is.

    +
    +
    +
    +
    +

    Next Steps

    +
    +
    +

    So, what to do next? While you can peruse the documentation in whatever way you’d like, here are +a few recommendations:

    +
    +
    +
      +
    • +

      See "Basic Usage" to get yourself familiar with RuboCop’s +capabilities.

      +
    • +
    • +

      Adjust RuboCop to your style/preferences. RuboCop is an extremely flexible tool and most aspects of its behavior +can be tweaked via various configuration +options. See "Configuration" for more details.

      +
    • +
    • +

      See "Versioning" for information about RuboCop versioning, +updates, and the process of introducing new cops.

      +
    • +
    • +

      Explore the existing extensions.

      +
    • +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/installation.html b/docs/rubocop/1.69/installation.html new file mode 100644 index 000000000..f5c4d74e7 --- /dev/null +++ b/docs/rubocop/1.69/installation.html @@ -0,0 +1,715 @@ + + + + + + Installation :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Installation

    +
    +

    RuboCop’s installation is pretty standard:

    +
    +
    +
    +
    $ gem install rubocop
    +
    +
    +
    +

    If you’d rather install RuboCop using bundler, don’t require it in your Gemfile:

    +
    +
    +
    +
    gem 'rubocop', require: false
    +
    +
    +
    +

    RuboCop is stable between major versions, both in terms of API and cop configuration. +We aim the ease the maintenance of RuboCop extensions and the upgrades between RuboCop +releases. All big changes are reserved for major releases. +To prevent an unwanted RuboCop update you might want to use a conservative version lock +in your Gemfile:

    +
    +
    +
    +
    gem 'rubocop', '~> 1.69', require: false
    +
    +
    +
    +
    +
    A Modular RuboCop
    +
    +

    Originally RuboCop bundled cops focused on performance and Ruby on Rails, but those were +extracted into their own gems eventually:

    +
    +
    +
      +
    • +

      the performance cops were extracted in rubocop-performance and were removed from the main gem in 0.68

      +
    • +
    • +

      the Ruby on Rails cops were extracted in rubocop-rails and were removed from the main gem in 0.72

      +
    • +
    +
    +
    +

    You’ll need to install those gems separately if you’d like to use them. +See this article for more details.

    +
    +
    +

    Additionally, RuboCop’s AST logic was extracted to rubocop-ast in RuboCop 0.84. While this change is +transparent to RuboCop’s users (rubocop-ast is a runtime dependency of rubocop), it’s good to be +aware that you can leverage RuboCop’s AST node extensions and AST node pattern matching outside of RuboCop.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/integration_with_other_tools.html b/docs/rubocop/1.69/integration_with_other_tools.html new file mode 100644 index 000000000..375baa004 --- /dev/null +++ b/docs/rubocop/1.69/integration_with_other_tools.html @@ -0,0 +1,944 @@ + + + + + + Integration with Other Tools :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Integration with Other Tools

    +
    +

    Speeding up integrations

    +
    +
    +

    RuboCop integrates with quite a number of other tools, including editors which may attempt +to do autocorrection for you. In these cases, rubocop ends up getting called repeatedly, +which may result in some slowness, as rubocop has to require its entire environment on +each call.

    +
    +
    +

    You can alleviate some of that boot time by using "Server" or +rubocop-daemon. rubocop-daemon is a +wrapper around rubocop that loads everything into a daemonized process so that +subsequent runs save on that boot time after the first execution. Please see the +rubocop-daemon documentation for setup instructions and examples of how to use it +with some editors and other tools.

    +
    +
    + + + + + +
    + + +RuboCop’s built-in caching should also be used to ensure +that source files that have not been changed are not being re-evaluated unnecessarily. +
    +
    +
    +
    +
    +

    Editor integration

    +
    +
    +

    All popular editors provide some form of linter integration. Typically this is done +either via LSP (if supported by the underlying server) or by shelling out to a linter and processing its output so it could be displayed inside the editor.

    +
    +
    +

    As noted above, the rubocop binary starts relatively slowly which makes it problematic in the shelling out case. The RuboCop "Server" functionality +has designed to address this problem and provide lightning fast editor integration.

    +
    +
    +

    LSP

    +
    +

    The Language Server Protocol is the modern standard for providing cross-editor support for various programming languages. The RuboCop LSP functionality has designed to provide built-in language server. The following is a LSP client for interacting with the built-in server:

    +
    +
    + +
    +
    +

    And the following Ruby LSP servers are using RuboCop internally to provide code linting functionality:

    +
    +
    + +
    +
    +
    +

    Emacs

    +
    +

    rubocop.el is a simple +Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs +and quickly jump between problems in your code.

    +
    +
    +

    flycheck > 0.9 also supports +RuboCop and uses it by default when available.

    +
    +
    +
    +

    Vim

    +
    +

    RuboCop is supported by +syntastic, +neomake, +and ale.

    +
    +
    +

    There is also the vim-rubocop plugin.

    +
    +
    +
    +

    Helix

    +
    +

    Helix supports Solargraph natively to provide LSP features. For formatting support, see the External binary formatter configuration for RuboCop to use either your bundled or globally installed version of RuboCop.

    +
    +
    +
    +

    Sublime Text

    +
    +

    For ST you might find the +SublimeLinter-rubocop or the +Sublime RuboCop plugin useful.

    +
    +
    +

    You may also consider using one of the LSP servers mentioned above which utilize RuboCop by using the Sublime-LSP plugin and follow its documentation for configuration.

    +
    +
    +
    +

    Brackets

    +
    +

    The brackets-rubocop +extension displays RuboCop results in Brackets. +It can be installed via the extension manager in Brackets.

    +
    +
    +
    +

    TextMate2

    +
    +

    The textmate2-rubocop +bundle displays formatted RuboCop results in a new window. +Installation instructions can be found here.

    +
    +
    +
    +

    Atom

    +
    +

    The linter-rubocop plugin for Atom’s +linter runs RuboCop and highlights the offenses in Atom.

    +
    +
    +
    +

    LightTable

    +
    +

    The lt-rubocop plugin +provides LightTable integration.

    +
    +
    +
    +

    RubyMine / Intellij IDEA

    +
    +

    RuboCop support is available as of the 2017.1 releases.

    +
    +
    +
    +

    Visual Studio Code

    +
    +

    The ruby extension +provides RuboCop integration for Visual Studio Code. RuboCop is also used for the formatting +capabilities of this extension.

    +
    +
    +
    +

    Other Editors

    +
    +

    Here’s one great opportunity to contribute to RuboCop - implement +RuboCop integration for your favorite editor.

    +
    +
    +
    +
    +
    +

    Git pre-commit hook integration with overcommit

    +
    +
    +

    overcommit is a fully configurable and +extendable Git commit hook manager. To use RuboCop with overcommit, add the +following to your .overcommit.yml file:

    +
    +
    +
    +
    PreCommit:
    +  RuboCop:
    +    enabled: true
    +
    +
    +
    +
    +
    +

    Git pre-commit hook integration with pre-commit

    +
    +
    +

    pre-commit is a framework for managing and maintaining +multi-language pre-commit hooks. To use RuboCop with pre-commit, add the +following to your .pre-commit-config.yaml file:

    +
    +
    +
    +
    - repo: https://github.com/rubocop/rubocop
    +  rev: v1.8.1
    +  hooks:
    +    - id: rubocop
    +
    +
    +
    +

    If your RuboCop configuration uses extensions, be sure to include the gems as +entries in additional_dependencies:

    +
    +
    +
    +
    - repo: https://github.com/rubocop/rubocop
    +  rev: v1.8.1
    +  hooks:
    +    - id: rubocop
    +      additional_dependencies:
    +        - rubocop-rails
    +        - rubocop-rspec
    +
    +
    +
    +
    +
    +

    Guard integration

    +
    +
    +

    If you’re fond of Guard you might +like +guard-rubocop. It +allows you to automatically check Ruby code style with RuboCop when +files are modified.

    +
    +
    +
    +
    +

    Mega-Linter integration

    +
    +
    +

    You can use Mega-Linter +to run RuboCop automatically on every PR, and also lint all file +types detected in your repository.

    +
    +
    +

    Please follow the +installation instructions +to activate RuboCop without any additional configuration.

    +
    +
    +

    Mega-Linter’s Ruby flavor +is optimized for Ruby linting.

    +
    +
    +
    +
    +

    Rake integration

    +
    +
    +

    To use RuboCop in your Rakefile add the following:

    +
    +
    +
    +
    require 'rubocop/rake_task'
    +
    +RuboCop::RakeTask.new
    +
    +
    +
    +

    If you run rake -T, the following two RuboCop tasks should show up:

    +
    +
    +
    +
    $ rake rubocop                                 # Run RuboCop
    +$ rake rubocop:autocorrect                     # Autocorrect RuboCop offenses
    +
    +
    +
    +

    The above will use default values

    +
    +
    +
    +
    require 'rubocop/rake_task'
    +
    +desc 'Run RuboCop on the lib directory'
    +RuboCop::RakeTask.new(:rubocop) do |task|
    +  task.patterns = ['lib/**/*.rb']
    +  # only show the files with failures
    +  task.formatters = ['files']
    +  # don't abort rake on failure
    +  task.fail_on_error = false
    +end
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/support.html b/docs/rubocop/1.69/support.html new file mode 100644 index 000000000..61b9fafb3 --- /dev/null +++ b/docs/rubocop/1.69/support.html @@ -0,0 +1,737 @@ + + + + + + Support :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Support

    +
    +
    +
    +

    RuboCop currently has several official & unofficial support channels. +For questions, suggestions, and support refer to one of them.

    +
    +
    + + + + + +
    + + +Please, don’t use the support channels to report issues, as this makes them +harder to track. +
    +
    +
    +
    +
    +

    GitHub Discussions

    +
    +
    +

    These days our primary discussion board is +here.

    +
    +
    +

    It’s a great place to share ideas, help other RuboCop users and just be up-to-date +with interesting developments.

    +
    +
    +
    +
    +

    Discord

    +
    +
    +

    If you’re into chat you can drop by RuboCop’s +Discord server. You can often find some +RuboCop maintainers there and get some interesting news from the project’s +kitchen.

    +
    +
    +
    +
    +

    Mailing List

    +
    +
    +

    The official mailing list is +hosted at Google Groups. It’s a low-traffic list, so don’t be too hesitant to subscribe.

    +
    +
    +
    +
    +

    Freenode

    +
    +
    +

    If you’re into IRC you can visit the #rubocop channel on Freenode. +It’s not actively +monitored by the RuboCop maintainers themselves, but still you can get support +from other RuboCop users there.

    +
    +
    +
    +
    +

    StackOverflow

    +
    +
    +

    We’re also encouraging users to ask RuboCop-related questions on StackOverflow.

    +
    +
    +

    When doing so you should use the +RuboCop tag (ideally combined +with the tag ruby).

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/autocorrect.html b/docs/rubocop/1.69/usage/autocorrect.html new file mode 100644 index 000000000..fe9062210 --- /dev/null +++ b/docs/rubocop/1.69/usage/autocorrect.html @@ -0,0 +1,832 @@ + + + + + + Autocorrect :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Autocorrect

    +
    +
    +
    +

    In autocorrect mode, RuboCop will try to automatically fix offenses:

    +
    +
    +
    +
    $ rubocop -A
    +# or
    +$ rubocop --autocorrect-all
    +
    +
    +
    +

    There are a couple of things to keep in mind about autocorrect:

    +
    +
    +
      +
    • +

      For some offenses, it is not possible to implement automatic correction.

      +
    • +
    • +

      Some automatic corrections that are possible have not been implemented yet.

      +
    • +
    • +

      Some automatic corrections might change (slightly) the semantics of the code, +meaning they’d produce code that’s mostly equivalent to the original code, but +not 100% equivalent. We call such autocorrect behavior "unsafe".

      +
    • +
    +
    +
    + + + + + +
    + + +You should always run your test suite after using the autocorrect functionality. +
    +
    +
    +
    +
    +

    Safe autocorrect

    +
    +
    +
    +
    $ rubocop -a
    +# or
    +$ rubocop --autocorrect
    +
    +
    +
    +

    In RuboCop 0.60, we began to annotate cops as Safe or not safe. The definition of +safety is that the cop doesn’t generate false positives. On top of that there’s SafeAutoCorrect +that might be set to false in cases where only the autocorrect performed by a cop +is unsafe, but that the offense detection logic is safe. To sum it up:

    +
    +
    +
      +
    • +

      Safe (true/false) - indicates whether the cop can yield false positives (by +design) or not.

      +
    • +
    • +

      SafeAutoCorrect (true/false) - indicates whether the autocorrect a cop +does is safe (equivalent) by design. If a cop is unsafe its autocorrect automatically +becomes unsafe as well.

      +
    • +
    +
    +
    +

    If a cop or its autocorrect is annotated as "not safe", it will be omitted when using --autocorrect.

    +
    +
    + + + + + +
    + + +Currently there might still be cops that aren’t marked as unsafe or +with unsafe autocorrect. Eventually, the safety of each cop will be specified +in the default configuration. +
    +
    +
    +

    Example of Unsafe Cop

    +
    +
    +
    class Miner
    +  def dig(how_deep)
    +    # ...
    +  end
    +end
    +
    +
    +Miner.new.dig(42) # => Style/SingleArgumentDig
    +                  # => Use Miner.new[] instead of dig
    +
    +
    +
    +

    This is the wrong diagnostic; this (contrived) use of dig is not an issue, +and there might not be an alternative. This cop is marked as Safe: false.

    +
    +
    +
    +
    # example.rb:
    +str = 'hello' # => Missing magic comment `# frozen_string_literal: true`
    +str << 'world'
    +
    +# autocorrects to:
    +# frozen_string_literal: true
    +
    +str = 'hello'
    +str << 'world' # => now fails because `str` is frozen
    +
    +# must be manually corrected to:
    +# frozen_string_literal: true
    +
    +str = +'hello' # => We want an unfrozen string literal here...
    +str << 'world' # => ok
    +
    +
    +
    +

    This diagnostic is valid since the magic comment is indeed missing (thus Safe: true), +but the autocorrection is not; some string literals need to be prefixed with + to avoid +having them frozen.

    +
    +
    +

    To run all autocorrections (safe and unsafe):

    +
    +
    +
    +
    $ rubocop -A
    +# or
    +$ rubocop --autocorrect-all
    +
    +
    +
    +

    It is recommended to be even more vigilant when using this option and review carefully the changes.

    +
    +
    +
    +
    +
    +

    Generating comments

    +
    +
    +
    +
    $ rubocop --autocorrect --disable-uncorrectable
    +
    +
    +
    +

    or

    +
    +
    +
    +
    $ rubocop --autocorrect-all --disable-uncorrectable
    +
    +
    +
    +

    You can add the flag --disable-uncorrectable, which will generate +# rubocop:todo comments in the code to stop the reporting of offenses that +could not be corrected automatically.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/basic_usage.html b/docs/rubocop/1.69/usage/basic_usage.html new file mode 100644 index 000000000..6449a1750 --- /dev/null +++ b/docs/rubocop/1.69/usage/basic_usage.html @@ -0,0 +1,1130 @@ + + + + + + Basic Usage :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Basic Usage

    +
    +
    +
    +

    RuboCop has three primary uses:

    +
    +
    +
      +
    1. +

      Code style checker (a.k.a. linter)

      +
    2. +
    3. +

      A replacement for ruby -w (a subset of its linting capabilities)

      +
    4. +
    5. +

      Code formatter

      +
    6. +
    +
    +
    +

    In the next sections we’ll briefly cover all of them.

    +
    +
    +
    +
    +

    Code style checker

    +
    +
    +

    Running rubocop with no arguments will check all Ruby source files +in the current directory:

    +
    +
    +
    +
    $ rubocop
    +
    +
    +
    +

    Alternatively you can pass rubocop a list of files and directories to check:

    +
    +
    +
    +
    $ rubocop app spec lib/something.rb
    +
    +
    +
    +

    Here’s RuboCop in action. Consider the following Ruby source code:

    +
    +
    +
    +
    def badName
    +  if something
    +    test
    +    end
    +end
    +
    +
    +
    +

    Running RuboCop on it (assuming it’s in a file named test.rb) would produce the following report:

    +
    +
    +
    +
    Inspecting 1 file
    +W
    +
    +Offenses:
    +
    +test.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
    +def badName
    +^
    +test.rb:1:5: C: Naming/MethodName: Use snake_case for method names.
    +def badName
    +    ^^^^^^^
    +test.rb:2:3: C: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
    +  if something
    +  ^^
    +test.rb:2:3: C: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    +  if something
    +  ^^
    +test.rb:4:5: W: Layout/EndAlignment: end at 4, 4 is not aligned with if at 2, 2.
    +    end
    +    ^^^
    +
    +1 file inspected, 5 offenses detected
    +
    +
    +
    +

    Autocorrecting offenses

    +
    +

    You can also run RuboCop in an autocorrect mode, where it will try to +automatically fix the problems it found in your code:

    +
    +
    +
    +
    $ rubocop -a
    +# or
    +$ rubocop --autocorrect
    +
    +
    +
    + + + + + +
    + + +See Autocorrect for more details. +
    +
    +
    +
    +

    Changing what RuboCop considers to be offenses

    +
    +

    RuboCop comes with a preconfigured set of rules for each of its cops, based on the Ruby Style Guide. +Depending on your project, you may wish to reconfigure a cop, tell to ignore certain files, or disable it altogether.

    +
    +
    +

    The most common way to change RuboCop’s behavior is to create a configuration file named .rubocop.yml in the +project’s root directory.

    +
    +
    +

    For more information, see Configuration.

    +
    +
    +
    +
    +
    +

    RuboCop as a replacement for ruby -w

    +
    +
    +

    RuboCop natively implements almost all ruby -w lint warning checks, and then some. If you want you can use RuboCop +simply as a replacement for ruby -w:

    +
    +
    +
    +
    $ rubocop -l
    +# or
    +$ rubocop --lint
    +
    +
    +
    +
    +
    +

    RuboCop as a formatter

    +
    +
    +

    There’s a handy shortcut to run autocorrection only on code layout (a.k.a. formatting) offenses:

    +
    +
    +
    +
    $ rubocop -x
    +# or
    +$ rubocop --fix-layout
    +
    +
    +
    + + + + + +
    + + +This option was introduced in RuboCop 0.57.0. +
    +
    +
    +
    +
    +

    Command-line flags

    +
    +
    +

    For more details check the available command-line options:

    +
    +
    +
    +
    $ rubocop -h
    +
    +
    +
    +

    To specify multiple cops for one flag, separate cops with commas and no spaces:

    +
    +
    +
    +
    $ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName
    +
    +
    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Command flagDescription

    -a/--autocorrect

    Autocorrect offenses (only when it’s safe). See Autocorrect.

    --auto-correct

    Deprecated alias of -a/--autocorrect.

    -A/--autocorrect-all

    Autocorrect offenses (safe and unsafe). See Autocorrect.

    --auto-correct-all

    Deprecated alias of -A/--autocorrect-all.

    --auto-gen-config

    Generate a configuration file acting as a TODO list.

    --[no-]color

    Force color output on or off.

    -c/--config

    Run with specified config file.

    -C/--cache

    Store and reuse results for faster operation.

    -d/--debug

    Displays some extra debug output.

    --disable-pending-cops

    Run without pending cops.

    --disable-uncorrectable

    Used with --autocorrect to annotate any offenses that do not support autocorrect with rubocop:todo comments.

    -D/--[no-]display-cop-names

    Displays cop names in offense messages. Default is true.

    --display-time

    Display elapsed time in seconds.

    --display-only-fail-level-offenses

    Only output offense messages at the specified --fail-level or above.

    --display-only-correctable

    Only output correctable offense messages.

    --display-only-safe-correctable

    Only output safe correctable offense messages.

    --enable-pending-cops

    Run with pending cops.

    --except

    Run all cops enabled by configuration except the specified cop(s) and/or departments.

    --exclude-limit

    Limit how many individual files --auto-gen-config can list in Exclude parameters, default is 15.

    -E/--extra-details

    Displays extra details in offense messages.

    -f/--format

    Choose a formatter, see Formatters.

    -F/--fail-fast

    Inspect files in order of modification time and stops after first file with offenses.

    --fail-level

    Minimum severity for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use A or autocorrect if you’d like any autocorrectable offense to trigger failure, regardless of severity.

    --force-exclusion

    Force excluding files specified in the configuration Exclude even if they are explicitly passed as arguments.

    --only-recognized-file-types

    Inspect files given on the command line only if they are listed in AllCops/Include parameters of user configuration or default configuration.

    -h/--help

    Print usage information.

    --ignore-parent-exclusion

    Ignores all Exclude: settings from all .rubocop.yml files present in parent folders. This is useful when you are importing submodules when you want to test them without being affected by the parent module’s rubocop settings.

    --ignore-unrecognized-cops

    Ignore unrecognized cops or departments in the config.

    --init

    Generate a .rubocop.yml file in the current directory.

    -l/--lint

    Run only lint cops.

    -L/--list-target-files

    List all files RuboCop will inspect.

    --[no-]auto-gen-only-exclude

    Generate only Exclude parameters and not Max when running --auto-gen-config, except if the number of files with offenses is bigger than exclude-limit. Default is false

    --[no-]auto-gen-timestamp

    Include the date and time when --auto-gen-config was run in the config file it generates. Default is true.

    --[no-]offense-counts

    Show offense counts in config file generated by --auto-gen-config. Default is true.

    --only

    Run only the specified cop(s) and/or cops in the specified departments.

    -o/--out

    Write output to a file instead of STDOUT.

    --[no-]parallel

    Use available CPUs to execute inspection in parallel. Default is parallel.

    --raise-cop-error

    Raise cop-related errors with cause and location. This is used to prevent cops from failing silently. Default is false.

    -r/--require

    Require Ruby file (see Loading Extensions).

    --regenerate-todo

    Regenerate the TODO list using the same options as the last time it was generated with --auto-gen-config (generation options can be overridden).

    --safe

    Run only safe cops.

    --safe-auto-correct

    Deprecated alias of -a/--autocorrect.

    --show-cops

    Shows available cops and their configuration.

    --show-docs-url

    Shows urls for documentation pages of supplied cops.

    --stderr

    Write all output to stderr except for the autocorrected source. This is especially useful when combined with --autocorrect and --stdin.

    -s/--stdin

    Pipe source from STDIN. This is useful for editor integration. Takes one argument, a path, relative to the root of the project. RuboCop will use this path to determine which cops are enabled (via eg. Include/Exclude), and so that certain cops like Naming/FileName can be checked.

    --editor-mode

    Optimize real-time feedback in editors, adjusting behaviors for editing experience. Editors that run RuboCop directly (e.g., by shelling out) encounter the same issues as with --lsp. This option is designed for such editors.

    -S/--display-style-guide

    Display style guide URLs in offense messages.

    -x/--fix-layout

    Autocorrect only code layout (formatting) offenses.

    -v/--version

    Displays the current version and exits.

    -V/--verbose-version

    Displays the current version plus the version of Parser and Ruby.

    +
    +

    Default command-line options are loaded from .rubocop and RUBOCOP_OPTS and are combined with command-line options that are explicitly passed to rubocop. +Thus, the options have the following order of precedence (from highest to lowest):

    +
    +
    +
      +
    1. +

      Explicit command-line options

      +
    2. +
    3. +

      Options from RUBOCOP_OPTS environment variable

      +
    4. +
    5. +

      Options from .rubocop file.

      +
    6. +
    +
    +
    +
    +
    +

    Exit codes

    +
    +
    +

    RuboCop exits with the following status codes:

    +
    +
    +
      +
    • +

      0 if no offenses are found or if the severity of all offenses are less than +--fail-level. (By default, if you use --autocorrect, offenses which are +autocorrected do not cause RuboCop to fail.)

      +
    • +
    • +

      1 if one or more offenses equal or greater to --fail-level are found. (By +default, this is any offense which is not autocorrected.)

      +
    • +
    • +

      2 if RuboCop terminates abnormally due to invalid configuration, invalid CLI +options, or an internal error.

      +
    • +
    +
    +
    +
    +
    +

    Parallel Processing

    +
    +
    +

    RuboCop enables the --parallel option by default. This option allows for +parallel processing using the number of available CPUs based on Etc.nprocessors. +If the default number of parallel processes causes excessive consumption of CPU and memory, +you can adjust the number of parallel processes using the $PARALLEL_PROCESSOR_COUNT environment variable. +Here is how to set the parallel processing to use two CPUs:

    +
    +
    +
    +
    $ PARALLEL_PROCESSOR_COUNT=2 bundle exec rubocop
    +
    +
    +
    +

    Alternatively, specifying --no-parallel will disable parallel processing.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/caching.html b/docs/rubocop/1.69/usage/caching.html new file mode 100644 index 000000000..704855ce7 --- /dev/null +++ b/docs/rubocop/1.69/usage/caching.html @@ -0,0 +1,762 @@ + + + + + + Caching :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Caching

    +
    +
    +
    +

    Large projects containing hundreds or even thousands of files can take +a really long time to inspect, but RuboCop has functionality to +mitigate this problem. There’s a caching mechanism that stores +information about offenses found in inspected files.

    +
    +
    +
    +
    +

    Cache Validity

    +
    +
    +

    Later runs will be able to retrieve this information and present the +stored information instead of inspecting the file again. This will be +done if the cache for the file is still valid, which it is if there +are no changes in:

    +
    +
    +
      +
    • +

      the contents of the inspected file

      +
    • +
    • +

      RuboCop configuration for the file

      +
    • +
    • +

      the options given to rubocop, with some exceptions that have no +bearing on which offenses are reported

      +
    • +
    • +

      the Ruby version used to invoke rubocop

      +
    • +
    • +

      version of the rubocop program (or to be precise, anything in the +source code of the invoked rubocop program)

      +
    • +
    +
    +
    +
    +
    +

    Enabling and Disabling the Cache

    +
    +
    +

    The caching functionality is enabled if the configuration parameter +AllCops: UseCache is true, which it is by default. The command +line option --cache false can be used to turn off caching, thus +overriding the configuration parameter. If AllCops: UseCache is set +to false in the local .rubocop.yml, then it’s --cache true that +overrides the setting.

    +
    +
    +
    +
    +

    Cache Path

    +
    +
    +

    By default, the cache is stored in either +$XDG_CACHE_HOME/$UID/rubocop_cache if $XDG_CACHE_HOME is set or in +$HOME/.cache/rubocop_cache/ if it’s not.

    +
    +
    +

    The root can be set to a different path in a number of ways (from +highest precedence to lowest):

    +
    +
    +
      +
    • +

      the --cache-root command line option

      +
    • +
    • +

      the $RUBOCOP_CACHE_ROOT environment variable

      +
    • +
    • +

      the AllCops: CacheRootDirectory configuration parameter

      +
    • +
    +
    +
    +

    One reason to set the cache root could be that there’s a network disk +where users on different machines want to have a common RuboCop cache. +Another could be that a Continuous Integration system allows +directories, but not a temporary directory, to be saved between runs, +or that the system caches certain folders by default.

    +
    +
    +
    +
    +

    Cache Pruning

    +
    +
    +

    Each time a file has changed, its offenses will be stored under a new +key in the cache. This means that the cache will continue to grow +until we do something to stop it. The configuration parameter +AllCops: MaxFilesInCache sets a limit, and when the number of files +in the cache exceeds that limit, the oldest files will be automatically +removed from the cache.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/lsp.html b/docs/rubocop/1.69/usage/lsp.html new file mode 100644 index 000000000..9543f1cb2 --- /dev/null +++ b/docs/rubocop/1.69/usage/lsp.html @@ -0,0 +1,1117 @@ + + + + + + LSP (Language Server Protocol) :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    LSP (Language Server Protocol)

    +
    +
    +
    + + + + + +
    + + +The built-in language server was introduced in RuboCop 1.53. This experimental feature has been under consideration for a while. +
    +
    +
    +

    The Language Server Protocol is the modern standard for providing cross-editor support for various programming languages.

    +
    +
    +

    This feature enables extremely fast interactions through the LSP.

    +
    +
    +

    Offense detection and autocorrection are performed in real-time by editors and IDEs using the language server. +The Server Mode is primarily used to speed up RuboCop runs in the terminal. +Therefore, if you want real-time feedback from RuboCop in your editor or IDE, +opting to use this language server instead of the server mode will not only provide a fast and efficient solution, +but also offer a straightforward setup for integration.

    +
    +
    +
    +
    +

    Examples of LSP Client

    +
    +
    +

    Here are examples of LSP client configurations.

    +
    +
    +

    VS Code

    +
    +

    vscode-rubocop integrates RuboCop into VS Code.

    +
    +
    +

    You can install this VS Code extension from the Visual Studio Marketplace.

    +
    +
    +

    For VS Code-based IDEs like VSCodium or Eclipse Theia, the extension can be installed from the Open VSX Registry.

    +
    +
    +
    +

    Emacs (Eglot)

    +
    +

    Eglot is a client for Language Server Protocol servers on Emacs.

    +
    +
    +

    Add the following to your Emacs configuration file (e.g. ~/.emacs.d/init.el):

    +
    +
    +
    +
    (require 'eglot)
    +
    +(add-to-list 'eglot-server-programs '(ruby-mode . ("bundle" "exec" "rubocop" "--lsp")))
    +(add-hook 'ruby-mode-hook 'eglot-ensure)
    +
    +
    +
    +

    Below is an example of additional setting for autocorrecting on save:

    +
    +
    +
    +
    (add-hook 'ruby-mode-hook (lambda () (add-hook 'before-save-hook 'eglot-format-buffer nil 'local)))
    +
    +
    +
    +

    If you run into problems, first use "M-x eglot-reconnect" to reconnect to the language server.

    +
    +
    +

    See Eglot’s official documentation for more information.

    +
    +
    +
    +

    Emacs (LSP Mode)

    +
    +

    LSP Mode is an Emacs client/library for the Language Server Protocol.

    +
    +
    +

    You can get the new lsp-mode package from MELPA.

    +
    +
    +

    See LSP Mode official documentation for more information: +https://emacs-lsp.github.io/lsp-mode/page/lsp-rubocop/

    +
    +
    +
    +

    Vim and Neovim (coc.nvim)

    +
    +

    coc.nvim is an extension host for Vim and Neovim, powered by Node.js. +It allows the loading of extensions similar to VSCode and provides hosting for language servers.

    +
    +
    +

    Add the following to your coc.nvim configuration file. For example, in Vim, it would be ~/.vim/coc-settings.json, +and in Neovim, it would be ~/.config/nvim/coc-settings.json:

    +
    +
    +
    +
    {
    +  "languageserver": {
    +    "rubocop": {
    +      "command": "bundle",
    +      "args" : ["exec", "rubocop", "--lsp"],
    +      "filetypes": ["ruby"],
    +      "rootPatterns": [".git", "Gemfile"],
    +      "requireRootPattern": true
    +    }
    +  }
    +}
    +
    +
    +
    +

    Below is an example of additional setting for autocorrecting on save:

    +
    +
    +
    +
    {
    +  "coc.preferences.formatOnSave": true
    +}
    +
    +
    +
    +

    See coc.nvim’s official documentation for more information.

    +
    +
    +
    +

    Neovim (nvim-lspconfig)

    +
    +

    nvim-lspconfig provides quickstart configs for Neovim’s LSP.

    +
    +
    +

    Add the following to your nvim-lspconfig configuration file (e.g. ~/.config/nvim/init.lua):

    +
    +
    +
    +
    vim.opt.signcolumn = "yes"
    +vim.api.nvim_create_autocmd("FileType", {
    +  pattern = "ruby",
    +  callback = function()
    +    vim.lsp.start {
    +      name = "rubocop",
    +      cmd = { "bundle", "exec", "rubocop", "--lsp" },
    +    }
    +  end,
    +})
    +
    +
    +
    +

    Below is an example of additional setting for autocorrecting on save:

    +
    +
    +
    +
    vim.api.nvim_create_autocmd("BufWritePre", {
    +  pattern = "*.rb",
    +  callback = function()
    +    vim.lsp.buf.format()
    +  end,
    +})
    +
    +
    +
    +

    See nvim-lspconfig’s official documentation for more information.

    +
    +
    +
    +

    Helix

    +
    +

    Helix is a post-modern modal text editor with built-in language server support.

    +
    +
    +

    Add the following to your Helix language configuration file (e.g. ~/.config/helix/languages.toml):

    +
    +
    +

    Helix 23.10 or later:

    +
    +
    +
    +
    [language-server.rubocop]
    +command = "bundle"
    +args = ["exec", "rubocop", "--lsp"]
    +
    +[[language]]
    +name = "ruby"
    +auto-format = true
    +language-servers = [
    +  { name = "rubocop" }
    +]
    +
    +
    +
    +

    Before Helix 23.10 or earlier:

    +
    +
    +
    +
    [[language]]
    +name = "ruby"
    +language-server = { command = "bundle", args = ["exec", "rubocop", "--lsp"] }
    +auto-format = true
    +
    +
    +
    +

    See Helix’s official documentation for more information: +https://docs.helix-editor.com/languages.html

    +
    +
    +
    +

    Sublime Text

    +
    +

    For Sublime Text LSP support is available through the Sublime-LSP plugin. +Add the following to its settings (accessible via Preferences → Package Settings → LSP → Settings) to enable RuboCop:

    +
    +
    +
    +
    {
    +    "clients": {
    +        "rubocop": {
    +            "enabled": true,
    +            "command": ["bundle", "exec", "rubocop", "--lsp"],
    +            "selector": "source.ruby | text.html.ruby | text.html.rails",
    +        },
    +    },
    +}
    +
    +
    +
    +
    +
    +
    +

    Autocorrection

    +
    +
    +

    The language server supports textDocument/formatting method and is autocorrectable. The autocorrection is safe by default (rubocop -a).

    +
    +
    +

    LSP client can switch to unsafe autocorrection (rubocop -A) by passing the following safeAutocorrect parameter in the initialize request.

    +
    +
    +
    +
    {
    +  "jsonrpc": "2.0",
    +  "id": 42,
    +  "method": "initialize",
    +  "params": {
    +    "initializationOptions": {
    +      "safeAutocorrect": false
    +    }
    +  }
    +}
    +
    +
    +
    +

    For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

    +
    +
    + + + + + +
    + + +The safeAutocorrect parameter was introduced in RuboCop 1.54. +
    +
    +
    +

    As execute commands in the workspace/executeCommand parameters, it provides rubocop.formatAutocorrects for safe autocorrections (rubocop -a) and +rubocop.formatAutocorrectsAll for unsafe autocorrections (rubocop -A). +These parameters take precedence over the initializationOptions:safeAutocorrect value set in the initialize parameter.

    +
    +
    + + + + + +
    + + +The rubocop.formatAutocorrectsAll execute command was introduced in RuboCop 1.56. +
    +
    +
    +
    +
    +

    Lint Mode

    +
    +
    +

    LSP client can run lint cops by passing the following lintMode parameter in the initialize request +if you only want to enable the feature as a linter like ruby -w:

    +
    +
    +
    +
    {
    +  "jsonrpc": "2.0",
    +  "id": 42,
    +  "method": "initialize",
    +  "params": {
    +    "initializationOptions": {
    +      "lintMode": true
    +    }
    +  }
    +}
    +
    +
    +
    +

    Furthermore, enabling autocorrect in a LSP client at the time of saving equates to the effect of rubocop -l option.

    +
    +
    +

    For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

    +
    +
    + + + + + +
    + + +The lintMode parameter was introduced in RuboCop 1.55. +
    +
    +
    +
    +
    +

    Layout Mode

    +
    +
    +

    LSP client can run layout cops by passing the following layoutMode parameter in the initialize request +if you only want to enable the feature as a formatter:

    +
    +
    +
    +
    {
    +  "jsonrpc": "2.0",
    +  "id": 42,
    +  "method": "initialize",
    +  "params": {
    +    "initializationOptions": {
    +      "layoutMode": true
    +    }
    +  }
    +}
    +
    +
    +
    +

    Furthermore, enabling autocorrect in a LSP client at the time of saving equates to the effect of rubocop -x option.

    +
    +
    +

    For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

    +
    +
    + + + + + +
    + + +The layoutMode parameter was introduced in RuboCop 1.55. +
    +
    +
    +
    +
    +

    Enable YJIT

    +
    +
    +

    YJIT, a Ruby JIT compiler, has been supported since Ruby 3.1. +In an LSP client, you can enable YJIT by launching rubocop --lsp with the RUBY_YJIT_ENABLE=1 environment variable using the env command:

    +
    +
    +
    +
    env RUBY_YJIT_ENABLE=1 bundle exec rubocop --lsp
    +
    +
    +
    +

    Below is an example for Emacs’s Eglot:

    +
    +
    +
    +
    (add-to-list 'eglot-server-programs '(ruby-mode . ("env" "RUBY_YJIT_ENABLE=1" "bundle" "exec" "rubocop" "--lsp")))
    +
    +
    +
    +

    The console of the LSP client will display +YJIT:

    +
    +
    +
    +
    RuboCop 1.63.4 language server +YJIT initialized, PID 13501
    +
    +
    +
    +

    For more details, please refer to the respective LSP configuration documentation. +In some cases, like with vscode-rubocop, it may be available as a built-in option: +https://github.com/rubocop/vscode-rubocop#rubocopyjitenabled

    +
    +
    +
    +
    +

    Run as a Language Server

    +
    +
    +

    Run rubocop --lsp command from LSP client.

    +
    +
    +

    When the language server is started, the command displays the language server’s PID:

    +
    +
    +
    +
    $ ps aux | grep 'rubocop --lsp'
    +user             17414   0.0  0.2  5557716 144376   ??  Ss    4:48PM   0:02.13 rubocop --lsp /Users/user/src/github.com/rubocop/rubocop
    +
    +
    +
    + + + + + +
    + + +rubocop --lsp is for starting LSP client, so users don’t manually execute it. +
    +
    +
    +
    +
    +

    Language Server Development

    +
    +
    +

    RuboCop provides APIs for developers of original language server or tools analogous to LSP, using RuboCop as the backend, instead of the RuboCop’s built-in LSP.

    +
    +
    +
      +
    • +

      RuboCop::LSP.enable enables LSP mode, customizing for LSP-specific features such as autocorrection and short offense message.

      +
    • +
    • +

      RuboCop::LSP.disable disables LSP mode, which can be particularly useful for testing. And intentional autocorrection by the user can be specified. +e.g, workspace/executeCommand and textDocument/codeAction LSP methods.

      +
    • +
    +
    +
    +

    When implementing custom cops, RuboCop::LSP.enabled? can be used to achieve behavior that considers these states.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/profiling.html b/docs/rubocop/1.69/usage/profiling.html new file mode 100644 index 000000000..412aa952c --- /dev/null +++ b/docs/rubocop/1.69/usage/profiling.html @@ -0,0 +1,709 @@ + + + + + + Profiling :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Profiling

    +
    +
    +
    + + + + + +
    + + +Profiling was introduced in RuboCop 1.45. +
    +
    +
    +

    RuboCop comes with the ability to profile itself while running. It has 2 CLI options:

    +
    +
    +
      +
    • +

      the --profile option will use the stackprof gem (you need to have it in Gemfile) to profile wall time and print a report at the tmp/rubocop-stackprof.dump location of the project. See the gem’s documentation on how to interpret the results

      +
    • +
    • +

      the --memory option will additionally profile memory usage using memory_profiler gem (you need to have it in Gemfile) and print a report at the tmp/rubocop-memory_profiler.txt location of the project

      +
    • +
    +
    +
    + + + + + +
    + + +Profiling memory can be very costly and time consuming when run on a large codebase. In this case, it is recommended to use it over a limited set of files (app/ directory, for example) or use a limited set of cops (with --only, for example). +
    +
    +
    +
    +
    +

    Reporting Back

    +
    +
    +

    If you found a performance problem in RuboCop, you are encouraged to report back via an issue or a pull request. See Contributing Guide for more details.

    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/usage/server.html b/docs/rubocop/1.69/usage/server.html new file mode 100644 index 000000000..393fa9264 --- /dev/null +++ b/docs/rubocop/1.69/usage/server.html @@ -0,0 +1,890 @@ + + + + + + Server Mode :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Server Mode

    +
    +
    +
    + + + + + +
    + + +The server mode was introduced in RuboCop 1.31. If you’re using an older +RuboCop version you can check out the rubocop-daemon +project that served as the inspiration for RuboCop’s built-in functionality. +
    +
    +
    +

    You can reduce the RuboCop boot time significantly (something like 850x faster) by using the --server command-line option.

    +
    +
    +

    The --server option speeds up the launch of the rubocop command by utilizing +a standalone server process that loads the RuboCop runtime production files (i.e. require 'rubocop').

    +
    +
    +

    Normally RuboCop starts somewhat slowly because it needs to require a ton of files and that’s fairly +slow. With the RuboCop server we sidestep this nasty issue and make it much more pleasant to +interact with RuboCop from text editors and IDEs.

    +
    +
    + + + + + +
    + + +The feature cannot be used on JRuby and Windows, as they do not support the fork system call. +
    +
    +
    +
    +
    +

    Run with Server

    +
    +
    +

    There are two ways to enable server:

    +
    +
    +
      +
    • +

      rubocop --server: If server process has not started yet, +start server process and execute inspection with server.

      +
    • +
    • +

      rubocop --start-server: Just start server process.

      +
    • +
    +
    +
    +

    When the server is started, it outputs the host and port.

    +
    +
    +
    +
    $ rubocop --start-server
    +RuboCop server starting on 127.0.0.1:55772.
    +
    +
    +
    + + + + + +
    + + +The rubocop command is executed using the server process if a server is started. +Whenever a server process is not running, it will load the RuboCop runtime files and execute. +(same behavior as with RuboCop 1.30 and lower) +
    +
    +
    +

    If a server is already running, the command only displays the server’s PID. A new server will not be started.

    +
    +
    +
    +
    $ rubocop --start-server
    +RuboCop server (16060) is already running.
    +
    +
    +
    +

    The server process name is basically rubocop --server and the project directory path:

    +
    +
    +
    +
    $ ps aux | grep 'rubocop --server'
    +user             16060   0.0  0.0  5078568   2264   ??  S     7:54AM   0:00.00 rubocop --server /Users/user/src/github.com/rubocop/rubocop
    +user             16337   0.0  0.0  5331560   2396   ??  S    23:51PM   0:00.00 rubocop --server /Users/user/src/github.com/rubocop/rubocop-rails
    +
    +
    +
    +

    When you run bundle update or update a local config file (e.g., .rubocop.yml or .rubocop_todo.yml), and then run rubocop, the server process will automatically restart.

    +
    +
    +
    +
    $ rubocop --server
    +RuboCop version incompatibility found, RuboCop server restarting...
    +RuboCop server starting on 127.0.0.1:60665.
    +
    +
    +
    +

    If you would like to start the server in the foreground, which can be useful when running within Docker, you can pass the --no-detach option.

    +
    +
    +
    +
    $ rubocop --start-server --no-detach
    +
    +
    +
    +
    +
    +

    Restart Server

    +
    +
    +

    The started server does not reload the configuration file. +You will need to restart the server when you upgrade RuboCop or change +the RuboCop configuration.

    +
    +
    +
    +
    $ rubocop --restart-server
    +RuboCop server starting on 127.0.0.1:55822.
    +
    +
    +
    +
    +
    +

    Command Line Options

    +
    +
    +

    These are the command-line options for server operations:

    +
    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Command flagDescription

    --server

    If a server process has not been started yet, start the server process and execute inspection with server.

    --no-server

    If a server process has been started, stop the server process and execute inspection without the server.

    --restart-server

    Restart server process.

    --start-server

    Start server process.

    --stop-server

    Stop server process.

    --server-status

    Show server status.

    --no-detach

    Run the server process in the foreground.

    +
    + + + + + +
    + + +You can specify the server host and port with the $RUBOCOP_SERVER_HOST and the $RUBOCOP_SERVER_PORT environment variables. +
    +
    +
    +

    If RUBOCOP_OPTS environment variable or .rubocop file contains --server option, rubocop command defaults to server mode. +Other server options such as stop-server, restart-server specified on the command line will take precedence over them.

    +
    +
    +
    +
    +

    Environment Variables

    +
    +
    +

    You can change the startup host and port of server process with +environment variables.

    +
    +
    +
      +
    • +

      $RUBOCOP_SERVER_HOST

      +
    • +
    • +

      $RUBOCOP_SERVER_PORT

      +
    • +
    +
    +
    +

    The following is an example:

    +
    +
    +
    +
    $ RUBOCOP_SERVER_PORT=98989 rubocop --start-server
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/v1_upgrade_notes.html b/docs/rubocop/1.69/v1_upgrade_notes.html new file mode 100644 index 000000000..e51b12eb9 --- /dev/null +++ b/docs/rubocop/1.69/v1_upgrade_notes.html @@ -0,0 +1,1016 @@ + + + + + + v1 Upgrade Notes :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    v1 Upgrade Notes

    +
    +

    Cop Upgrade guide

    +
    +
    +

    Your custom cops should continue to work in v1.

    +
    +
    +

    Nevertheless it is suggested that you tweak them to use the v1 API by following the following steps:

    +
    +
    +

    1) Your class should inherit from RuboCop::Cop::Base instead of RuboCop::Cop::Cop.

    +
    +
    +

    2) Locate your calls to add_offense and make sure that you pass as the first argument either an AST::Node, a ::Parser::Source::Comment or a ::Parser::Source::Range, and no location: named parameter.

    +
    +

    Example:

    +
    +
    +
    # Before
    +class MySillyCop < Cop
    +  def on_send(node)
    +    if node.method_name == :-
    +      add_offense(node, location: :selector, message: "Be positive")
    +    end
    +  end
    +end
    +
    +# After
    +class MySillyCop < Base
    +  def on_send(node)
    +    if node.method_name == :-
    +      add_offense(node.loc.selector, message: "Be positive")
    +    end
    +  end
    +end
    +
    +
    +
    +

    If your class supports autocorrection

    +
    +

    Your class must extend AutoCorrector.

    +
    +
    +

    The corrector is now yielded from add_offense. Move the code of your method autocorrect in that block and do not wrap your correction in a lambda. Corrector are more powerful and can now be `merge`d.

    +
    +
    +

    Example:

    +
    +
    +
    # Before
    +class MySillyCorrectingCop < Cop
    +  def on_send(node)
    +    if node.method_name == :-
    +      add_offense(node, location: :selector, message: 'Be positive')
    +    end
    +  end
    +
    +  def autocorrect(node)
    +    lambda do |corrector|
    +      corrector.replace(node.loc.selector, '+')
    +    end
    +  end
    +end
    +
    +# After
    +class MySillyCorrectingCop < Base
    +  extend AutoCorrector
    +
    +  def on_send(node)
    +    if node.method_name == :-
    +      add_offense(node.loc.selector, message: 'Be positive') do |corrector|
    +        corrector.replace(node.loc.selector, '+')
    +      end
    +    end
    +  end
    +end
    +
    +
    +
    +
    +
    +

    Instance variables

    +
    +

    Do not use RuboCop’s internal instance variables. If you used @processed_source, use processed_source. If you have a need to access an instance variable, open an issue with your use case.

    +
    +
    +

    By default, a Cop instance will be called only once for a given processed_source, so instance variables will be uninitialized when the investigation starts. Using @cache ||= …​ is fine. If you want to initialize some instance variable, the callback on_new_investigation is the best place to do so.

    +
    +
    +
    +
    class MyCachingCop < Base
    +  def on_send(node)
    +    if my_cached_data[node]
    +      @counts(node.method_name) += 1
    +      #...
    +    end
    +  end
    +
    +  # One way:
    +  def my_cached_data
    +    @data ||= processed_source.comments.map { # ... }
    +  end
    +
    +  # Another way:
    +  def on_new_investigation
    +    @counts = Hash.new(0)
    +    super  # Be nice and call super for callback
    +  end
    +end
    +
    +
    +
    +
    +

    Other API changes

    +
    +

    If your cop uses investigate, investigate_post_walk, join_force?, or internal classes like Corrector, Commissioner, Team, these have changed. See the Detailed API Changes.

    +
    +
    +
    +

    Upgrading specs

    +
    +

    It is highly recommended you use expect_offense / expect_correction / expect_no_offense in your specs, e.g.:

    +
    +
    +
    +
    require 'rubocop/rspec/support'
    +
    +RSpec.describe RuboCop::Cop::Custom::MySillyCorrectingCop, :config do
    +  # No need for `let(:cop)`
    +  it 'is positive' do
    +    expect_offense(<<~RUBY)
    +      42 + 2 - 2
    +             ^ Be positive
    +    RUBY
    +
    +    expect_correction(<<~RUBY)
    +      42 + 2 + 2
    +    RUBY
    +  end
    +
    +  it 'does not register an offense for calls to `despair`' do
    +    expect_no_offenses(<<~RUBY)
    +      "don't".despair
    +    RUBY
    +  end
    +end
    +
    +
    +
    +

    In the unlikely case where you use the class RuboCop::Cop::Corrector directly, it has changed a bit but you can ease your transition with RuboCop::Cop::Legacy::Corrector that is meant to be somewhat backwards compatible. You will need to require 'rubocop/cop/legacy/corrector'.

    +
    +
    +
    +
    +
    +

    Detailed API Changes

    +
    +
    +

    This section lists all changes (big or small) to the API. It is meant for maintainers of the nuts & bolts of RuboCop; most cop writers will not be impacted by these and are thus not the target audience.

    +
    +
    +

    Base class

    +
    +

    Legacy: Cops inherit from Cop::Cop.

    +
    +
    +

    Current: Cops inherit from Cop::Base. Having a different base class makes the implementation much cleaner and makes it easy to signal which API is being used. Cop::Cop inherits from Cop::Base and refines some methods for backward compatibility.

    +
    +
    +
    +

    add_offense API

    +
    +

    arguments

    +
    +

    Legacy: interface allowed for a node, with an optional location (symbol or range) or a range with a mandatory range as the location. Some cops were abusing the node argument and passing very different things.

    +
    +
    +

    Current: pass a range (or node as a shortcut for node.loc.expression), no location:. No abuse tolerated.

    +
    +
    +
    +

    deduping changes

    +
    +

    Both dedupe on range and won’t process the duplicated offenses at all.

    +
    +
    +

    Legacy: if offenses on same node but different range: considered as multiple offenses but a single autocorrect call.

    +
    +
    +

    Current: not applicable and not needed with autocorrection’s API.

    +
    +
    +
    +

    yield

    +
    +

    Both yield under the same conditions (unless cop is disabled for that line), but:

    +
    +
    +

    Legacy: yields after offense added to #offenses

    +
    +
    +

    Current: yields before offense is added to #offenses.

    +
    +
    +

    Even the legacy mode yields a corrector, but if a developer uses it an error will be raised asking her to inherit from Cop::Base instead.

    +
    +
    +
    +
    +

    Autocorrection

    +
    +

    #autocorrect

    +
    +

    Legacy: calls autocorrect unless it is disabled / autocorrect is off.

    +
    +
    +

    Current: yields a corrector unless it is disabled. The corrector will be ignored if autocorrecting is off, etc. No support for autocorrect method, but a warning is issued if that method is still defined.

    +
    +
    +
    +

    Empty corrections

    +
    +

    Legacy: autocorrect could return nil / false in cases where it couldn’t actually make a correction.

    +
    +
    +

    Current: No special API. Cases where no corrections are made are automatically detected.

    +
    +
    +
    +

    Correction timing

    +
    +

    Legacy: the lambda was called only later in the process, and only under specific conditions (if the autocorrect setting is turned on, etc.)

    +
    +
    +

    Current: correction is built immediately (assuming the cop isn’t disabled for the line) and applied later in the process.

    +
    +
    +
    +

    Exception handling

    +
    +

    Both: Commissioner will rescue all StandardErrors during analysis (unless option[:raise_error]) and store a corresponding ErrorWithAnalyzedFileLocation in its error list. This is done when calling the cop’s on_send & al., or when calling investigate / investigate_post_walk callback.

    +
    +
    +

    Legacy: autocorrecting cops were treating errors differently depending on when they occurred. Some errors were silently ignored. Others were rescued as above. Others crashed. Some code in Team would rescue errors and add them to the list of errors but I don’t think the code worked.

    +
    +
    +

    Current: Team no longer has any special error handling to do as potential exceptions happen when Commissioner is running.

    +
    +
    +
    +

    Other error handling

    +
    +

    Legacy: Clobbering errors are silently ignored. Calling insert_before with ranges that extend beyond the source code was silently fixed.

    +
    +
    +

    Current: Such errors are not ignored. It is still ok that a given Cop’s corrections clobber another Cop’s, but any given Cop should not issue corrections that clobber each other, or with invalid ranges, otherwise these will be listed in the processing errors.

    +
    +
    +
    +

    #corrections

    +
    +

    Legacy: Corrections were held in #corrections as an array of lambdas. A proxy was written to maintain compatibility with cop.corrections << ..., cop.corrections.concat ..., etc.

    +
    +
    +

    Current: Corrections are held in current_corrector, a Corrector which inherits from Source::TreeRewriter.

    +
    +
    +
    +

    #support_autocorrect?

    +
    +

    Legacy: was an instance method.

    +
    +
    +

    Current: now a class method.

    +
    +
    +
    +

    Joining forces

    +
    +

    Legacy: join_force?(force_class) was called with every force class

    +
    +
    +

    Current: self.joining_forces is now used to return the force (or an array of forces) to join.

    +
    +
    +
    +
    +

    Cop persistence

    +
    +

    Cops can now be persisted between files. By default new cop instances are created for each source. See support_multiple_source? documentation.

    +
    +
    +
    +

    Internal classes

    +
    +

    Corrector

    +
    +

    Legacy: initialize accepted a second argument (an array of lambdas). Available through Legacy::Corrector if needed.

    +
    +
    +

    Current: derives from parser’s `TreeRewriter. No second argument to initialize; not needed as correctors can be merged.

    +
    +
    +
    +

    Commissioner & Team

    +
    +

    Refactored for better separation of concern, being reusable, better result reporting and better error handling.

    +
    +
    +
    +
    +

    Misc API changes

    +
    +
      +
    • +

      internal API clarified for Commissioner. It calls begin_investigation and receives the results in complete_investigation.

      +
    • +
    • +

      New method add_global_offense for offenses that are not attached to a location in particular.

      +
    • +
    • +

      #offenses: No longer accessible.

      +
    • +
    • +

      Callbacks investigate(processed_source) and investigate_post_walk(processed_source) are renamed on_new_investigation and on_investigation_end and don’t accept an argument; all on_ callbacks should rely on processed_source.

      +
    • +
    • +

      #find_location is deprecated.

      +
    • +
    • +

      Correction is deprecated.

      +
    • +
    • +

      A few registry access methods were moved from Cop to Registry both for correctness (e.g. MyCop.qualified_cop_name did not work nor made sense) and so that Cop::Cop no longer holds any necessary code anymore. Backwards compatibility is maintained.

      +
      +
        +
      • +

        Cop.registry => Registry.global

        +
      • +
      • +

        Cop.all => Registry.all

        +
      • +
      • +

        Cop.qualified_cop_name => Registry.qualified_cop_name

        +
      • +
      +
      +
    • +
    • +

      The ConfigurableMax mixin for tracking exclude limits of configuration options is deprecated. Use exclude_limit ParameterName instead.

      +
    • +
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/1.69/versioning.html b/docs/rubocop/1.69/versioning.html new file mode 100644 index 000000000..f9b43e87b --- /dev/null +++ b/docs/rubocop/1.69/versioning.html @@ -0,0 +1,854 @@ + + + + + + Versioning :: RuboCop Docs + + + + + + + + + + + + + + +
    + +
    + +
    + +
    +

    Versioning

    +
    +
    +
    +

    RuboCop is stable between major versions, both in terms of API and cop +configuration. We aim the ease the maintenance of RuboCop extensions +(by keeping the API stable) and the upgrades between RuboCop releases +(by not enabling new cops and changing the configuration of existing cops). +All big (breaking) changes are reserved for major releases.

    +
    +
    +
    +
    +

    Release Policy

    +
    +
    +

    We’re following Semantic Versioning. API +compatibility between major releases is a big concern, as there are +many RuboCop extensions that can be affected by breaking API changes.

    +
    +
    + + + + + +
    + + +Dropping runtime support for a particular Ruby version is not considered a breaking change, +as it doesn’t affect clients in any way. They are simply restricted to the last version of +RuboCop supporting their Ruby runtime. In practice this means that dropping runtime support for Ruby versions +can happen in any minor release. +
    +
    +
    +

    The development cycle for the next minor (feature) release starts +immediately after the previous one has been shipped. Bug-fix (point) +releases (if any) address only serious bugs and never contain new +features.

    +
    +
    +

    Here are a few examples:

    +
    +
    +
      +
    • +

      1.1.0 - Feature release

      +
    • +
    • +

      1.1.1 - Bug-fix release

      +
    • +
    • +

      1.1.2 - Bug-fix release

      +
    • +
    • +

      1.2.0 - Feature release

      +
    • +
    +
    +
    + + + + + +
    + + +Prior to RuboCop 1.0 bumps of the minor (second) version number +were considered major releases and always included new features and/or +changes to existing features. +
    +
    +
    +
    +
    +

    Pending Cops

    +
    +
    +

    In the early versions of RuboCop a common source of frustration was that +new cops were added to pretty much every release, and as they were enabled +by default, every upgrade resulted in broken CI builds and trying to figure +out what exactly was changed. After considering many options to address +this eventually we opted for an approach that limits these type of changes +to major RuboCop releases.

    +
    +
    +

    Now new cops introduced between major versions are set to a special pending +status and are not enabled by default. A warning is emitted if such cops +are not explicitly enabled or disabled in the user configuration. Here’s +one such message:

    +
    +
    +
    +
    The following cops were added to RuboCop, but are not configured. Please
    +set Enabled to either `true` or `false` in your `.rubocop.yml` file:
    + - Style/HashEachMethods (0.80)
    + - Style/HashTransformKeys (0.80)
    + - Style/HashTransformValues (0.80)
    +For more information: https://docs.rubocop.org/rubocop/versioning.html
    +
    +
    +
    +

    You can see that 3 new cops were added in RuboCop 0.80 and it’s up to you +to decide if you want to enable or disable them.

    +
    +
    + + + + + +
    + + +Occasionally, some new cops will be introduced as disabled by +default. Usually, this means that we believe that the cop is useful, +but not for everyone. Typical cases might be the enforcement of +programming styles that are not very common in the wild, or cops that +yield too many false positives (so you’d run them manually from time +to time, instead of running them all the time). +
    +
    +
    +

    Enabling/Disabling Pending Cops in Bulk

    +
    +

    To suppress this message set NewCops to either enable or disable in your .rubocop.yml file. +You can use following configuration or the --enable-pending-cops command-line option, to enable all pending cops in bulk:

    +
    +
    +
    +
    AllCops:
    +  NewCops: enable
    +
    +
    +
    +

    Alternatively, you can use following configuration or the --disable-pending-cops command-line option, to disable all pending cops in bulk:

    +
    +
    +
    +
    AllCops:
    +  NewCops: disable
    +
    +
    +
    + + + + + +
    + + +The command-line options takes precedence over .rubocop.yml file. +
    +
    +
    +
    +

    Enabling/Disabling Individual Pending Cops

    +
    +

    Finally, you can enable/disable individual pending cops by setting their Enabled configuration to either true or false in your .rubocop.yml file:

    +
    +
    +

    Style/ANewCop is an example of a newly added pending cop:

    +
    +
    +
    +
    Style/ANewCop:
    +  Enabled: true
    +
    +
    +
    +

    or

    +
    +
    +
    +
    Style/ANewCop:
    +  Enabled: false
    +
    +
    +
    + + + + + +
    + + +On major RuboCop version updates (e.g. 1.0 → 2.0), all pending cops are enabled in bulk. +
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + diff --git a/docs/rubocop/about/changelog.html b/docs/rubocop/about/changelog.html index fa23c7169..3431b3b6a 100644 --- a/docs/rubocop/about/changelog.html +++ b/docs/rubocop/about/changelog.html @@ -7,7 +7,7 @@ - + @@ -222,6 +222,9 @@

    RuboCop

  • default
  • +
  • + 1.69 +
  • 1.68
  • @@ -586,7 +589,7 @@

    RuboCop