summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/module_with_instance_variables.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/rubocop/cop/module_with_instance_variables.rb b/rubocop/cop/module_with_instance_variables.rb
index 6ed1b986fdd..95e612b58e8 100644
--- a/rubocop/cop/module_with_instance_variables.rb
+++ b/rubocop/cop/module_with_instance_variables.rb
@@ -45,11 +45,29 @@ module RuboCop
def check_method_definition(node)
node.each_child_node(:def) do |definition|
- definition.each_descendant(:ivar, :ivasgn) do |offense|
- add_offense(offense, :expression)
+ # We allow this pattern:
+ # def f
+ # @f ||= true
+ # end
+ if only_ivar_or_assignment?(definition)
+ # We don't allow if any other ivar is used
+ definition.each_descendant(:ivar) do |offense|
+ add_offense(offense, :expression)
+ end
+ else
+ definition.each_descendant(:ivar, :ivasgn) do |offense|
+ add_offense(offense, :expression)
+ end
end
end
end
+
+ def only_ivar_or_assignment?(definition)
+ node = definition.child_nodes.last
+
+ definition.child_nodes.size == 2 &&
+ node.or_asgn_type? && node.child_nodes.first.ivasgn_type?
+ end
end
end
end