Class: Rubocop::Cop::Style::OpenStructUse

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/open_struct_use.rb

Overview

Flags uses of OpenStruct, as it is now officially discouraged to be used for performance, version compatibility, and potential security issues.

See also:

Examples:

# bad
class SubClass < OpenStruct
end

# good
class SubClass
end

Constant Summary collapse

MSG =
'Avoid using `OpenStruct`. It is officially discouraged. ' \
'Replace it with `Struct`, `Hash`, or RSpec doubles. ' \
'See https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats'

Instance Method Summary collapse

Instance Method Details

#on_const(node) ⇒ Object



32
33
34
35
36
37
# File 'lib/rubocop/cop/style/open_struct_use.rb', line 32

def on_const(node)
  return unless uses_open_struct?(node)
  return if custom_class_or_module_definition?(node)

  add_offense(node)
end

#uses_open_struct?(node) ⇒ Object



28
29
30
# File 'lib/rubocop/cop/style/open_struct_use.rb', line 28

def_node_matcher :uses_open_struct?, <<-PATTERN
  (const {nil? (cbase)} :OpenStruct)
PATTERN