Class: RuboCop::Cop::Legion::ConstantSafety::InheritParam

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/constant_safety/inherit_param.rb

Overview

Detects ‘const_defined?` or `const_get` called with only one argument. Without `false` as the second argument, Ruby searches the entire inheritance chain including `Object`, which can cause false positives and namespace leaks.

Examples:

# bad
mod.const_defined?('Foo')
mod.const_get('Bar')

# good
mod.const_defined?('Foo', false)
mod.const_get('Bar', false)

Constant Summary collapse

MSG =
'Pass `false` as inherit parameter: `%<method>s(%<arg>s, false)`. ' \
'Default `true` leaks through `Object`.'
RESTRICT_ON_SEND =
%i[const_defined? const_get].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/legion/constant_safety/inherit_param.rb', line 27

def on_send(node)
  return unless node.arguments.size == 1

  arg = node.first_argument
  message = format(MSG, method: node.method_name, arg: arg.source)

  add_offense(node, message: message) do |corrector|
    corrector.insert_after(arg.source_range, ', false')
  end
end