Skip to content

Cop idea: prevent instance variables in concerns #1047

Closed
@ydakuka

Description

@ydakuka

Actual behavior

I have the following code:

# frozen_string_literal: true

module Informable
  extend ActiveSupport::Concern

  def define_user_info
    session[:user_info_id] = @user.id
  end
end
# frozen_string_literal: true

class UsersController
  include Informable
  # include A-able
  # ...
  # include Z-able

  def create
    @user = User.first
    # other code

    define_user_info

    # other code
  end
end

I run rubocop:

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop app/controllers/concerns/informable.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

If I replace the instance variable @user with a local variable in the controller, I will get an error, because the variable user is not explicitly passed to the #define_user_info method.

Rubocop

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop -V
1.54.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.18.0
  - rubocop-factory_bot 2.23.1
  - rubocop-performance 1.18.0
  - rubocop-rails 2.20.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.22.0
  - rubocop-thread_safety 0.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions