Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider disallowing ivars in methods of any module #1

Open
mikegee opened this issue May 17, 2022 · 1 comment
Open

Consider disallowing ivars in methods of any module #1

mikegee opened this issue May 17, 2022 · 1 comment

Comments

@mikegee
Copy link
Collaborator

mikegee commented May 17, 2022

This is a recreation of covermymeds/issues/22 opened by @biinari on Jun 22, 2020.


As an extension to the suggestion in covermymeds/issues/4, perhaps we should flag up ivars used in methods of any module.

This would unfortunately add some false positives as modules may be included in classes, making instance methods and hence would be using instance variables rather than class instance variables.

They could be individually checked by the user and ignored with a rubocop:disable comment. Or perhaps this should be an option to the InstanceVariableInClassMethod, which may be set depending on how prevalent extending modules is in the user's codebase.

Concerns I would like to catch would be:

module Mod
  def some_method(something)
    @params = something
  end
end

module ExtMod
  extend Mod
end

class ExtClass
  extend Mod
end

ExtMod.some_method(something) # sets a class instance variable on ExtMod
ExtClass.some_method(something) # sets a class instance variable on ExtClass

False positives that would be difficult / impossible to rule out in a static analysis:

module Mod
  def some_method(something)
    @params = something
  end
end

class Includer
  include Mod
end

instance = Includer.new
instance.some_method(something) # sets an instance variable on instance
@SampsonCrowley
Copy link

this would cause more false positives than it would correct catches don't you think? people include and prepend modules way more often than they extend them except for the ClassMethods convention, at least from my experience.

is there a way code is capable of being analyzed for if it is being used as an extension?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants